Created
January 29, 2022 00:15
-
-
Save willybahuaud/87603064d7dff3a370d8feade18f0b84 to your computer and use it in GitHub Desktop.
Des boutons de quantité synchronisés en ajax avec le panier WooCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'woocommerce_after_shop_loop_item', 'custom_add_to_cart' ); | |
function custom_add_to_cart() { | |
global $product; | |
vprintf( '<div class="add-to-cart-block"> | |
<div class="add-to-cart-block__price">%s</div> | |
<hr/> | |
%s | |
</div>', | |
array( | |
$product->get_price_html(), | |
get_selectors() | |
) ); | |
} | |
function get_selectors() { | |
global $product; | |
if ( ! $product->is_type( 'variable' ) ) { | |
return get_select_qty() . '<button class="w-button-cart w-add-to-cart">Ajouter au panier</button>'; | |
} else { | |
$atts = array(); | |
foreach ($product->get_attributes() as $taxonomy => $attribute_obj ) { | |
if ( $attribute_obj->get_variation() ) { | |
$attribute_label_name = wc_attribute_label( $taxonomy ); | |
$atts[] = $attribute_label_name; | |
} | |
} | |
return '<a href="' . get_permalink( $product->get_ID() ) . '" class="need-configuration">' . wp_sprintf( __( 'Choisir %l', 'w-espace-client' ), $atts ) . '</a><a class="w-button-cart" href="' . get_permalink( $product->get_ID() ) . '">Voir le produit</a>'; | |
} | |
} | |
function get_select_qty() { | |
global $product; | |
return vsprintf( ' | |
<form method="post" class="product-qty-selector"> | |
<input type="hidden" name="pid" value="%1$s"/> | |
<input type="hidden" name="action" value="%3$s"/> | |
<button class="qty-button qty-button--remove" type="button">−</button><input type="number" min="1" max="%4$d" value="%2$d" name="qty" class="product-qty product-qty--%1$s"/><button class="qty-button qty-button--add" type="button">+</button> | |
</form>', array( | |
esc_attr( $product->get_ID() ), | |
1, | |
'w-product.cart.adjust', | |
$product->get_stock_quantity() ?? 100, | |
) ); | |
} | |
add_action('wp_ajax_w-product.cart.adjust', 'w_ajax_add_to_cart'); | |
add_action('wp_ajax_nopriv_w-product.cart.adjust', 'w_ajax_add_to_cart'); | |
function w_ajax_add_to_cart() { | |
$product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_POST['pid'])); | |
$qty = $_POST['qty']; | |
if ( $qty > 0 ) { | |
// add | |
$passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity); | |
$product_status = get_post_status( $product_id ); | |
$quantity = wc_stock_amount( $qty ); | |
if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $qty, 0) && 'publish' === $product_status ) { | |
do_action( 'woocommerce_ajax_added_to_cart', $product_id ); | |
if ('yes' === get_option('woocommerce_cart_redirect_after_add')) { | |
wc_add_to_cart_message(array($product_id => $quantity), true); | |
} | |
\WC_AJAX::get_refreshed_fragments(); | |
} | |
} | |
// if ( $qty < 0 && ! empty( $cart_item_key ) ) { | |
// //remove | |
// WC()->cart->set_quantity( $cart_item_key, $new_qty ); | |
// \WC_AJAX::get_refreshed_fragments(); | |
// } | |
exit; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function ($) { | |
$('body').on('click', '.qty-button',function(e){ | |
e.preventDefault(); | |
var $in = $(this).siblings('.product-qty'); | |
var val = $in.val(); | |
$in.val($(this).hasClass('qty-button--add') ? parseInt(val) + 1 : Math.max(1, parseInt(val) - 1 ) ); | |
$in.trigger('change'); | |
}); | |
$('body').on('click', '.w-add-to-cart', function(e){ | |
e.preventDefault(); | |
var $form = $(this).prev('.product-qty-selector'); | |
$.ajax({ | |
url: ajaxUrl, | |
method:'POST', | |
data:$form.serialize(), | |
success:function(response) { | |
$(document.body).trigger('added_to_cart', [response.fragments, response.cart_hash]); | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment