【问题标题】:Allow backorders and notify customer for specific product categories in Woocommerce在 Woocommerce 中允许延期交货并通知客户特定产品类别
【发布时间】:2018-09-23 01:34:52
【问题描述】:

在 woocommerce 中,我正在尝试在 functions.php 中添加一些代码,以允许特定产品类别的延期交货。但是代码不起作用。

如何在 Woocommerce 中允许延期交货并通知客户特定产品类别?

【问题讨论】:

    标签: php wordpress woocommerce product stock


    【解决方案1】:

    更新

    尝试以下操作(您将在数组中为每个函数设置产品类别)

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

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

    对于父产品类别:
    Allow backorders and notify customer for parent product categories in Woocommerce

    【讨论】:

    • 我已经尝试了function.php中的代码,但是出现了一些错误,先生。致命错误:无法在 /home1/digitsor/public_html/wp-content/themes/dokan/functions 中重新声明 filter_products_backorders_allowed()(之前在 /home1/digitsor/public_html/wp-content/themes/dokan/functions.php:369 中声明) .php 在第 388 行
    • 谢谢楼主的回复,我试过了。我将这两个类别放在允许延期交货和通知中,但没有任何反应。
    • @ErwinManalang 我已经添加了更多代码,经过测试,现在它可以完美运行......如果它适合你,你可以接受答案。谢谢。
    • 它现在可以工作了。先生,我有最后一个问题,代码似乎只适用于子类别。如何在数组中添加根或父类别并乘以类别?抱歉新手问题。
    • 先生,请帮助有关根类别以使用代码。谢谢先生
    猜你喜欢
    • 2019-03-05
    • 1970-01-01
    • 2021-10-18
    • 2018-07-03
    • 2022-07-11
    • 2019-05-01
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    相关资源
    最近更新 更多