Фиксированная скидка Local Pickup по продукту для определенных продуктов в WooCommerce

В нашем магазине мы хотим предложить нашим покупателям возможность забрать товар. Для продуктов типа X мы хотим предоставить нашим клиентам скидку в размере 6 евро на каждый из этих продуктов, если они выберут вариант самовывоза. Чтобы сделать это еще более сложным, если они не выберут вариант пикапа. Плата за доставку в размере 5,95 не должна применяться к продуктам типа X.

Я пытался работать с этим фрагментом кода, но не мог понять. Было бы лучше, если бы мы могли идентифицировать эти продукты по их идентификатору, а не по категории.

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

person Lucas    schedule 13.02.2021    source источник
comment
высоко там, некоторые отзывы на ответ ниже высоко ценятся, пожалуйста.   -  person LoicTheAztec    schedule 16.02.2021


Ответы (1)


Следующее добавит фиксированную скидку по продукту только к определенным определенным продуктам, если выбран способ доставки «Местный самовывоз».

Определите в коде ниже желаемые идентификаторы продуктов и сумму скидки по продукту:

add_action( 'woocommerce_cart_calculate_fees', 'fixed_discount_by_product_for_pickup_shipping_method', 10, 1 );
function fixed_discount_by_product_for_pickup_shipping_method( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $discount_by_product  = 6; // Here set the discount amount by product
    $targeted_product_ids = array(37,40); // Here set your excluded product ids

    $chosen_shipping_id = WC()->session->get('chosen_shipping_methods')[0];
    $total_discount     = 0; // Initializing

    // Only for "Local pickup" shipping method
    if ( strpos( $chosen_shipping_id, 'local_pickup' ) !== false ) {
        // Loop though each cart items and set prices in an array
        foreach ( $cart->get_cart() as $item ) {
            // Get matching targeted product ids from the current cart item
            $matched_product_ids = array_intersect( array($item['product_id'], $item['variation_id']), $targeted_product_ids);  
            
            // If product matches with targeted product Ids
            if ( ! empty($matched_product_ids) ) {
                // Add the discount by product
                $total_discount += $discount_by_product; 
            }
        }
        if ( $total_discount > 0 ) {
            $cart->add_fee( __('Local pickup discount'), -$total_discount ); // Add the discount
        }
    }
}

Код находится в файле functions.php активной дочерней темы (или активной темы). Проверено и работает.


Чтобы обрабатывать количество продукта при расчете скидки, замените:

$total_discount += $discount_by_product;

с:

$total_discount += $discount_by_product * $item['quantity'];
person LoicTheAztec    schedule 13.02.2021