• Resolved nyenius

    (@nyenius)


    Hi, I have ready stock and backorder items in my store.

    What I need to achieve is to group products by their availability (stock status). So the items with 0 (zero) stock should go to “Preorder” category, and the available items should go to “Ready Stock” category.

    I can’t find any solution to this need. So i try to figure out how to do that, I think ‘auto-updating’ the product category should solve the problem. I try to write some pseudo code.

    add_action('woocommerce_before_single_product', 'check_and_update_category');
    
    function check_and_update_category() {
           // get the stock status of current product
           // if stock is empty, AND one of the item category is 'Ready Stock'
           // Change the product category to "Preorder"
    }

    I’ve spent hours trying to write the real code, but still I can’t find the functions to update the product’s category nor knowing where the relation is stored in the database.

    So, I need some help to write the real code, or any solution to solve my problem above.

    Thanks in advance

    https://www.ads-software.com/extend/plugins/woocommerce/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter nyenius

    (@nyenius)

    This what i’ve done so far, and it works.

    add_action('woocommerce_before_single_product', 'your_function_name');
    
    function your_function_name() {
    
    $postid = get_the_ID();
    $post_custom = get_post_custom($postid);
    $current_stock = get_post_meta($postid, '_stock', 1);
    
    if($current_stock < 1 ){
    // delete all product_cat related to this product
    wp_set_object_terms( $postid, NULL, 'product_cat' );
    // Stock is empty, so we set to "preorder" category (mine is 15)
    wp_set_object_terms( $postid, array(15), 'product_cat' );
    
    }else{
    // // delete all product_cat related to this product
    wp_set_object_terms( $postid, NULL, 'product_cat' );
    // Stock is empty, so we set to "Ready Stock" category (mine is 14)
    wp_set_object_terms( $postid, array(14), 'product_cat' );
    }

    And for specific product category, I use tags.

    That works great thank you very much!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Updating Product Category based on stock status in single product view’ is closed to new replies.