Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
799 views
in Technique[技术] by (71.8m points)

php - Allow backorders and notify customer for parent product categories in Woocommerce

With woocommerce, I used some code based on my previous thread:
Allow backorders and notify customer for specific product categories in Woocommerce

add_filter( 'woocommerce_product_is_in_stock', 'filter_product_is_in_stock', 10, 2 );
function filter_product_is_in_stock( $is_in_stock, $product ){
    // Here set the products categories in the array (can be terms ids, slugs or names)
    $categories = array("clothing");

    if( has_term( $categories, 'product_cat', $product->get_id() ) ){
        $is_in_stock = true;
    }
    return $is_in_stock;
}

add_filter( 'woocommerce_product_backorders_allowed', 'filter_products_backorders_allowed', 10, 3 );
function filter_products_backorders_allowed( $backorder_allowed, $product_id, $product ){
    // Here set the products categories in the array (can be terms ids, slugs or names)
    $categories = array("clothing");

    if( has_term( $categories, 'product_cat', $product_id ) ){
        $backorder_allowed = true;
    }
    return $backorder_allowed;
}

add_filter( 'woocommerce_product_backorders_require_notification', 'filter_product_backorders_require_notification', 10, 2 );
function filter_product_backorders_require_notification( $notify, $product ){
    // Here set the products categories in the array (can be terms ids, slugs or names)
    $categories = array("clothing");

    if( has_term( $categories, 'product_cat', $product->get_id() ) ){
        $notify = true;
    }
    return $notify;
}

But it seems to be not working when I used the root product categories.

How can I get the root product categories in the if statements of the code?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update 2 - 2018 Nov 17th (removed a bug and cleaned a bit)

To make it work for the parent categories, we add a custom conditional function that checks for parent product categories (where you will define your targeted parent product categories):

// Custom conditional function that checks for parent product categories
function has_parent_terms( $product_id ) {
    // HERE define the parent products categories SLUGS in the array
    $categories = array("clothing", "posters");

    $parent_term_ids = $categories_ids = array(); // Initializing

    // Convert categories term slugs to categories term ids
    foreach ( $categories as $category ){
        $categories_ids[] = get_term_by('slug', $category, 'product_cat')->term_id;
    }

    $terms = get_the_terms( $product_id, 'product_cat' );

    if( ! $terms ) return false; // Check that is not empty

    // Loop through the current product category terms to get only parent main category term
    foreach( $terms as $term ){
        if( $term->parent > 0 ){
            $parent_term_ids[] = $term->parent; // Set the parent product category
        } else {
            $parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
        }
    }
    return array_intersect( $categories_ids, $parent_term_ids ) ? true : false;
}

add_filter( 'woocommerce_product_is_in_stock', 'filter_product_is_in_stock', 10, 2 );
function filter_product_is_in_stock( $is_in_stock, $product ){
    // For product variations
    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    if( has_parent_terms( $product->get_id() ) ){
        $is_in_stock = true;
    }
    return $is_in_stock;
}

add_filter( 'woocommerce_product_backorders_allowed', 'filter_products_backorders_allowed', 10, 3 );
function filter_products_backorders_allowed( $backorder_allowed, $product_id, $product ){
    // For product variations
    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product_id;

    if( has_parent_terms( $product_id ) ){
        $backorder_allowed = true;
    }
    return $backorder_allowed;
}

add_filter( 'woocommerce_product_backorders_require_notification', 'filter_product_backorders_require_notification', 10, 2 );
function filter_product_backorders_require_notification( $notify, $product ){
    // For product variations
    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    if( has_parent_terms( $product->get_id() ) ){
        $notify = true;
    }
    return $notify;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Related: Allow backorders and notify customer for specific product categories in Woocommerce


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...