0

I use a code that adds a 20% discount when choosing "Local Pickup" in the cart and at checkout based on Do not give discounts for on sale products when selecting “local pickup” in WooCommerce

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

Tell me how to make sure that the local-pickup discount is not taken if the product is from certain categories? I must specify the category name manually, for example, "steak", "sauce" and "bread".

And how to make a notice to customers "Pickup discount does not apply to products from the" Category Name "category"?

I will be glad for your help!

Dmitry
  • 119
  • 1
  • 9
  • 38

2 Answers2

1

Try below code

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

Based my update discount will not apply Category 1, Test Category when shipping method is local_pickup and if any of those category products found from the cart list, it will show notification to customer on the success page.

Thank you.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Karthik Sekar
  • 625
  • 4
  • 7
  • Everything works just fine! Many thanks! Only there is a question. As I understand it, I can specify any number of categories here? – Dmitry Feb 02 '21 at 18:15
  • You can list down all the excluded categories in variable $arr_discount_excluded_category as an array – Karthik Sekar Feb 02 '21 at 18:30
0

As i can see your code, you have already used below function to get all product added in cart

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

now you just need to check that your product belongs to any of that category you want to check

i think this way you can figure out

wpdevloper_j
  • 330
  • 2
  • 12