コード例 #1
0
 /**
  * Renders the price/sale price in terms of a unit of measurement for display
  * on the catalog/product pages
  *
  * @since 3.0
  * @param $price string the formatted sale price
  * @param $product WC_Product the product
  * @return string the formatted sale price, per unit
  */
 public function price_per_unit_html($price_html, $product)
 {
     // if this is a product variation, get the parent product which holds the calculator settings
     $_product = $product;
     if (isset($product->variation_id) && $product->variation_id) {
         $_product = SV_WC_Plugin_Compatibility::wc_get_product($product->id);
     }
     if (WC_Price_Calculator_Product::pricing_per_unit_enabled($_product)) {
         $settings = new WC_Price_Calculator_Settings($product);
         // if this is a quantity calculator, the displayed price per unit will have to be calculated from
         //  the product price and pricing measurement.  alternatively, for a pricing calculator product,
         //  the price set in the admin *is* the price per unit, so we just need to format it by adding the units
         if ($settings->is_quantity_calculator_enabled()) {
             $measurement = null;
             // for variable products we must go synchronize price levels to our per unit price
             if ($product->is_type('variable')) {
                 // synchronize to the price per unit pricing
                 WC_Price_Calculator_Product::variable_product_sync($product, $settings);
                 // get price suffix and remove it from the price html
                 $price_suffix = $product->get_price_suffix();
                 add_filter('woocommerce_get_price_suffix', '__return_empty_string');
                 // remove the price_html filters, get the appropriate price html, then re-add them
                 $this->remove_price_html_filters();
                 $price_html = $product->get_price_html();
                 $this->add_price_html_filters();
                 // re-add price suffix
                 remove_filter('woocommerce_get_price_suffix', '__return_empty_string');
                 // add units
                 $price_html .= ' ' . __($settings->get_pricing_label(), WC_Measurement_Price_Calculator::TEXT_DOMAIN);
                 // add price suffix
                 $price_html .= $price_suffix;
                 // restore the original values
                 WC_Price_Calculator_Product::variable_product_unsync($product, $settings);
             } else {
                 // other product types
                 $measurement = WC_Price_Calculator_Product::get_product_measurement($product, $settings);
                 $measurement->set_unit($settings->get_pricing_unit());
                 if ($measurement && $measurement->get_value() && '' !== $price_html) {
                     // save the original price and remove the filter that we're currently within, to avoid an infinite loop
                     $original_price = $product->price;
                     $original_regular_price = $product->regular_price;
                     $original_sale_price = $product->sale_price;
                     // calculate the price per unit, then format it
                     $product->price = $product->price / $measurement->get_value();
                     $product->regular_price = $product->regular_price / $measurement->get_value();
                     $product->sale_price = $product->sale_price / $measurement->get_value();
                     $product = apply_filters('wc_measurement_price_calculator_quantity_price_per_unit', $product, $measurement);
                     // get price suffix and remove it from the price html
                     $price_suffix = $product->get_price_suffix();
                     add_filter('woocommerce_get_price_suffix', '__return_empty_string');
                     // remove the price_html filters, get the appropriate price html, then re-add them
                     $this->remove_price_html_filters();
                     $price_html = $product->get_price_html();
                     $this->add_price_html_filters();
                     // re-add price suffix
                     remove_filter('woocommerce_get_price_suffix', '__return_empty_string');
                     // restore the original product price and price_html filters
                     $product->price = $original_price;
                     $product->regular_price = $original_regular_price;
                     $product->sale_price = $original_sale_price;
                     // add units
                     $price_html .= ' ' . __($settings->get_pricing_label(), WC_Measurement_Price_Calculator::TEXT_DOMAIN);
                     // add price suffix
                     $price_html .= $price_suffix;
                 }
             }
         } else {
             // pricing calculator
             if ($settings->pricing_rules_enabled()) {
                 // pricing rules product
                 $price_html = WC_Price_Calculator_Product::get_pricing_rules_price_html($product);
             } elseif ('' !== $price_html) {
                 // normal pricing calculator non-empty price: add units
                 $price_html .= ' ' . __($settings->get_pricing_label(), WC_Measurement_Price_Calculator::TEXT_DOMAIN);
             }
         }
     }
     return $price_html;
 }