public function validate_add_cart_item($passed, $product_id, $quantity, $variation_id = '', $variations = '') { if ($variation_id) { $product_id = $variation_id; } // skip if not a nyp product - send original status back if (!WC_Name_Your_Price_Helpers::is_nyp($product_id)) { return $passed; } $prefix = apply_filters('nyp_field_prefix', '', $product_id); // get the posted price (can be null string) $input = WC_Name_Your_Price_Helpers::get_posted_price($product_id, $prefix); // get minimum price $minimum = WC_Name_Your_Price_Helpers::get_minimum_price($product_id); // null error message $error_message = ''; // the product title $the_product = wc_nyp_get_product($product_id); $product_title = $the_product->get_title(); // check that it is a positive numeric value if (!is_numeric($input) || is_infinite($input) || floatval($input) < 0) { $passed = false; $error_message = WC_Name_Your_Price_Helpers::error_message('invalid', array('%%TITLE%%' => $product_title)); // check that it is greater than minimum price for variable billing subscriptions } elseif ($minimum && WC_Name_Your_Price_Helpers::is_subscription($product_id) && WC_Name_Your_Price_Helpers::is_billing_period_variable($product_id)) { // get the posted billing period, defaults to 'month' $period = WC_Name_Your_Price_Helpers::get_posted_period($product_id, $prefix); // minimum billing period $minimum_period = WC_Name_Your_Price_Helpers::get_minimum_billing_period($product_id); // annual minimum $minimum_annual = WC_Name_Your_Price_Helpers::annualize_price($minimum, $minimum_period); // annual input $input_annual = WC_Name_Your_Price_Helpers::annualize_price($input, $period); // by standardizing the prices over the course of a year we can safely compare them if ($input_annual < $minimum_annual) { $passed = false; $factors = WC_Name_Your_Price_Helpers::annual_price_factors(); // If set period is in the $factors array we can calc the min price shown in the error according to entered period if (isset($factors[$period])) { $error_price = $minimum_annual / $factors[$period]; $error_period = $period; // otherwise, just show the saved minimum price and period } else { $error_price = $minimum; $error_period = $minimum_period; } // the minimum is a combo of price and period $minimum_error = wc_price($error_price) . ' / ' . $error_period; $error_message = WC_Name_Your_Price_Helpers::error_message('minimum', array('%%TITLE%%' => $product_title, '%%MINIMUM%%' => $minimum_error), $the_product); } // check that it is greater than minimum price } elseif ($minimum && floatval(WC_Name_Your_Price_Helpers::standardize_number($input)) < floatval($minimum)) { $passed = false; $minimum_error = wc_price($minimum); $error_message = WC_Name_Your_Price_Helpers::error_message('minimum', array('%%TITLE%%' => $product_title, '%%MINIMUM%%' => $minimum_error), $the_product); } // show the error message if ($error_message) { wc_add_notice($error_message, 'error'); } return $passed; }
/** * Call the Price Input Template * * @param int $product_id * @param string $prefix - prefix is key to integration with Bundles * @return void * @since 1.0 */ public function display_price_input($product_id = false, $prefix = false) { if (!$product_id) { global $product; $product_id = $product->id; } // If not NYP quit right now if (!WC_Name_Your_Price_Helpers::is_nyp($product_id) && !WC_Name_Your_Price_Helpers::has_nyp($product_id)) { return; } // load up the NYP scripts $this->nyp_scripts(); // If the product is a subscription add some items to the price input if (WC_Name_Your_Price_Helpers::is_subscription($product_id)) { // add the billing period input if (WC_Name_Your_Price_Helpers::is_billing_period_variable($product_id)) { add_filter('woocommerce_get_price_input', array('WC_Name_Your_Price_Helpers', 'get_subscription_period_input'), 10, 3); } // add the price terms add_filter('woocommerce_get_price_input', array('WC_Name_Your_Price_Helpers', 'get_subscription_terms'), 10, 2); } // get the price input template wc_get_template('single-product/price-input.php', array('product_id' => $product_id, 'prefix' => $prefix), FALSE, WC_Name_Your_Price()->plugin_path() . '/templates/'); }