wordpress - Woocommerce - 当产品的销售计划开始和结束时,从特定类别中自动添加和删除产品
问题描述
我编写了一个代码,用于在产品的销售计划开始时将产品添加到特定类别,并在产品的销售计划结束时从这些类别中删除。这是我的代码,但不起作用。有人可以帮助我吗?
function zt_wc_scheduled_sales() {
$data_store = WC_Data_Store::load( 'product' );
// Sales which are due to start.
$product_ids = $data_store->get_starting_sales();
if ( $product_ids ) {
do_action( 'wc_before_products_starting_sales', $product_ids );
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( $product ) {
$sale_price = $product->get_sale_price();
wp_set_object_terms( $product_id, 'pishnahad', 'product_cat', true );
$product->save();
}
}
do_action( 'wc_after_products_starting_sales', $product_ids );
WC_Cache_Helper::get_transient_version( 'product', true );
delete_transient( 'wc_products_onsale' );
}
// Sales which are due to end.
$product_ids = $data_store->get_ending_sales();
if ( $product_ids ) {
do_action( 'wc_before_products_ending_sales', $product_ids );
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( $product ) {
wp_remove_object_terms( $product_id, 'pishnahad', 'product_cat' );
$product->save();
}
}
do_action( 'wc_after_products_ending_sales', $product_ids );
WC_Cache_Helper::get_transient_version( 'product', true );
delete_transient( 'wc_products_onsale' );
}
}
add_action( 'woocommerce_scheduled_sales', 'zt_wc_scheduled_sales' );
解决方案
我已经修复了您代码中的错误。
我已经用自定义查询替换get_starting_sales()
了Firts 。get_ending_sales()
所以现在你可以获得产品ID
然后我重新排列并删除了未使用的代码。
function zt_wc_scheduled_sales() {
$data_store = WC_Data_Store::load( 'product' );
global $wpdb;
// Sales which are due to start.
$product_ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT postmeta.post_id FROM {$wpdb->postmeta} as postmeta
LEFT JOIN {$wpdb->postmeta} as postmeta_2 ON postmeta.post_id = postmeta_2.post_id
LEFT JOIN {$wpdb->postmeta} as postmeta_3 ON postmeta.post_id = postmeta_3.post_id
WHERE postmeta.meta_key = '_sale_price_dates_from'
AND postmeta_2.meta_key = '_price'
AND postmeta_3.meta_key = '_sale_price'
AND postmeta.meta_value > 0
AND postmeta.meta_value < %s",
time()
)
);
if ( $product_ids ) {
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( $product ) {
wp_set_object_terms( $product_id, 'pishnahad', 'product_cat', true );
$product->save();
}
}
}
// Sales which are due to end.
$product_ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT postmeta.post_id FROM {$wpdb->postmeta} as postmeta
LEFT JOIN {$wpdb->postmeta} as postmeta_2 ON postmeta.post_id = postmeta_2.post_id
LEFT JOIN {$wpdb->postmeta} as postmeta_3 ON postmeta.post_id = postmeta_3.post_id
WHERE postmeta.meta_key = '_sale_price_dates_to'
AND postmeta_2.meta_key = '_price'
AND postmeta_3.meta_key = '_regular_price'
AND postmeta.meta_value > 0
AND postmeta.meta_value < %s",
time()
)
);
if ( $product_ids ) {
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( $product ) {
wp_remove_object_terms( $product_id, 'pishnahad', 'product_cat' );
$product->save();
}
}
}
}
add_action( 'woocommerce_scheduled_sales', 'zt_wc_scheduled_sales' );
测试和工作。
推荐阅读
- c# - c# 从 Sharepoint 加载列表
- python - 我得到 __init__() 使用 CreateView 获得了一个意外的关键字参数“实例”
- java - 使用 HashMap 的值更新 Arraylist
- r - 使用标准化 Y 模拟回归数据
- java - 如何使用附加信息(如参数)在 java 中创建自定义堆栈跟踪?
- vba - Excel VBA:将唯一行从一张纸复制到另一张纸
- laravel-5 - 如何在paypal中退款?拉拉维尔
- go - 使用常量初始化 gocql ips
- python-3.x - 如何在 python 中使用简单的马尔科夫模型(不是“隐藏马尔科夫模型”)进行训练和预测?
- pandas - Pandas 根据另一列的日期时间值填充一列的缺失值