In our shop, we want to offer our customers the option to pick up products. For products of type X, we want to give our customers a discount of 6€ on each of those products if they choose the pick-up option. To make it even more, complicated if they don't choose the pick-up option. The delivery fee which is 5,95 should not be applied to products of type X.
I tried to work with this piece of code but could not figure it out. It would be best if we could identify these products via their id and not their category
/**
* 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;
$discount_amount = 6; // Discount
$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 = ['merch', 'rum'];
// 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 - $discount_amount;
}
//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') . ' (' . $discount_amount . '€)', -$discount );
}
}