在 WooCommerce 中以编程方式应用或删除优惠券后更新总计

Update totals after applying or removing a coupon programmatically In WooCommerce

我创建了一个复选框(它看起来不再像一个复选框了)apply/remove 更改时的优惠券。这很好用。但是apply上总不更新,页面要刷新。我用其他函数的一些剪切和粘贴构建了这个函数,它曾经是一个无线电领域,它可能不是最好的做法。优惠券广告优惠 500 瑞典克朗。

但是使用优惠券后如何重新计算总数?

如你所见,我试过WC()->cart->calculate_totals();.

这是网站和结帐:https://www.klubbtryck.se/nif/kassa/

这是我的代码:

    // Add a custom checkout field
    add_action( 'woocommerce_review_order_after_shipping', 'checkout_shipping_form_delivery_addition_nifny', 20 );

function checkout_shipping_form_delivery_addition_nifny(){
    $domain = 'wocommerce';

    if (  WC()->session->get( 'chosen_shipping_methods' )[0] == 'local_pickup:3' ) :

        echo '<tr class="delivery-radio"><th>' . __('Gift Card', $domain) . '</th><td>';

        $chosen = WC()->session->get('chosen_delivery');
        $chosen = empty($chosen) ? WC()->checkout->get_value('delivery') : $chosen;
        $chosen = empty($chosen) ? 0 : $chosen;
        if( $chosen == 1){ $chosen = true; } else { $chosen = false; }

        // Add a custom checkbox field
        woocommerce_form_field( 'radio_delivery', array(
            'type' => 'checkbox',
            'label' => '<label for="radio_delivery" class="checkbox-label"><span class="presentkortbesk">I have a gift card</span><span class="priset">-500kr</span></label>',
            'class' => array( 'form-row-wide' ),
            'required' => false,
            //'default' => false,
        ), $chosen );

        echo '</td></tr>';

    endif;
}

// jQuery - Ajax script
add_action( 'wp_footer', 'checkout_delivery_script_nifny' );
function checkout_delivery_script_nifny() {
    // Only checkout page
    if ( ! is_checkout() ) return;
    ?>
    <script type="text/javascript">
    jQuery( function($){
        if (typeof wc_checkout_params === 'undefined')
            return false;

        $('form.checkout').on('change', 'input[name=radio_delivery]', function(e){
            e.preventDefault();
            var d = $(this).prop('checked') === true ? 1 : 0;
            //var d = $(this).val();
            //alert('value: '+d);
            $.ajax({
                type: 'POST',
                url: wc_checkout_params.ajax_url,
                data: {
                    'action': 'delivery',
                    'delivery': d,
                },
                success: function (result) {
                    $('body').trigger('update_checkout');
                    //console.log(result); // just for testing | TO BE REMOVED
                },
                error: function(error){
                    //console.log(error); // just for testing | TO BE REMOVED
                }
            });
        });
    });
    </script>
    <?php

}

// Get Ajax request and saving to WC session
add_action( 'wp_ajax_delivery', 'wc_get_delivery_ajax_data_nifny' );
add_action( 'wp_ajax_nopriv_delivery', 'wc_get_delivery_ajax_data_nifny' );
function wc_get_delivery_ajax_data_nifny() {
    if ( isset($_POST['delivery']) ){
        WC()->session->set('chosen_delivery', sanitize_key( $_POST['delivery'] ) );
        echo json_encode( $delivery ); // Return the value to jQuery
    }
    die();
}

// Add a custom dynamic delivery fee
add_action( 'woocommerce_cart_calculate_fees', 'add_packaging_fee_nifny', 20, 1 );
function add_packaging_fee_nifny( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Only for targeted shipping method
    if (  WC()->session->get( 'chosen_shipping_methods' )[0] != 'local_pickup:3' )
        return;

    if( WC()->session->get( 'chosen_delivery' ) == 1 ){
        if (!in_array('nynashamn2020', WC()->cart->get_applied_coupons())) {
                WC()->cart->apply_coupon('card2020');
                //WC()->cart->calculate_totals();
            }
    } else {
        if (in_array('nynashamn2020', WC()->cart->get_applied_coupons())) {
            WC()->cart->remove_coupon('card2020');
        }
    }
}

您应该用类似的 woocommerce_before_calculate_totals 更合适的钩子替换仅用于费用的 woocommerce_cart_calculate_fees 钩子,如下所示:

// Add a custom dynamic delivery fee
add_action( 'woocommerce_before_calculate_totals', 'add_packaging_fee_nifny' );
function add_packaging_fee_nifny( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Only for targeted shipping method
    if (  WC()->session->get( 'chosen_shipping_methods' )[0] != 'local_pickup:3' )
        return;

    if( WC()->session->get( 'chosen_delivery' ) == 1 ){
        if (!in_array('nynashamn2020', WC()->cart->get_applied_coupons())) {
                WC()->cart->apply_coupon('card2020');
                //WC()->cart->calculate_totals();
            }
    } else {
        if (in_array('nynashamn2020', WC()->cart->get_applied_coupons())) {
            WC()->cart->remove_coupon('card2020');
        }
    }
}

代码进入活动子主题(或活动主题)的 functions.php 文件。它应该更好用。