Skip to content

Instantly share code, notes, and snippets.

@guytzhak
Created June 8, 2020 20:23
Show Gist options
  • Save guytzhak/44717fb4ada76314dea1f3dd1f6957e1 to your computer and use it in GitHub Desktop.
Save guytzhak/44717fb4ada76314dea1f3dd1f6957e1 to your computer and use it in GitHub Desktop.
Woocommerce Ajax add to cart + Update the fragments needed
$('body').on('submit', '.product form', function (e) {
e.preventDefault();
var productVariationID = $(this).find('.variations').length > 0 ? $(this).find('.variation_id').val() : null,
productID = $(this).find('[name="add-to-cart"]').val(),
productQty = $(this).find('input.qty').val(),
variationArray = {},
productElm = $(this).closest('.product');
if( $(this).find('.select-variation').length > 0 ) {
var customAttributeName = $(this).find('.select-variation').attr('name');
variationArray[customAttributeName] = $(this).find('.select-variation').val();
}
$.ajax({
url: mz_obj.ajax_url,
type: 'post',
data: {
action: 'mz_action_add_to_cart',
product_id: productID,
quantity: productQty,
variation_id: productVariationID,
variation: variationArray,
},
beforeSend: function() {
$('.toast').toast('hide');
},
success: function (response) {
$('.toast-body').html('');
var data = JSON.parse(response);
if (data.add_to_cart == false) {
// Do something on Error
} else {
$( document.body ).trigger( 'wc_fragment_refresh' );
}
},
});
});
// Ajax add to cart
add_action( 'wp_ajax_mz_action_add_to_cart', 'mz_action_add_to_cart' );
add_action( 'wp_ajax_nopriv_mz_action_add_to_cart', 'mz_action_add_to_cart' );
function mz_action_add_to_cart() {
$product_id = isset($_POST['product_id']) && !empty($_POST['product_id']) ? sanitize_text_field($_POST['product_id']) : false;
$variation_id = isset($_POST['variation_id']) && !empty($_POST['variation_id']) ? sanitize_text_field($_POST['variation_id']) : false;
$quantity = isset($_POST['quantity']) && !empty($_POST['quantity']) ? sanitize_text_field($_POST['quantity']) : false;
$variation = isset($_POST['variation']) && !empty($_POST['variation']) && is_array($_POST['variation']) ? $_POST['variation'] : false;
wc_clear_notices();
if( $variation_id && $variation ) {
$add_to_cart = WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation );
} else {
$add_to_cart = WC()->cart->add_to_cart( $product_id, $quantity );
}
if( $add_to_cart ) {
do_action('woocommerce_ajax_added_to_cart', $product_id);
$notices[] = 'המוצר נוסף בהצלחה';
$_product = WC()->cart->get_cart_item($add_to_cart);
} else {
$notices = wc_get_notices();
}
$return_array = [
'add_to_cart' => $add_to_cart,
'notices' => $notices,
];
echo json_encode( $return_array );
wp_die();
}
// Return fragments for woocomemrce on add to cart
add_filter( 'woocommerce_add_to_cart_fragments', 'mz_add_to_cart_fragment', 999 , 1 );
function mz_add_to_cart_fragment($fragments){
ob_start();
?>
<span class="cart_count"><?php echo WC()->cart->get_cart_contents_count( ); ?></span>
<?php
$fragments['.cart_count'] = ob_get_clean();
ob_start();
?>
<span class="cart_total"><?php echo wc_price(WC()->cart->get_cart_contents_total()); ?></span>
<?php
$fragments['.cart_total'] = ob_get_clean();
$fragments['.cross-sells'] = '<div class="cross-sells">';
ob_start();
woocommerce_cross_sell_display(8);
$fragments['.cross-sells'] .= ob_get_clean();
$fragments['.cross-sells'] .= '</div>';
return $fragments;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment