我有一个woocommerce设置,它每天晚上导入产品,更新当前产品,如果安装中不存在它们,则创建新产品。导入创建的新产品在前端不可见。我已经进行了设置,因此没有图像的产品不会显示在产品查询中-但是它们仍会显示在产品搜索中,这是一个问题。
我希望有一个更好的解决方案,是根据创建日期更改产品的发布状态,然后将发布状态设置为“草稿”,这样它们就不可见了。
这是我到目前为止所拥有的:
add_action( 'woocommerce_before_shop_loop_item_title', 'display_new_loop_woocommerce' );
function display_new_loop_woocommerce() {
global $product;
$id = $product->get_id();
$status = 'publish';
// Get the date for the product published and current date
$datetime_created = $product->get_date_created(); // Get product created datetime
$timestamp_created = $datetime_created->getTimestamp(); // product created timestamp
$datetime_now = new WC_DateTime(); // Get now datetime (from Woocommerce datetime object)
$timestamp_now = $datetime_now->getTimestamp(); // Get now timestamp
$time_delta = $timestamp_now - $timestamp_created; // Difference in seconds
$time = 60 * 24 * 60 * 2; // X AMOUNT OF SECONDS TIMES DAYS
// If the difference is less than TIME, apply draft status
if ( $time_delta < $time ) {
$status = 'draft';
}
$my_post = array(
'ID' => $id,
'post_status' => $status
);
// Update the post into the database
wp_update_post( $my_post );
}
这不能完全起作用-状态将更改为“发布”,但不会更改为“草稿”,因此条件中的某些内容不正确。如果我尝试在条件条件中回显某些内容,并且删除了有关更新帖子状态的部分,则效果很好。
任何帮助将不胜感激-甚至是一个更好地解决问题的想法。