【问题标题】:Get the regular price of variable product in Woocommerce在 Woocommerce 中获取可变产品的正常价格
【发布时间】:2019-03-27 23:48:37
【问题描述】:

我找到了很多答案,但没有一个适用于可变产品。我想同时显示可变产品的销售价格和正常价格。

global $product;
if( $product->is_on_sale() ) {
    $sale_price = $product->get_sale_price();
}
$regular_price = $product->get_regular_price();

这是有效的,但前提是它是普通产品。我认为即使产品变化也一定有可能,但我没有找到。

感谢任何帮助

【问题讨论】:

    标签: php wordpress woocommerce product price


    【解决方案1】:

    对于可变产品的价格,您需要使用特定于WC_Product_Variable 类的不同方法。可变产品由多个产品变体组成,因此可变产品的价格就是它的产品变体价格。

    您可以获得可变产品的最低或最高价格,例如:

    global $product;
    
    // Regular price min and max
    $min_regular_price = $product->get_variation_regular_price( 'min' );
    $max_regular_price = $product->get_variation_regular_price( 'max' );
    
    // Sale price min and max
    $min_sale_price = $product->get_variation_sale_price( 'min' );
    $max_sale_price = $product->get_variation_sale_price( 'max' );
    
    // The active price min and max
    $min_price = $product->get_variation_price( 'min' );
    $max_price = $product->get_variation_price( 'max' );
    

    对于可变产品,如果其中一个产品变体正在销售,则方法 is_on_sale() 将返回 true...

    您还可以使用get_variation_prices() 方法,该方法将为您提供一个多维数组,其中包含在变量产品中设置的所有产品变体价格(有效价格、常规价格和销售价格)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-15
      • 1970-01-01
      • 2019-07-22
      • 2019-02-12
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多