首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sale_price和regular_price的价格有什么区别?

sale_price和regular_price的价格有什么区别?
EN

Stack Overflow用户
提问于 2020-11-26 15:33:32
回答 1查看 2.7K关注 0票数 7

几个月前,我以为我已经理解了该产品的元_price_regular_price_sale_price

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

但是现在我正在做一些测试,看起来好像没有真正使用_price吗?

我是记错了,还是曾经像我说的那样起作用了,后来又变了?

代码语言:javascript
复制
// Set Everything to 5$
$product->set_price(5);
$product->set_regular_price(5);
$product->set_sale_price(5);

$product->save();

代码语言:javascript
复制
// Set Price to 9$
$product->set_price(9);

// Set Sale Price to 2$
$product->set_sale_price(2);
$product->save();

代码语言:javascript
复制
// Set Sale Price to ''
$product->set_sale_price('');
$product->save();

代码语言:javascript
复制
// Set Sale Price back to 2
$product->set_sale_price(2);
// Set Price to ''
$product->set_price('');
$product->save();

为什么需要两个似乎包含相同内容的元?(_price_regular_price)

我遗漏了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-05 17:48:03

我查看了woocommerce的源代码,它花费了时间;-)它总结了您的主题如何处理价格-在一个存档或作为单一产品。深入你的主题,你就会发现。

https://golfball.pro上使用主题时,归档和单个产品调用WC_Product类的get_price_html()方法:

代码语言:javascript
复制
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 );
}

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

如果该值不存在,则从

  1. _price检索wp_postmeta
  2. ,该值不显示价格,但调用了一个筛选器,您可以在其中添加自己的价格逻辑。如果你不这样做,价格仍然是空的。如果产品存在关键的
  3. ,它会检查该产品是否正在销售,如果是,则使用两种价格-- _regular_price和惊喜 _price (出售价格)
  4. ,如果该产品不在销售,则使用_price字段。

所以..。这里的逻辑不是在检索值时发生的,而是在保存WC_Product_Data_Store_CPT类中的更新时发生的,handle_updated_props方法从第650行开始:

代码语言:javascript
复制
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' ) );
    }
}

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

如果在pruduct_type

  • Update 或SaleTo或SaleFrom日期、常规价格或销售价格中有更新,则为

长话短说,_price总是被更新。在您的情况下,我将检查您的主题是从哪里得到的价格,也许不是从get_price_html,因此行为是不同的。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65025024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档