【问题标题】:Do not give discounts on certain product categories when choosing “local pickup” in WooCommerce在 WooCommerce 中选择“本地取货”时,不要对某些产品类别给予折扣
【发布时间】:2021-02-02 15:49:33
【问题描述】:

我使用的代码在购物车中和结帐时根据Do not give discounts for on sale products when selecting “local pickup” in WooCommerce 选择“本地取货”时可享受 20% 的折扣

/**
* Discount for Local Pickup
*/
add_action( 'woocommerce_cart_calculate_fees', 'custom_discount_for_pickup_shipping_method', 10, 1 );
function custom_discount_for_pickup_shipping_method( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $percentage = 20; // Discount percentage

    $chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
    $chosen_shipping_method    = explode(':', $chosen_shipping_method_id)[0];

    // Only for Local pickup chosen shipping method
    if ( strpos( $chosen_shipping_method_id, 'local_pickup' ) !== false ) {

        // Set variable
        $new_subtotal = 0;

        // Loop though each cart items and set prices in an array
        foreach ( $cart->get_cart() as $cart_item ) {

            // Get product
            $product = wc_get_product( $cart_item['product_id'] );

            // Product has no discount
            if ( ! $product->is_on_sale() ) {
                // line_subtotal
                $line_subtotal = $cart_item['line_subtotal'];

                // Add to new subtotal
                $new_subtotal += $line_subtotal;
            }
        }

        // Calculate the discount
        $discount = $new_subtotal * $percentage / 100;

        // Add the discount
        $cart->add_fee( __('Discount') . ' (' . $percentage . '%)', -$discount );
    }
}

告诉我,如果产品属于某些类别,如何确保不享受本地提货折扣?我必须手动指定类别名称,例如“steak”、“sauce”和“bread”。

以及如何通知客户“提货折扣不适用于”Category Name“类别”的产品?

我会很高兴得到您的帮助!

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    试试下面的代码

    /**
    * Discount for Local Pickup
    */
    add_action( 'woocommerce_cart_calculate_fees', 'custom_discount_for_pickup_shipping_method', 10, 1 );
    function custom_discount_for_pickup_shipping_method( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        $percentage = 20; // Discount percentage
    
        $chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
        $chosen_shipping_method    = explode(':', $chosen_shipping_method_id)[0];
        
    
        // Only for Local pickup chosen shipping method
        if ( strpos( $chosen_shipping_method_id, 'local_pickup' ) !== false) {
    
            // Set variable
            $new_subtotal = 0;
            
            // Set discount excluded categories list
            $arr_discount_excluded_category = ['Category 1', 'Test Category'];
    
            // Set variable for matched excluded category from the cart list
            $arr_discount_excluded_category_matched = [];
    
            // Loop though each cart items and set prices in an array
            foreach ( $cart->get_cart() as $cart_item ) {
    
                // Get product
                $product = wc_get_product( $cart_item['product_id'] );
    
                // Get product category
                $category_list = wp_get_post_terms($cart_item['product_id'],'product_cat',array('fields'=>'names'));
                
                // Product has no discount
                $arr_discount_excluded_category_matched_by_item = array_intersect($category_list, $arr_discount_excluded_category);
                if ( ! $product->is_on_sale() && empty($arr_discount_excluded_category_matched_by_item)) {
                    // line_subtotal
                    $line_subtotal = $cart_item['line_subtotal'];
    
                    // Add to new subtotal
                    $new_subtotal += $line_subtotal;
                }
                else{
                    $arr_discount_excluded_category_matched = array_merge($arr_discount_excluded_category_matched, $arr_discount_excluded_category_matched_by_item);
                }
            }
            
            // Calculate the discount
            $discount = 0;
            if($new_subtotal > 0){
                $discount = $new_subtotal * $percentage / 100;
            }
            
            //Add notification
            if(!empty($arr_discount_excluded_category_matched)){
                $str_message = 'Pickup discount does not apply to products from the Category "' . implode('", "', array_unique($arr_discount_excluded_category_matched)) . '"';
                wc_add_notice($str_message, 'notice');
            }
    
            // Add the discount
            $cart->add_fee( __('Discount') . ' (' . $percentage . '%)', -$discount );
        }
    }
    

    基于我的更新折扣不适用于Category 1Test Category,当运输方式为local_pickup 时,如果从购物车列表中找到任何这些类别的产品,它将在成功页面上向客户显示通知。

    谢谢。

    【讨论】:

    • 一切正常!非常感谢!只有一个问题。据我了解,我可以在这里指定任意数量的类别?
    • 您可以将变量 $arr_discount_excluded_category 中的所有排除类别作为数组列出
    【解决方案2】:

    我可以看到您的代码,您已经使用以下函数将所有产品添加到购物车中

    /* 获取产品 */ $product = wc_get_product($cart_item['product_id']);

    现在您只需要检查您的产品是否属于您要检查的任何类别

    我认为这样你就可以弄清楚

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 2021-05-16
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      • 2021-02-19
      • 2018-07-03
      • 2015-03-14
      • 2022-11-21
      相关资源
      最近更新 更多