Reputation: 1
How can I update cart content via AJAX after 'update cart' button click?
When I'm trying to use $fragments
function, it always returns true... I've tryied use woocommerce hooks but fields was not refreshing via AJAX. I realy need a little help, because I'm stuck. I've tried everything... I belive that can be done via "add_action". Help? :)
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++;
}
}
Upvotes: 0
Views: 12821
Reputation: 4243
You can write scripts that listen to WooCommerce javascript trigger events and then run your own code when it's fired. Here the javascript trigger for the add to cart action.
$( document.body ).trigger( 'updated_cart_totals' );
Hook into that trigger using this 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
}
});
});
Upvotes: 3