/**
 * Add product page information if calculator is enabled
 * Example: Add a heading above the calculator section
 */
function sv_wc_measurement_price_calculator_heading()
{
    // bail if MPC isn't active
    if (!class_exists('WC_Price_Calculator_Product')) {
        return;
    }
    global $product;
    $measurement = WC_Price_Calculator_Product::calculator_enabled($product);
    // if the calculator is enabled, add the heading
    if ($measurement) {
        echo '<h4>Enter Your Needed Measurements</h4>';
    }
}
/**
 * Hide the unit price from the top of the product page
 * for Measurement Calculator products
 */
function sv_wc_measurement_price_calculator_hide_unit_price()
{
    // bail if the calculator isn't active or this isn't a product page
    if (!class_exists('WC_Price_Calculator_Product') || !is_product()) {
        return;
    }
    global $product;
    $measurement = WC_Price_Calculator_Product::calculator_enabled($product);
    // if the calculator is enabled, hide unit price
    if ($measurement) {
        echo '<style>.product form .price { display: none; }</style>';
    }
}
/**
 * Changes MPC width / length inputs to number type inputs
 *   then adds min / max values accepted for each
 *
 * requires HTML5 support
 */
function sv_wc_measurement_price_calculator_input_min_max()
{
    // bail unless we're on a product page and MPC is active
    if (!(is_product() && class_exists('WC_Price_Calculator_Product'))) {
        return;
    }
    global $product;
    // bail unless the calculator is enabled for this product, this is also why we hook into the footer scripts
    // since this isn't available immediately at page load
    if (!WC_Price_Calculator_Product::calculator_enabled($product)) {
        return;
    }
    wc_enqueue_js("\n\t\t\$('#length_needed').attr({ type: 'number', min: '.1', max: '24', step: '.1' }).addClass('input-text');\n\t\t\$('#width_needed').attr({ type: 'number', min: '.1', max: '5', step: '.1' }).addClass('input-text');\n\t");
}
 /**
  * Add the pricing calculator and quantity input if the user can view the price
  *
  * @since 3.7.0
  */
 public function catalog_visibility_options_pricing_calculator_quantity_input()
 {
     global $product;
     // bail if the calculator is not enabled for this product
     if (!$product || !WC_Price_Calculator_Product::calculator_enabled($product)) {
         return;
     }
     // bail if current user can't view the price
     if (class_exists('WC_Catalog_Restrictions_Filters') && !WC_Catalog_Restrictions_Filters::instance()->user_can_view_price($product)) {
         return;
     }
     // render pricing calculator
     wc_measurement_price_calculator()->get_product_page_instance()->render_price_calculator();
     // render quantity input
     if (!$product->is_sold_individually()) {
         woocommerce_quantity_input(array('min_value' => apply_filters('woocommerce_quantity_input_min', 1, $product), 'max_value' => apply_filters('woocommerce_quantity_input_max', $product->backorders_allowed() ? '' : $product->get_stock_quantity(), $product)));
     }
 }
 /**
  * Render the price calculator on the product page
  *
  * @since 3.0
  */
 public function render_price_calculator()
 {
     global $product, $wc_measurement_price_calculator;
     // is the calculator enabled for this product?
     if (!$product || !WC_Price_Calculator_Product::calculator_enabled($product)) {
         return;
     }
     $settings = new WC_Price_Calculator_Settings($product);
     if (WC_Price_Calculator_Product::pricing_calculator_enabled($product)) {
         // Pricing calculator with custom dimensions and a price "per unit"
         // get the product total measurement (ie Area or Volume, etc)
         $product_measurement = WC_Price_Calculator_Product::get_product_measurement($product, $settings);
         $product_measurement->set_unit($settings->get_pricing_unit());
         // get the product measurements, get a measurement, and set the product total measurement common unit based on the measurements common unit
         $measurements = $settings->get_calculator_measurements();
         list($measurement) = $measurements;
         $product_measurement->set_common_unit($measurement->get_unit_common());
         // pricing calculator enabled, get the template
         wc_get_template('single-product/price-calculator.php', array('product_measurement' => $product_measurement, 'settings' => $settings, 'measurements' => $measurements), '', $wc_measurement_price_calculator->get_plugin_path() . '/templates/');
         // need an element to contain the price for simple pricing rule products
         if ($product->is_type('simple') && $settings->pricing_rules_enabled()) {
             echo '<div class="single_variation"></div>';
         }
     } else {
         // quantity calculator.  where the quantity of product needed is based on the configured product dimensions.  This is a actually bit more complex
         // get the starting quantity, max quantity, and total product measurement in product units
         $quantity_range = WC_Price_Calculator_Product::get_quantity_range($product);
         // set the product measurement based on the minimum quantity value, and set the unit to the frontend calculator unit
         $measurements = $settings->get_calculator_measurements();
         // The product measurement will be used to create the 'amount actual' field.
         $product_measurement = WC_Price_Calculator_Product::get_product_measurement($product, $settings);
         // see whether all calculator measurements are defined in the same units (ie 'in', 'sq. in.' are considered the same)
         $measurements_unit = null;
         foreach ($measurements as $measurement) {
             if (!$measurements_unit) {
                 $measurements_unit = $measurement->get_unit();
             } else {
                 if (!WC_Price_Calculator_Measurement::compare_units($measurements_unit, $measurement->get_unit())) {
                     $measurements_unit = false;
                     break;
                 }
             }
         }
         // All calculator measurements use the same base units, so lets use those for the 'amount actual' field
         //  area/volume product measurement can have a calculator measurement defined in units of length, so it
         //  will need to be converted to units of area or volume respectively
         if ($measurements_unit) {
             switch ($product_measurement->get_type()) {
                 case 'area':
                     $measurements_unit = WC_Price_Calculator_Measurement::to_area_unit($measurements_unit);
                     break;
                 case 'volume':
                     $measurements_unit = WC_Price_Calculator_Measurement::to_volume_unit($measurements_unit);
                     break;
             }
         }
         // if the price per unit is displayed for this product, default to the pricing units for the 'amount actual' field
         if (WC_Price_Calculator_Product::pricing_per_unit_enabled($product)) {
             $measurements_unit = $settings->get_pricing_unit();
         }
         // if a measurement unit other than the default was determined, set it
         if ($measurements_unit) {
             $product_measurement->set_unit($measurements_unit);
         }
         $total_price = '';
         if ($product->is_type('simple')) {
             // If the product type is simple we can set an initial 'Amount Actual' and 'total price'
             //  we can't do this for variable products because we don't know which will be configured
             //  initially (actually I guess a default product can be configured, so maybe we can do something here)
             // not enough product physical attributes defined to get our measurement, so bail
             if (!$product_measurement->get_value()) {
                 return;
             }
             // figure out the starting measurement amount
             // multiply the starting quantity by the measurement value
             $product_measurement->set_value(round($quantity_range['min_value'] * $product_measurement->get_value(), 2));
             $total_price = wc_price($quantity_range['min_value'] * $product->get_price(), 2);
         } elseif ($product->is_type('variable')) {
             // clear the product measurement value for variable products, since we can't really know what it is ahead of time (except for when a default is set)
             $product_measurement->set_value('');
         }
         // pricing calculator enabled, get the template
         wc_get_template('single-product/quantity-calculator.php', array('calculator_type' => $settings->get_calculator_type(), 'product_measurement' => $product_measurement, 'measurements' => $measurements, 'total_price' => $total_price), '', $wc_measurement_price_calculator->get_plugin_path() . '/templates/');
     }
 }