cari

Rumah  >  Soal Jawab  >  teks badan

Elakkan menggunakan kupon apabila memilih Pickup Tempatan sebagai pilihan penghantaran anda

Saya menggunakan kod untuk menambah diskaun dalam troli dan apabila memilih "Pengambilan Tempatan" semasa membuat pembayaran.

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

Saya juga mencipta beberapa kupon. Bagaimanakah saya boleh menukar kod ini untuk mengecualikan semua kupon daripada pengiraan apabila saya memilih "Pilihan Tempatan"?

Bagaimana untuk memaparkan pemberitahuan bahawa kupon tidak berfungsi apabila "Pilihan Tempatan" dipilih?

Saya gembira atas bantuan anda!

P粉930534280P粉930534280225 hari yang lalu480

membalas semua(1)saya akan balas

  • P粉517475670

    P粉5174756702024-04-03 15:36:26

    // Add Percentage Discount to Local Pickup in Woocommerce
    function local_pickup_discount($cart) {
    
        $applied_coupons = WC()->cart->get_applied_coupons();
        $chosen_methods = WC()->session->get('chosen_shipping_methods');
        $chosen_shipping_no_ajax = $chosen_methods[0];
        if (empty($applied_coupons)) {
    
            if (0 === strpos($chosen_shipping_no_ajax, 'local_pickup')) {
                $discount = $cart->subtotal * 0.15; // Set your percentage. This here gives 15% discount
                $cart->add_fee(__('Discount added', 'yourtext-domain'), -$discount); // Change the text if needed
            }
        }
    
        if (0 === strpos($chosen_shipping_no_ajax, 'local_pickup') && !empty($applied_coupons)) {
            foreach ($applied_coupons as $key => $coupon) {
                WC()->cart->remove_coupon($coupon);
            }
            wc_add_notice(__('Coupon will not work for local pickup'), 'error');
        }
    }
    
    add_action('woocommerce_cart_calculate_fees', 'local_pickup_discount');

    Menggunakan WooCommerce 6.4 Ujian adalah perkara biasa

    balas
    0
  • Batalbalas