价格、促销价格和常规价格之间有什么区别?

Dav*_*iid 7 woocommerce

几个月前,我以为我已经了解了该产品的元数据_price_regular_price以及_sale_price

我似乎记得,在进行一些测试时_price存储了“用户看到的价格”,然后如果它与之匹配,_sale_price则会显示“SALE”标签或百分比折扣,并使用 来_regular_price显示旧价格并计算百分比。

但现在我正在做一些测试,似乎并_price没有真正使用?

是我记错了,还是以前按照我说的那样工作,后来又改变了?


// Set Everything to 5$
$product->set_price(5);
$product->set_regular_price(5);
$product->set_sale_price(5);

$product->save();
Run Code Online (Sandbox Code Playgroud)

所有价格均等

// Set Price to 9$
$product->set_price(9);

// Set Sale Price to 2$
$product->set_sale_price(2);
$product->save();
Run Code Online (Sandbox Code Playgroud)

全部价格不同

// Set Sale Price to ''
$product->set_sale_price('');
$product->save();
Run Code Online (Sandbox Code Playgroud)

空售价

// Set Sale Price back to 2
$product->set_sale_price(2);
// Set Price to ''
$product->set_price('');
$product->save();
Run Code Online (Sandbox Code Playgroud)

空价,促销价 2

2 个似乎包含相同内容的元数据有什么必要?( _price, _regular_price)

我缺少什么?

Mar*_*oeb 7

我检查了 woocommerce 来源,这需要时间;-) 它归结为您的主题如何处理价格 - 在存档或作为单个产品。深入研究你的主题,你就会发现。

在我的主题中,我在https://golfballpro.de上使用存档,单个产品调用 WC_Product 类的 get_price_html() 方法:

public function get_price_html( $deprecated = '' ) {
        if ( '' === $this->get_price() ) {
            $price = apply_filters( 'woocommerce_empty_price_html', '', $this );
        } elseif ( $this->is_on_sale() ) {
            $price = wc_format_sale_price( wc_get_price_to_display( $this, array( 'price' => $this->get_regular_price() ) ), wc_get_price_to_display( $this ) ) . $this->get_price_suffix();
        } else {
            $price = wc_price( wc_get_price_to_display( $this ) ) . $this->get_price_suffix();
        }

        return apply_filters( 'woocommerce_get_price_html', $price, $this );
}
Run Code Online (Sandbox Code Playgroud)

发生的情况如下 - 代码检查存储在表 wp_postmeta 中的价格:

  1. _price 从 wp_postmeta 检索
  2. 如果该值不存在,则不会显示价格,但会调用过滤器,您可以在其中添加自己的价格逻辑。如果您不这样做,价格将保持为空。
  3. 如果产品存在键_price,它会检查该产品是否在促销,如果是,则使用两个价格 - _regular_price 和惊喜_price (促销价)
  4. 如果产品不打折,则使用 _price 字段。

所以...逻辑不会发生在检索值时,它发生在 WC_Product_Data_Store_CPT 类中保存更新时,handle_updated_props 方法从第 650 行开始:

if ( in_array( 'date_on_sale_from', $this->updated_props, true ) || in_array( 'date_on_sale_to', $this->updated_props, true ) || in_array( 'regular_price', $this->updated_props, true ) || in_array( 'sale_price', $this->updated_props, true ) || in_array( 'product_type', $this->updated_props, true ) ) {
    if ( $product->is_on_sale( 'edit' ) ) {
        update_post_meta( $product->get_id(), '_price', $product->get_sale_price( 'edit' ) );
        $product->set_price( $product->get_sale_price( 'edit' ) );
    } else {
        update_post_meta( $product->get_id(), '_price', $product->get_regular_price( 'edit' ) );
        $product->set_price( $product->get_regular_price( 'edit' ) );
    }
}
Run Code Online (Sandbox Code Playgroud)

这是 WC_Product_Data_Store_CPT 中处理保存并具有 _price、_regular_price 和 _sale_price 逻辑的代码;该代码片段在保存之前执行。因此,如果 _regular_price 更新或 _sale_price 更新,这将触发更新:

  1. 如果 SaleTo 或 SaleFrom Date、常规价格或促销价格或 pruduct_type 有更新
  2. 更新_价格

长话短说,_price 总是更新的。在你的情况下,我会检查你的主题从哪里获取价格,也许不是从 get_price_html 获取价格,因此行为是不同的。