几个月前,我以为我已经理解了该产品的元_price、_regular_price和_sale_price。
我似乎还记得,在进行一些测试时,_price存储了“用户看到的价格”,如果它与_sale_price匹配,则会显示“出售”标签或百分比折扣,并使用_regular_price显示旧价格并计算百分比。
但是现在我正在做一些测试,看起来好像没有真正使用_price吗?
我是记错了,还是曾经像我说的那样起作用了,后来又变了?
// Set Everything to 5$
$product->set_price(5);
$product->set_regular_price(5);
$product->set_sale_price(5);
$product->save();

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

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

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

为什么需要两个似乎包含相同内容的元?(_price,_regular_price)
我遗漏了什么?
发布于 2021-12-05 17:48:03
我查看了woocommerce的源代码,它花费了时间;-)它总结了您的主题如何处理价格-在一个存档或作为单一产品。深入你的主题,你就会发现。
在https://golfball.pro上使用主题时,归档和单个产品调用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 );
}所发生的情况如下--代码检查存储在表wp_postmeta中的价格:
如果该值不存在,则从
所以..。这里的逻辑不是在检索值时发生的,而是在保存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' ) );
}
}这是WC_Product_Data_Store_CPT中处理保存的代码,并具有_price、_regular_price和_sale_price的逻辑;此代码段在保存之前执行。因此,如果更新了_regular_price或更新了_sale_price,这将触发更新:
如果在pruduct_type
长话短说,_price总是被更新。在您的情况下,我将检查您的主题是从哪里得到的价格,也许不是从get_price_html,因此行为是不同的。
https://stackoverflow.com/questions/65025024
复制相似问题