/** * Returns an array of option values for the given measurement. This is * used for the pricing calculator only. * * @since 3.0 * @param string $measurement_name the measurement name * @return array associative array of measurement option values to label */ public function get_options($measurement_name) { $calculator_type = $this->get_calculator_type(); $options = array(); if ($this->is_pricing_calculator_enabled() && isset($this->settings[$calculator_type][$measurement_name]['options'])) { foreach ($this->settings[$calculator_type][$measurement_name]['options'] as $value) { if ('' !== $value) { $result = WC_Price_Calculator_Measurement::convert_to_float($value); $options[(string) $result] = $value; } } } return $options; }
/** * Filter to check whether a product is valid to be added to the cart. * This is used to ensure a measurement is provided when the price * calculator is used * * @since 3.0 * @param boolean $valid whether the product as added is valid * @param int $product_id the product identifier * @param int $quantity the amount being added * @param int $variation_id optional variation id * @param array $variations optional variation configuration * @param array $cart_item_data optional cart item data. This will only be * supplied when an order is being-ordered, in which case the * required measurements will not be available from the REQUEST array */ public function add_to_cart_validation($valid, $product_id, $quantity, $variation_id = '', $variations = array(), $cart_item_data = array()) { $product = wc_get_product($product_id); // is the calculator enabled for this product? if ($valid && WC_Price_Calculator_Product::pricing_calculator_enabled($product)) { $settings = new WC_Price_Calculator_Settings($product); $measurements = $settings->get_calculator_measurements(); // the individual measurements (for simple calculators like the length or weight or area this will be just the length/weight/area/whatever, // while for more complicated ones like area-dimension this will be the length and width foreach ($measurements as $measurement) { $value = null; if (isset($_REQUEST[$measurement->get_name() . '_needed'])) { $value = str_replace(get_option('woocommerce_price_decimal_sep'), '.', $_REQUEST[$measurement->get_name() . '_needed']); } elseif (isset($cart_item_data['pricing_item_meta_data'][$measurement->get_name()])) { $value = $cart_item_data['pricing_item_meta_data'][$measurement->get_name()]; } // save the value of measurement needed if ($value) { $value = WC_Price_Calculator_Measurement::convert_to_float($value); $this->measurements_needed[$measurement->get_name()] = array('value' => $value, 'unit' => $measurement->get_unit(), 'common_unit' => $measurement->get_unit_common()); } if (!$value || !is_numeric($value) || $value <= 0) { wc_add_notice(sprintf(__("%s missing", WC_Measurement_Price_Calculator::TEXT_DOMAIN), $measurement->get_label()), 'error'); $valid = false; } } // get the measurement needed, from the $_POST object for a normal add to cart action, or from the $cart_item_data for a programmatic add-to-cart $measurement_needed_value = null; if (isset($_POST['_measurement_needed'])) { $measurement_needed_value = $_POST['_measurement_needed']; $measurement_needed_value_unit = $_POST['_measurement_needed_unit']; } elseif (isset($cart_item_data['pricing_item_meta_data']['_measurement_needed_internal'])) { $measurement_needed_value = $cart_item_data['pricing_item_meta_data']['_measurement_needed_internal']; $measurement_needed_value_unit = $cart_item_data['pricing_item_meta_data']['_measurement_needed_unit_internal']; } // if pricing rules are enabled validate there is a matching rule available if ($settings->pricing_rules_enabled() && $measurement_needed_value) { $measurement_needed = new WC_Price_Calculator_Measurement($measurement_needed_value_unit, $measurement_needed_value); if (is_null($settings->get_pricing_rules_price($measurement_needed))) { $message = apply_filters('wc_measurement_price_calculator_no_price_available_notice_text', __("No price available for a product with this measurement, please contact the store for assistance.", WC_Measurement_Price_Calculator::TEXT_DOMAIN), $product); wc_add_notice($message, 'error'); $valid = false; } } // allow other code to validate based on the provided measurements $valid = apply_filters('wc_measurement_price_calculator_add_to_cart_validation', $valid, $product_id, $quantity, $measurements); } return $valid; }