/** * Load price input script * * @return void */ function nyp_scripts() { wp_enqueue_script('accounting'); wp_enqueue_script('woocommerce-nyp'); $params = array('currency_format_num_decimals' => esc_attr(wc_nyp_get_price_decimals()), 'currency_format_symbol' => get_woocommerce_currency_symbol(), 'currency_format_decimal_sep' => esc_attr(wc_nyp_get_price_decimal_separator()), 'currency_format_thousand_sep' => esc_attr(wc_nyp_get_price_thousand_separator()), 'currency_format' => esc_attr(str_replace(array('%1$s', '%2$s'), array('%s', '%v'), get_woocommerce_price_format())), 'annual_price_factors' => WC_Name_Your_Price_Helpers::annual_price_factors(), 'minimum_error' => WC_Name_Your_Price_Helpers::error_message('minimum_js')); wp_localize_script('woocommerce-nyp', 'woocommerce_nyp_params', $params); }
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; }