【问题标题】:Adding Variable products to the global allow backorder command将变量产品添加到全局允许延期交货命令
【发布时间】:2019-03-17 05:31:40
【问题描述】:

是否可以将可变产品添加到延期交货命令中?我以前使用过这个Allow backorders and notify customer for parent product categories in Woocommerce,它工作正常,但代码似乎不适用于可变产品。请帮忙谢谢

【问题讨论】:

    标签: php wordpress woocommerce custom-taxonomy stock


    【解决方案1】:

    要处理产品变化,请尝试以下方法:

    // Custom conditional function that checks for parent product categories
    function has_parent_term( $product_id ) {
        // HERE define the parent products categories SLUGS in the array
        $categories = array("clothing", "posters"); //  <===  <===  <=== HERE YOUR CATEGORIES SLUGS
    
        $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;
        }
    
        // Loop through the current product category terms to get only parent main category term
        foreach( get_the_terms( $product_id, 'product_cat' ) 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 ){
        // Here set the products categories in the array (can be terms ids, slugs or names)
        $categories = array("clothing");
    
        // For product variations
        $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
    
    
        if( has_parent_term( $product_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");
    
        // For product variations
        $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product_id;
    
        if( has_parent_term( $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");
    
        // For product variations
        $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
    
        if( has_parent_term( $product_id ) ){
            $notify = true;
        }
        return $notify;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。

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

    【讨论】:

    • 久经考验。谢谢先生
    猜你喜欢
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多