WordPress

filtry – Zastosuj kupon Woocommerce po spersonalizowanym rabacie w koszyku lub podczas meldowania się

  • 18 lipca, 2024
  • 3 min read
filtry – Zastosuj kupon Woocommerce po spersonalizowanym rabacie w koszyku lub podczas meldowania się


W moim motywie potomnym functions.php dodałem niestandardowy kod do obliczania dynamicznych rabatów na podstawie potrzeb klienta. Czego nie mogę zrobić, to gdy zastosowany jest kupon o stałej cenie (powiedzmy o stałej cenie w koszyku), jest on automatycznie dodawany PRZED niestandardowymi rabatami, podczas gdy ja chcę, aby był stosowany PO.

Przykład pożądanego wyniku:

Subtotal = 1000
10% discount (custom) = -100
coupon = -50
Total = 850

Co się dzieje zamiast tego?

Subtotal = 1000
coupon = -50
10% discount = -95
Total = 855

Oto mój niestandardowy kod rabatowy (działa w przypadku prostych produktów, które mają niestandardowe pole „is_student”, gdzie wyjściem jest „yes”

// Display custom field value in the cart
function display_student_option_in_cart($item_data, $cart_item) {
    $product = wc_get_product($cart_item['product_id']);

    if (isset($cart_item['is_student'])) {
        if ($product->is_on_sale()) {
            $item_data[] = array(
                'key'     => __('Student discount', 'woocommerce'),
                'value'   => __('Not applicable', 'woocommerce')
            );
        } else {
            $item_data[] = array(
                'key'     => __('Student discount', 'woocommerce'),
                'value'   => __('Yes', 'woocommerce')
            );
        }
    }
    return $item_data;
}
add_filter('woocommerce_get_item_data', 'display_student_option_in_cart', 10, 2);


//function to calculate discounts for students and adults (only if they book > 1 product)
function cart_discount_subsequent_variations_same_product() {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    global $woocommerce;

    $cart = WC()->cart->get_cart();
    $cart_array = array();

    $total_student_discount = 0; // Initialize total student discount
    $total_adult_discount = 0; // Initialize total adult discount
    $student_percentage = 0.10;
    $adult_percentage = 0.10;
    

    foreach ($cart as $cart_item_key => $values) {
        
        $product_id = $values['product_id'];
        $cart_lines_quantity = $values["quantity"];
        $cart_lines_total = $values["line_total"];
        $is_student = isset($values['is_student']) ? $values['is_student'] : false;
        // Retrieve the product object
        $product = wc_get_product($product_id);
        // Check if product is on sale
        $is_on_sale = $product && $product->is_on_sale();

        //case 1: is student && is not on sale
        if ($is_student && !$is_on_sale) {
            $student_discount = $student_percentage * $cart_lines_total;
            $total_student_discount += $student_discount; // Accumulate the student discount for each item
        }
         //case 2: is student && is on sale 
       /* else if($is_student && $is_on_sale) {
        }*/
        //case 3: is not student 
        else if(!$is_student && !$is_on_sale) {
            $cart_array[] = array('product_id' => $product_id, 'quantity' => $cart_lines_quantity, 'total' => $cart_lines_total);
        }
        //case 4: is not student && is on sale
        else if(!$is_student && $is_on_sale) {
            $cart_array[] = array('product_id' => $product_id, 'quantity' => $cart_lines_quantity, 'total' => $cart_lines_total);
        }              
    }
    // Apply student discount
    if ($total_student_discount > 0) {
        $student_discount_neg = -($total_student_discount);
        $discount_text = __('10% student discount', 'woocommerce');
        WC()->cart->add_fee($discount_text, $student_discount_neg, false);
    }

    // Apply adult discount for subsequent cruises
    if (count($cart_array) > 1) {
        usort($cart_array, function($a, $b) {
            return $a['product_id'] <=> $b['product_id'];
        });

        // Calculate discount for subsequent adult bookings
        for ($i = 1; $i < count($cart_array); $i++) {
            $total_adult_discount += ($adult_percentage * $cart_array[$i]['total']);
        }
        if ($total_adult_discount > 0) {
            $adult_discount_neg = -($total_adult_discount);
            $discount_text = __('10% discount on subsequent week(s)', 'woocommerce');
            WC()->cart->add_fee($discount_text, $adult_discount_neg, false);
        }
    }
}
add_action('woocommerce_cart_calculate_fees', 'cart_discount_subsequent_variations_same_product', 10);


Źródło

Warto przeczytać!  Przenieś swoje domeny do Huba