/** * Tells if a product is a Name Your Price product, provided that the extension is installed. * * @param mixed $product_id product or id to check * @return boolean true if NYP exists and product is a NYP */ public function is_nyp($product_id) { if (!class_exists('WC_Name_Your_Price_Helpers')) { return false; } if (WC_Name_Your_Price_Helpers::is_nyp($product_id)) { return true; } return false; }
public static function admin_price_html($price, $product) { if (WC_Name_Your_Price_Helpers::is_nyp($product) && !isset($product->is_filtered_price_html)) { $price = $product->get_price_html_from_text() . WC_Name_Your_Price_Helpers::get_price_string($product, 'minimum'); } else { if (WC_Name_Your_Price_Helpers::has_nyp($product) && !isset($product->is_filtered_price_html)) { $price = $product->get_price_html_from_text() . WC_Name_Your_Price_Helpers::get_price_string($product, 'minimum-variation'); } } return $price; }
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; }
public function available_variation($data, $product, $variation) { $is_nyp = WC_Name_Your_Price_Helpers::is_nyp($variation); $nyp_data = array('is_nyp' => $is_nyp); if ($is_nyp) { $nyp_data['minimum_price'] = WC_Name_Your_Price_Helpers::get_minimum_price($variation->variation_id); $nyp_data['initial_price'] = WC_Name_Your_Price_Helpers::get_price_value_attr($variation->variation_id); $nyp_data['price_html'] = '<span class="price">' . WC_Name_Your_Price_Helpers::get_suggested_price_html($variation) . '</span>'; $nyp_data['minimum_price_html'] = WC_Name_Your_Price_Helpers::get_minimum_price_html($variation); $nyp_data['add_to_cart_text'] = $variation->single_add_to_cart_text(); if ($product->is_type('variable-subscription')) { $nyp_data['subscription_terms'] = WC_Name_Your_Price_Helpers::get_subscription_terms('', $variation); } } return array_merge($data, $nyp_data); }
public static function variable_product_sync($product_id, $children) { if ($children) { $min_price = null; $max_price = null; $min_price_id = null; $max_price_id = null; // Main active prices $min_price = null; $max_price = null; $min_price_id = null; $max_price_id = null; // Regular prices $min_regular_price = null; $max_regular_price = null; $min_regular_price_id = null; $max_regular_price_id = null; // Sale prices $min_sale_price = null; $max_sale_price = null; $min_sale_price_id = null; $max_sale_price_id = null; foreach (array('price', 'regular_price', 'sale_price') as $price_type) { foreach ($children as $child_id) { // if NYP if (WC_Name_Your_Price_Helpers::is_nyp($child_id)) { // Skip hidden variations if ('yes' === get_option('woocommerce_hide_out_of_stock_items')) { $stock = get_post_meta($child_id, '_stock', true); if ($stock !== "" && $stock <= get_option('woocommerce_notify_no_stock_amount')) { continue; } } // get the nyp min price for this variation $child_price = get_post_meta($child_id, '_min_price', true); // if there is no set minimum, technically the min is 0 $child_price = $child_price ? $child_price : 0; // Find min price if (is_null(${"min_{$price_type}"}) || $child_price < ${"min_{$price_type}"}) { ${"min_{$price_type}"} = $child_price; ${"min_{$price_type}_id"} = $child_id; } // Find max price if (is_null(${"max_{$price_type}"}) || $child_price > ${"max_{$price_type}"}) { ${"max_{$price_type}"} = $child_price; ${"max_{$price_type}_id"} = $child_id; } } else { $child_price = get_post_meta($child_id, '_' . $price_type, true); // Skip non-priced variations if ($child_price === '') { continue; } // Skip hidden variations if ('yes' === get_option('woocommerce_hide_out_of_stock_items')) { $stock = get_post_meta($child_id, '_stock', true); if ($stock !== "" && $stock <= get_option('woocommerce_notify_no_stock_amount')) { continue; } } // Find min price if (is_null(${"min_{$price_type}"}) || $child_price < ${"min_{$price_type}"}) { ${"min_{$price_type}"} = $child_price; ${"min_{$price_type}_id"} = $child_id; } // Find max price if ($child_price > ${"max_{$price_type}"}) { ${"max_{$price_type}"} = $child_price; ${"max_{$price_type}_id"} = $child_id; } } } // Store prices update_post_meta($product_id, '_min_variation_' . $price_type, ${"min_{$price_type}"}); update_post_meta($product_id, '_max_variation_' . $price_type, ${"max_{$price_type}"}); // Store ids update_post_meta($product_id, '_min_' . $price_type . '_variation_id', ${"min_{$price_type}_id"}); update_post_meta($product_id, '_max_' . $price_type . '_variation_id', ${"max_{$price_type}_id"}); } // The VARIABLE PRODUCT price should equal the min price of any type update_post_meta($product_id, '_price', $min_price); wc_delete_product_transients($product_id); } }