通过自定义 cron 作业功能向 WooCommerce 产品添加产品标签

问题描述 投票:0回答:1

我有一个自定义的 cron 作业,当预订产品到达预定日期时,它会自动将其更新为“缺货”。

有没有办法我也可以在产品中添加产品标签作为此 cron 作业的一部分?

这是我用于库存状态更新的代码:

// Custom Cron hooked Function called by wp_schedule_event
add_action( 'jape_pre_order_stock_update', 'update_pre_order_stock_status' );
function update_pre_order_stock_status(){
    // Get all related product IDs to update via a WP_Query
    $products_ids = get_posts( [
        'post_type'     => 'product',
        'post_status'   => 'publish',
        'numberposts'   => 100, // <= the number of products to process (-1 is all)
        'fields'        => 'ids',
        'tax_query'     => array( array(
            'taxonomy'  => 'product_cat',
            'field'     => 'slug',
            'terms'     => array(
                'comic-book-pre-orders',
                'dc-comics-pre-orders',
                'image-comics-pre-orders',
                'manga-pre-orders',
                'marvel-comics-pre-orders',
                'other-publisher-pre-orders',
            ),
        ) ),
        'meta_query' => array( 
            'relation' => 'AND',
            array(
                'key'       => '_stock_status',
                'value'     => 'instock',
                'compare'   => '=',
            ),
            array(
                'key'       => 'woo_expiry_date',
                'value'     => date_i18n('Y-m-d'),
                'compare'   => '<=',
                'type'      => 'DATE',
            ),
            array(
                'key'       => 'woo_expiry_action',
                'value'     => 'out',
                'compare'   => '=',
            ),
        ),
    ] );

    foreach( $products_ids as $product_id ) {
        $product = wc_get_product($product_id);
        $shipping_class_id = 1002;

        $product->set_stock_quantity(0); // Set stock quantity
        $product->set_stock_status('outofstock'); // Set stock status
        $product->set_shipping_class_id( $shipping_class_id ); // Set the shipping class ID
        $product->save(); // Save updated product data
    }
    
    rocket_clean_post( $product );
    
}

我有这个代码,用于在产品售完时添加标签,但无法弄清楚如何将其应用于上面的 cron 作业中的预订单:

// Add Out Of Stock tag to products when they sell out
function action_woocommerce_no_stock( $wc_get_product ) {
    // Get current tag id(s)
    $current_tag_ids = $wc_get_product->get_tag_ids();

    // Product set tag id(s), multiple IDs can be added, separated by a comma
    $new_tag_ids = array( '4074' );
    $wc_get_product->set_tag_ids( array_unique( array_merge( $current_tag_ids, $new_tag_ids ), SORT_REGULAR ) );

    // Save
    $wc_get_product->save();
}

add_action( 'woocommerce_no_stock', 'action_woocommerce_no_stock', 10, 1 );

如有任何建议,我们将不胜感激!

php wordpress woocommerce product taxonomy-terms
1个回答
1
投票

假设“4074”是所需的产品标签术语 ID,只需在产品 foreach 循环中使用,就在

save()
方法之前:

$product->set_tag_ids( array_unique( array_merge( $product->get_tag_ids(), [4074]) ) );

应该可以。

© www.soinside.com 2019 - 2024. All rights reserved.