代码之家  ›  专栏  ›  技术社区  ›  JapeNZ

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

  •  1
  • JapeNZ  · 技术社区  · 6 月前

    我有一个自定义的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 );
    

    任何建议都将不胜感激!

    2 回复  |  直到 6 月前
        1
  •  1
  •   LoicTheAztec    6 月前

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

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

    它应该工作。