作为WooCommerce网站的一部分,我希望有一个销售页面,列出销售项目(分页和过滤).我认为最好的办法是将"促销"类别自动添加到销售中的任何帖子中(因为类别页面允许自动过滤和分页).
到目前为止,我有这个代码,以便在保存时以编程方式将销售类别添加到产品中:
function update_test( $product) {
wp_set_object_terms($product, 'sale', 'product_cat', true );
}
add_action( 'save_post', 'update_test', 1, 2);`
Run Code Online (Sandbox Code Playgroud)
但是,我只希望在产品开始销售时(即设置了销售价格)发生这种情况,以便保存非销售的帖子不会增加销售类别.我尝试过几种不同的东西,但没有运气.我试过这个,但它没有用:
function update_test( $product ) {
if($product->is_on_sale()){
wp_set_object_terms($product, 'sale', 'product_cat', true );
}
}
add_action( 'save_post', 'update_test', 1, 2);`
Run Code Online (Sandbox Code Playgroud)
但这只是让我的网站冻结保存.
有任何想法吗?
安迪