【问题标题】:WooCommerce minimum order fee for products in specific categoryWooCommerce 特定类别产品的最低订购费
【发布时间】:2019-10-07 11:19:04
【问题描述】:

我想在我的 woocommerce 商店中为特定金额(价值,而不是数量)以下的订单添加费用。

我正在使用 "WooCommerce dynamic minimum order amount based fee" 完美运行的答案代码。

现在我正在尝试更改此功能并将其仅应用于特定产品类别的项目。基本上,我必须将购物车中与特定类别匹配的所有产品的价值相加,然后用这个值来计算费用。

我怎样才能达到这个目标?

【问题讨论】:

    标签: php wordpress woocommerce cart fee


    【解决方案1】:

    以下重新访问的代码将处理特定定义的产品类别。您必须定义:

    • 您在第一个功能中的特定产品类别。
    • 第 2 和第 3 函数中的最小订单量。

    代码:

    // Get the cart items subtotal amount from specific product categories
    function get_specific_cart_items_total( $cart ) {
        $categories   = array('t-shirts'); // <=== Define your product categories
        $items_amount = 0; // Initializing
    
        // Loop through cart items
        foreach( $cart->get_cart() as $item ) {
            if( has_term( $categories, 'product_cat', $item['product_id'] ) ) {
                $items_amount += $item['line_subtotal'];
            }
        }
        return $items_amount;
    }
    
    // Add a custom fee (displaying a notice in cart page)
    add_action( 'woocommerce_cart_calculate_fees', 'add_custom_surcharge', 10, 1 );
    function add_custom_surcharge( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_cart_calculate_fees' ) >= 2 )
            return;
    
        $min_price     = 200; // The minimal cart amount
    
        $current_price = get_specific_cart_items_total( $cart );
        $custom_fee    = $min_price - $current_price;
    
        if ( $custom_fee > 0 ) {
            $cart->add_fee( __('Minimum Order Adjustment'), $custom_fee, true );
    
            // NOTICE ONLY IN CART PAGE
            if( is_cart() ){
                wc_print_notice( get_custom_fee_message( $min_price, $current_price ), 'error' );
            }
        }
    }
    
    // Displaying the notice on checkout page
    add_action( 'woocommerce_before_checkout_form', 'custom_surcharge_message', 10 );
    function custom_surcharge_message(  ) {
        $min_price     = 200; // The minimal cart amount
    
        $cart          = WC()->cart;
        $current_price = get_specific_cart_items_total( $cart );
        $custom_fee    = $min_price - $current_price;
    
        // NOTICE ONLY IN CHECKOUT PAGE
        if ( $custom_fee > 0 ) {
            wc_print_notice( get_custom_fee_message( $min_price, $current_price ), 'error' );
        }
    }
    
    // The fee notice message
    function get_custom_fee_message( $min_price, $current_price ) {
        return sprintf(
            __('We have a minimum %s per order from specific categories. As your current order is only %s, an additional fee will be applied.', 'woocommerce'),
            wc_price( $min_price ),
            wc_price( $current_price )
        );
    }
    

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


    选项:要同时处理父产品类别,请添加以下附加功能:

    // Custom conditional function that handle parent product categories too
    function has_product_categories( $categories, $product_id = 0 ) {
        $parent_term_ids = $categories_ids = array(); // Initializing
        $taxonomy        = 'product_cat';
        $product_id      = $product_id == 0 ? get_the_id() : $product_id;
    
        if( is_string( $categories ) ) {
            $categories = (array) $categories; // Convert string to array
        }
    
        // Convert categories term names and slugs to categories term ids
        foreach ( $categories as $category ){
            $result = (array) term_exists( $category, $taxonomy );
            if ( ! empty( $result ) ) {
                $categories_ids[] = reset($result);
            }
        }
    
        // Loop through the current product category terms to get only parent main category term
        foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
            if( $term->parent > 0 ){
                $parent_term_ids[] = $term->parent; // Set the parent product category
                $parent_term_ids[] = $term->term_id; // (and the child)
            } else {
                $parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
            }
        }
        return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
    }
    

    并在第一个函数中替换这一行:

    if( has_term( $categories, 'product_cat', $item['product_id'] ) ) {
    

    通过这个:

    if( has_product_categories( $categories, $item['product_id'] ) ) {
    

    【讨论】:

    • 您好,我测试了上面的代码,但发现购物车中的产品存在一些问题。基本上,额外费用也适用于不同类别的物品,特别是当我添加/删除产品时。更新购物车时,$custom_fee 变量似乎没有更新。
    • @MarcoBocola 抱歉,但我再次测试了我的代码并且运行良好……对于您的类别问题,我已经进行了更新(检查)……现在对于刷新问题,在您的情况下还有其他问题制造麻烦。它可以是您的其他自定义、您的主题自定义或插件(此代码经过测试,适用于具有店面主题的最后一个 WooCommerce 版本)。
    猜你喜欢
    • 2020-09-21
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    相关资源
    最近更新 更多