In Woocommerce, I have added two custom radio buttons on the checkout page and on click, I called an ajax function to add a delivery fee.
Here is my code:
$(document).on('change','#shipping_method_0_local_pickup5',function(e) {
$('.woocommerce-shipping-fields').css({
'display': 'none'
});
$("#deli").css("display","block");
var selected = $("input[type='radio'][name='post-del']:checked");
var selectedVal = selected.val();
var pickurl= "<?php echo admin_url('admin-ajax.php');?>?action=delivery";
$.ajax({
url: pickurl,
type: "POST",
data:{
input:selectedVal,
},
success: function(responseText)
{
jQuery(".order-total .woocommerce-Price-amount").html(responseText);
//$(".discount_code").css("display","block");
}
});
});
Run Code Online (Sandbox Code Playgroud)
when the radio button click I want to add addition price $2 on …
所以我正在开发一个用于woocommerce的插件,我已经添加了一个包装选择,无论是在塑料袋中还是在卡通盒中,每个都有不同的成本.
用户选择其中一个选项我需要WordPress刷新价格并通过使用以下方式添加正确的费用来更新成本:
WC_Cart $cart->add_fee( 'Emballagegebyr', intval($fees));
Run Code Online (Sandbox Code Playgroud)
加入WC_Cart费用并更新价格的最佳方法是什么?代码应该怎么样?
是否可以使用$ _GET和$ _POST来获取值,甚至更好有没有办法使用AJAX更新价格而不刷新页面?
目前,我正在使用$ _GET通过以下代码从浏览器获取数据
function at87_add_custom_fees( WC_Cart $cart ){
$fees = 3; // fee amount
$fees = isset($_GET['test']) ? $_GET['test'] : 3;
$cart->add_fee( 'Emballagegebyr', intval($fees));
}
Run Code Online (Sandbox Code Playgroud)
然后我的计划可能是添加如下的Javascript代码,然后使用单选按钮刷新页面并传递选定的选项.
add_action( 'wp_footer', 'woocommerce_add_gift_box' );
function woocommerce_add_gift_box() {
if (is_checkout()) {
?>
<script type="text/javascript">
jQuery( document ).ready(function( $ ) {
// $('#add_gift_box').click(function(){
// jQuery('body').trigger('update_checkout');
// });
$("#pakpose1 input:radio").change(function(){
// Do something interesting here
alert("test");
});
});
</script>
<?php
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道但如果这是这样做的最聪明的方式,以及或有另一种方式,这可能是更好的,什么安全影响它可能有,可能也有一些挂钩,可以让做同样的工作.
顺便说一句:为了获得结账页面中的单选按钮,我以这样的方式制作了插件,它覆盖了woocommerce review-order.php并在该模板中添加了单选按钮,如下所示:
<tr class="packing-selections"> …Run Code Online (Sandbox Code Playgroud) 我通过更改模板在运输方式部分添加了两个自定义输入字段,/genesis-sample/woocommerce/checkout/review-order.php
我还设法使它们有条件地需要。只有当特定的单选按钮被选中时,输入字段才会出现并成为必需的。我正在使用 jQuery 代码使字段出现和消失。所有这些都运行良好。代码在这里:
if ( isset($_POST['shipping_method_0_legacy_local_pickup']) && $_POST['shipping_method_0_legacy_local_pickup'] == 1 ) {
$cheq = 'true';
}
else {
$cheq = 'false';
}
woocommerce_form_field( 'fieldtester1' , array(
'type' => 'text',
'class' => array('wccs-field-class wccs-form-row-wide blahblah1'),
'label' => 'Enter Your Carrier Name',
'required' => $cheq,
'placeholder' => 'Carrier Name',
), $checkout->get_value( 'fieldtester1' ));
woocommerce_form_field( 'fieldtester2' , array(
'type' => 'text',
'class' => array('wccs-field-class wccs-form-row-wide blahblah2'),
'label' => 'Enter Your Carrier Account #',
'required' => $cheq,
'placeholder' => 'Carrier Number',
), …Run Code Online (Sandbox Code Playgroud)