WooCommerce - 根据自定义字段添加费用

时间:2022-10-25 11:06:43

I am trying to add a fee to checkout based on whether or not a custom checkbox I've added it checked. I am really close to getting this to work, but I'm having issues getting the post values to determine if the fee should be applied or not.

我试图根据我是否添加了自定义复选框来检查结帐费用。我真的很接近让这个工作,但我有问题获得邮政价值,以确定是否应该收取费用。

Here is the code I have for my custom field:

这是我的自定义字段的代码:

add_action( 'woocommerce_after_checkout_billing_form', 'my_custom_fields' );
function my_custom_fields( $checkout ) {


  echo '<div id="message_fields"><h3>' . __('Add a Message') . '</h3>';
    woocommerce_form_field( 'add_gift_message', array(
        'type'          => 'checkbox',
        'class'         => array('gift_message form-row-wide'),

        'label'         => __('5x7 Enclosed Personal Message - $4'),
        'placeholder'   => __(''),
        ), $checkout->get_value( 'add_gift_message' ));

    woocommerce_form_field( 'gift_message', array(
        'type'          => 'textarea',
        'class'         => array('gift_message_text form-row-wide'),

        'label'         => false,
        'placeholder'   => __('Your message'),
        ), $checkout->get_value( 'gift_message' )); 
    echo '</div>';
}

This works, and the fields show up perfectly.

这是有效的,并且字段完美地显示出来。

At the bottom of form-checkout.php in my theme, I have added this javascript to update the cart total when the field it checked:

在我的主题中的form-checkout.php的底部,我添加了这个javascript,以便在检查字段时更新购物车总数:

<script type="text/javascript">
jQuery( document ).ready(function( $ ) {

$('#add_gift_message').click(function(){
    if ( $(this).is(':checked') ) { 
        $('#gift_message_field').show();
        var gift_message= true;
    } else { 
        $('#gift_message_field').hide();
        var gift_message = false;
    }
    $.ajax({
        type:       'POST',
        url:        wc_checkout_params.ajax_url,
        data:       $( 'form.checkout' ).serialize(),
        success:    function( response ) {
                if ( response ) {
                        var order_output = $(response);
                        $( '#order_review' ).html( $.trim( order_output ) );
                        $('body').trigger('update_checkout');
                    }
        },
        error: function(code){
        },
        dataType: 'html'
    });
});
});
</script>

This updates the order total and runs the following code (this is where the problem is):

这会更新订单总数并运行以下代码(这是问题所在):

add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

function woo_add_cart_fee( $data ){
    global $woocommerce;

    if ( isset($_POST['add_gift_message']) )
        $woocommerce->cart->add_fee( 'Personal Gift Message', '4.00', true, 'standard' );

}

I know this part is being called because if I take out if ( isset($_POST['add_gift_message']) ), it adds the fee. What I'm having trouble with is determining if the field has been checked or not - I can't seem to get the POST values inside of woo_add_cart_fee no matter what I do.

我知道这个部分正在被调用,因为如果我取出if(isset($ _ POST ['add_gift_message'])),它会增加费用。我遇到的问题是确定该字段是否已被检查 - 无论我做什么,我都无法在woo_add_cart_fee中获取POST值。

Does anyone know how to get this to work? Thank you!!

有谁知道如何使这个工作?谢谢!!

1 个解决方案

#1


-1  

The reason why you unable to get value from $_POST['add_gift_message'] is because update_checkout is an ajax event and it serialise form data into $_POST['post_data']. So, in order to cater for getting the value before (ajax) and during(non-ajax) user check out, you can get that by using below code:

您无法从$ _POST ['add_gift_message']获取值的原因是因为update_checkout是一个ajax事件,它将表单数据序列化为$ _POST ['post_data']。因此,为了满足在(ajax)和(非ajax)用户签出之前获取值,您可以使用以下代码来获取:

if ( isset( $_POST['post_data'] ) ) {
    parse_str( $_POST['post_data'], $post_data );
} else {
    $post_data = $_POST; // fallback for final checkout (non-ajax)
}

if ( isset( $post_data['add_gift_message'] ) ) {

    $woocommerce->cart->add_fee( 'Personal Gift Message', '4.00', true, 'standard' );
}

for reference, can see this post

供参考,可以看到这篇文章

#1


-1  

The reason why you unable to get value from $_POST['add_gift_message'] is because update_checkout is an ajax event and it serialise form data into $_POST['post_data']. So, in order to cater for getting the value before (ajax) and during(non-ajax) user check out, you can get that by using below code:

您无法从$ _POST ['add_gift_message']获取值的原因是因为update_checkout是一个ajax事件,它将表单数据序列化为$ _POST ['post_data']。因此,为了满足在(ajax)和(非ajax)用户签出之前获取值,您可以使用以下代码来获取:

if ( isset( $_POST['post_data'] ) ) {
    parse_str( $_POST['post_data'], $post_data );
} else {
    $post_data = $_POST; // fallback for final checkout (non-ajax)
}

if ( isset( $post_data['add_gift_message'] ) ) {

    $woocommerce->cart->add_fee( 'Personal Gift Message', '4.00', true, 'standard' );
}

for reference, can see this post

供参考,可以看到这篇文章