[英]AJAX cart update woocommerce
单击“更新购物车”按钮后如何通过AJAX更新购物车内容?
当我尝试使用$fragments
函数时,它总是返回true ...我尝试使用woocommerce挂钩,但字段未通过AJAX刷新。 我真的需要一点帮助,因为我被困住了。 我已经尝试了一切...我相信可以通过“ add_action”来完成。 救命? :)
add_action('woocommerce_checkout_after_customer_details','inputs_after_cart');
function inputs_after_cart($checkout) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$i = 1;
foreach($items as $item => $values) {
$_product = $values['data']->post;
$quantity = $values['quantity'];
$x = 1;
while ($x <= $quantity) {
echo '<div class="col-12 refresh-tickets"><h3>' . $_product->post_title . __(' - Bilet ' . $x . ' ') .'</h3>';
$namevar = 'name'.$x;
$emailvar = 'email'.$x;
woocommerce_form_field($namevar , array(
'type' => 'text',
'label' => __('Name'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array(''),
'clear' => true,
));
woocommerce_form_field($emailvar, array(
'type' => 'text',
'label' => __('E-mail'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array(''),
'clear' => true,
));
echo '</div>';
$x++;
}
$i++;
}
}
您可以编写脚本来侦听WooCommerce javascript触发事件,然后在触发时运行自己的代码。 这里是添加到购物车操作的javascript触发器。
$( document.body ).trigger( 'updated_cart_totals' );
使用此javascript捕获该触发器。
$( document.body ).on( 'updated_cart_totals', function(event){
//Run your own code here, use AJAX to call a PHP function to create new inputs
$.ajax({
url : 'http://yourwesbite.com/wp-admin/admin-ajax.php',
type : 'post',
data : {
action : 'the_function_that_creates_inputs'
},
success : function( data ) {
// data contains HTML inputs to display
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.