add_action('init', 'automatically_trash_sold_items_in_woocommerce_callback');
function automatically_trash_sold_items_in_woocommerce_callback(){
// Get any existing copy of our transient data
if ( false === ( $automatically_trash_sold_items_in_woocommerce = get_transient( 'automatically_trash_sold_items_in_woocommerce' ) ) ) {
// It wasn't there, so regenerate the data and save the transient
global $wpdb;
$wpdb->query( "UPDATE {$wpdb->posts} P JOIN {$wpdb->postmeta} PM ON P.ID = PM.post_id SET P.post_status='trash' WHERE P.post_type='product' and PM.meta_key='_stock_status' AND PM.meta_value='outofstock'");
set_transient( 'automatically_trash_sold_items_in_woocommerce', true, 12 * HOUR_IN_SECONDS );
}
}
add_action('trash_outofstock_products', 'check_trash_outofstock_products');
// The action will trigger when someone visits your WordPress site
function trash_outofstock_products_activation() {
if ( !wp_next_scheduled( 'trash_outofstock_products' ) ) {
wp_schedule_event( time(), 'daily', 'trash_outofstock_products');
}
}
add_action('wp', 'trash_outofstock_products_activation');
function check_trash_outofstock_products() {
global $wpdb;
$results = $wpdb->get_results( "
SELECT p.ID, pm.meta_value as stock_status
FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type LIKE 'product'
AND p.post_status LIKE 'publish'
AND pm.meta_key LIKE '_stock_status'
AND pm.meta_value LIKE 'outofstock'
GROUP BY p.ID
" );
foreach($results as $result) {
wp_trash_post($result->ID);
}
}
function remove_out_of_stock_status(){
global $wpdb;
$results = $wpdb->get_results( "
DELETE p FROM {$wpdb->prefix}posts p
join {$wpdb->prefix}postmeta pm
on p.ID = pm.post_id
WHERE p.post_type = 'product'
and pm.meta_key = '_stock_status'
and pm.meta_value = 'outofstock'
" );
}
add_action( 'init', 'remove_out_of_stock_status', 10, 1 );
3条答案
按热度按时间ggazkfy81#
对于这个问题,
我进行了分析,发现与WP_Query相比,core query的执行速度更快
我发现,只需将缺货产品的post_status更改为“垃圾”即可。
下面的代码执行每12小时提供的网站是在行动。您可以根据您的需要更改时间框架。
把下面的代码片段放在活动主题的functions.php中。
9rnv2umw2#
您将需要cron作业来自动化它。您可以随时安装可湿性粉剂控制插件,您可以从其中运行事件时,您需要。我的解决方案是用sql请求收集缺货的产品id。我认为性能明智这是最快的方法。
ws51t4hk3#
您可以使用
get_results
删除outofstock
产品。