WC_Product_Variable::get_variation_price PHP Method

get_variation_price() public method

Get the min or max variation (active) price.
public get_variation_price ( string $min_or_max = 'min', boolean $include_taxes = false ) : string
$min_or_max string Min or max price.
$include_taxes boolean Should the price include taxes?
return string
    public function get_variation_price($min_or_max = 'min', $include_taxes = false)
    {
        $prices = $include_taxes ? $this->get_variation_prices_including_taxes() : $this->get_variation_prices();
        $price = 'min' === $min_or_max ? current($prices['price']) : end($prices['price']);
        return apply_filters('woocommerce_get_variation_price', $price, $this, $min_or_max, $include_taxes);
    }

Usage Example

示例#1
0
 private function get_product_from_post($post_id)
 {
     $woocommerce_ver_below_2_1 = false;
     if (version_compare(WOOCOMMERCE_VERSION, '2.1', '<')) {
         $woocommerce_ver_below_2_1 = true;
     }
     if ($woocommerce_ver_below_2_1) {
         $product = new WC_Product_Simple($post_id);
     } else {
         $product = new WC_Product($post_id);
     }
     //$post_categories = wp_get_post_categories( $post_id );
     //$categories = get_the_category();
     try {
         $thumbnail = $product->get_image();
         if ($thumbnail) {
             if (preg_match('/data-lazy-src="([^\\"]+)"/s', $thumbnail, $match)) {
                 $thumbnail = $match[1];
             } else {
                 if (preg_match('/data-lazy-original="([^\\"]+)"/s', $thumbnail, $match)) {
                     $thumbnail = $match[1];
                 } else {
                     if (preg_match('/lazy-src="([^\\"]+)"/s', $thumbnail, $match)) {
                         // Animate Lazy Load Wordpress Plugin
                         $thumbnail = $match[1];
                     } else {
                         if (preg_match('/data-echo="([^\\"]+)"/s', $thumbnail, $match)) {
                             $thumbnail = $match[1];
                         } else {
                             preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $thumbnail, $result);
                             $thumbnail = array_pop($result);
                         }
                     }
                 }
             }
         }
     } catch (Exception $e) {
         $err_msg = "exception raised in thumbnails";
         self::send_error_report($err_msg);
         $thumbnail = '';
     }
     // handling scheduled sale price update
     if (!$woocommerce_ver_below_2_1) {
         $sale_price_dates_from = get_post_meta($post_id, '_sale_price_dates_from', true);
         $sale_price_dates_to = get_post_meta($post_id, '_sale_price_dates_to', true);
         if ($sale_price_dates_from || $sale_price_dates_to) {
             self::schedule_sale_price_update($post_id, null);
         }
         if ($sale_price_dates_from) {
             self::schedule_sale_price_update($post_id, $sale_price_dates_from);
         }
         if ($sale_price_dates_to) {
             self::schedule_sale_price_update($post_id, $sale_price_dates_to);
         }
     }
     $product_tags = array();
     foreach (wp_get_post_terms($post_id, 'product_tag') as $tag) {
         $product_tags[] = $tag->name;
     }
     $product_brands = array();
     if (taxonomy_exists('product_brand')) {
         foreach (wp_get_post_terms($post_id, 'product_brand') as $brand) {
             $product_brands[] = $brand->name;
         }
     }
     $taxonomies = array();
     try {
         $all_taxonomies = get_option('wcis_taxonomies');
         if (is_array($all_taxonomies)) {
             foreach ($all_taxonomies as $taxonomy) {
                 if (taxonomy_exists($taxonomy) && !array_key_exists($taxonomy, $taxonomies)) {
                     foreach (wp_get_post_terms($post_id, $taxonomy) as $taxonomy_value) {
                         if (!array_key_exists($taxonomy, $taxonomies)) {
                             $taxonomies[$taxonomy] = array();
                         }
                         $taxonomies[$taxonomy][] = $taxonomy_value->name;
                     }
                 }
             }
         }
     } catch (Exception $e) {
     }
     $acf_fields = array();
     try {
         if (class_exists('acf') && function_exists('get_field')) {
             $all_acf_fields = get_option('wcis_acf_fields');
             if (is_array($all_acf_fields)) {
                 foreach ($all_acf_fields as $acf_field_name) {
                     $acf_field_value = get_field($acf_field_name, $post_id);
                     if ($acf_field_value) {
                         $acf_fields[$acf_field_name] = $acf_field_value;
                     }
                 }
             }
         }
     } catch (Exception $e) {
     }
     $send_product = array('product_id' => $product->id, 'currency' => get_woocommerce_currency(), 'price' => $product->get_price(), 'url' => get_permalink($product->id), 'thumbnail_url' => $thumbnail, 'action' => 'insert', 'description' => $product->get_post_data()->post_content, 'short_description' => $product->get_post_data()->post_excerpt, 'name' => $product->get_title(), 'sku' => $product->get_sku(), 'categories' => $product->get_categories(), 'tag' => $product_tags, 'store_id' => get_current_blog_id(), 'identifier' => (string) $product->id, 'product_brand' => $product_brands, 'taxonomies' => $taxonomies, 'acf_fields' => $acf_fields, 'sellable' => $product->is_purchasable(), 'visibility' => $product->is_visible(), 'stock_quantity' => $product->get_stock_quantity(), 'is_managing_stock' => $product->managing_stock(), 'is_backorders_allowed' => $product->backorders_allowed(), 'is_purchasable' => $product->is_purchasable(), 'is_in_stock' => $product->is_in_stock(), 'product_status' => get_post_status($post_id));
     try {
         $variable = new WC_Product_Variable($post_id);
         $variations = $variable->get_available_variations();
         $variations_sku = '';
         if (!empty($variations)) {
             foreach ($variations as $variation) {
                 if ($product->get_sku() != $variation['sku']) {
                     $variations_sku .= $variation['sku'] . ' ';
                 }
             }
         }
         $send_product['variations_sku'] = $variations_sku;
         $all_attributes = $product->get_attributes();
         $attributes = array();
         if (!empty($all_attributes)) {
             foreach ($all_attributes as $attr_mame => $value) {
                 if ($all_attributes[$attr_mame]['is_taxonomy']) {
                     if (!$woocommerce_ver_below_2_1) {
                         $attributes[$attr_mame] = wc_get_product_terms($post_id, $attr_mame, array('fields' => 'names'));
                     } else {
                         $attributes[$attr_mame] = woocommerce_get_product_terms($post_id, $attr_mame, 'names');
                     }
                 } else {
                     $attributes[$attr_mame] = $product->get_attribute($attr_mame);
                 }
             }
         }
         $send_product['attributes'] = $attributes;
         $send_product['total_variable_stock'] = $variable->get_total_stock();
         try {
             if (version_compare(WOOCOMMERCE_VERSION, '2.2', '>=')) {
                 if (function_exists('wc_get_product')) {
                     $original_product = wc_get_product($product->id);
                     if (is_object($original_product)) {
                         $send_product['visibility'] = $original_product->is_visible();
                         $send_product['product_type'] = $original_product->product_type;
                     }
                 }
             } else {
                 if (function_exists('get_product')) {
                     $original_product = get_product($product->id);
                     if (is_object($original_product)) {
                         $send_product['visibility'] = $original_product->is_visible();
                         $send_product['product_type'] = $original_product->product_type;
                     }
                 }
             }
         } catch (Exception $e) {
         }
     } catch (Exception $e) {
         $err_msg = "exception raised in attributes";
         self::send_error_report($err_msg);
     }
     if (!$woocommerce_ver_below_2_1) {
         try {
             $send_product['price_compare_at_price'] = $product->get_regular_price();
             $send_product['price_min'] = $variable->get_variation_price('min');
             $send_product['price_max'] = $variable->get_variation_price('max');
             $send_product['price_min_compare_at_price'] = $variable->get_variation_regular_price('min');
             $send_product['price_max_compare_at_price'] = $variable->get_variation_regular_price('max');
         } catch (Exception $e) {
             $send_product['price_compare_at_price'] = null;
             $send_product['price_min'] = null;
             $send_product['price_max'] = null;
             $send_product['price_min_compare_at_price'] = null;
             $send_product['price_max_compare_at_price'] = null;
         }
     } else {
         $send_product['price_compare_at_price'] = null;
         $send_product['price_min'] = null;
         $send_product['price_max'] = null;
         $send_product['price_min_compare_at_price'] = null;
         $send_product['price_max_compare_at_price'] = null;
     }
     $send_product['description'] = self::content_filter_shortcode_with_content($send_product['description']);
     $send_product['short_description'] = self::content_filter_shortcode_with_content($send_product['short_description']);
     $send_product['description'] = self::content_filter_shortcode($send_product['description']);
     $send_product['short_description'] = self::content_filter_shortcode($send_product['short_description']);
     try {
         if (defined('ICL_SITEPRESS_VERSION') && is_plugin_active('woocommerce-multilingual/wpml-woocommerce.php') && function_exists('wpml_get_language_information')) {
             if (version_compare(ICL_SITEPRESS_VERSION, '3.2', '>=')) {
                 $language_info = apply_filters('wpml_post_language_details', NULL, $post_id);
             } else {
                 $language_info = wpml_get_language_information($post_id);
             }
             if ($language_info && is_array($language_info) && array_key_exists('locale', $language_info)) {
                 // WP_Error could be returned from wpml_get_language_information(...)
                 $send_product['lang'] = $language_info['locale'];
             }
         }
     } catch (Exception $e) {
     }
     return $send_product;
 }