【问题标题】:Allow backorders and notify customer for parent product categories in Woocommerce允许延期交货并通知客户在 Woocommerce 中的父产品类别
【发布时间】:2019-03-05 00:00:57
【问题描述】:

对于 woocommerce,我使用了一些基于我之前线程的代码:
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;
}

但是当我使用根产品类别时,它似乎不起作用。

如何在代码的if语句中获取根产品类别?

【问题讨论】:

    标签: php wordpress woocommerce custom-taxonomy stock


    【解决方案1】:

    更新 2 - 2018 年 11 月 17 日 (删除了一个错误并进行了一些清理)

    为了使其适用于父类别,我们添加了一个自定义条件函数来检查父产品类别(您将在其中定义目标父产品类别)

    // 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;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

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

    【讨论】:

    • 如何在数组中添加多个类别先生?它完美地工作
    • @ErwinManalang 只需在第一个数组(第一个函数)中添加任意数量,在其中定义父类别 SLUGS,例如 $categories = array("clothing", "posters");...
    • 非常感谢您解决了我的问题。可以将类别的数组留空吗?像这样? add_filter('woocommerce_product_is_in_stock', 'filter_product_is_in_stock', 10, 2); function filter_product_is_in_stock( $is_in_stock, $product ){ // 这里设置数组中的产品类别(可以是terms ids, slugs or names) $categories = array(""); if( has_parent_term( $product->get_id() ) ){ $is_in_stock = true; } 返回 $is_in_stock; } 我只想编辑父类
    • @ErwinManalang 如果将其留空,则不需要代码,因为什么都不会发生……
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 2022-07-11
    • 2021-10-18
    • 2020-07-31
    • 1970-01-01
    • 2018-07-03
    • 2019-03-17
    相关资源
    最近更新 更多