phpwordpresswoocommerceproducthook-woocommerce

Automatically change product tag on stock change in WooCommerce


I have a WooCommerce store, and every day I check my email that lists all products that are low in stock.

So if I see one of these products, I log in, find the product, and change the tag to 'Last Chance' which alerts my users that the item is almost out of stock, so you should buy it before it's gone.

This is fine, but it's a redundant task that I'm hoping can be automated.

Can I create a webhook that automatically applies this tag to any product that reaches low stock quantity in the inventory? It seems like it should be a native function of WooCommerce, but I don't see anywhere to do that.

Pseudocode:

IF PRODUCT QUANTITY = 'low'
    ADD TAG 'Last Chance' TO PRODUCT

Solution

  • The available hooks after stock change events, triggers emails and adds order notes are located in

    https://github.com/woocommerce/woocommerce/blob/master/includes/wc-stock-functions.php

    Where we find the function wc_trigger_stock_change_notifications() which contains some action hooks


    Available hooks:

    // No stock
    function action_woocommerce_no_stock( $wc_get_product ) {
        // make action magic happen here... 
    }
    add_action( 'woocommerce_no_stock', 'action_woocommerce_no_stock', 10, 1 );
    
    // Low stock
    function action_woocommerce_low_stock( $wc_get_product ) { 
        // make action magic happen here... 
    }
    add_action( 'woocommerce_low_stock', 'action_woocommerce_low_stock', 10, 1 );
    
    // On backorder
    function action_woocommerce_product_on_backorder( $array ) { 
        // make action magic happen here... 
    }
    add_action( 'woocommerce_product_on_backorder', 'action_woocommerce_product_on_backorder', 10, 1 );
    


    So for what you want you can use woocommerce_low_stock and CRUD Objects in 3.0

    function action_woocommerce_low_stock( $wc_get_product ) {    
        // Product set tag id(s), multiple IDs can be added, separated by a comma
        $new_tag_ids = array( 'YOUR TAG ID', 'ANOTHER TAG ID' );
        $wc_get_product->set_tag_ids( $new_tag_ids );
    
        // OPTIONAL: Set category ids
        //$wc_get_product->set_category_ids( array( 39, 2 ) );
    
        // Save
        $wc_get_product->save();
    }
    add_action( 'woocommerce_low_stock', 'action_woocommerce_low_stock', 10, 1 );
    

    Note) If you don't just want to add new tags to the product, but also keep the existing ones.

    Replace:

    // Product set tag id(s), multiple IDs can be added, separated by a comma
    $new_tag_ids = array( 'YOUR TAG ID', 'ANOTHER TAG ID' );
    $wc_get_product->set_tag_ids( $new_tag_ids );
    

    With:

    // 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( 'YOUR TAG ID', 'ANOTHER TAG ID' );
    $wc_get_product->set_tag_ids( array_unique( array_merge( $current_tag_ids, $new_tag_ids ), SORT_REGULAR ) );