htaccess
2
Вклад в тег
<button type="button" class="single_add_to_cart_button button alt<?php echo esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '' ); ?> add-to-cart-variation">В корзину</button>
jQuery(document).ready(function($) {
$('body').on('click', '.add-to-cart-variation', function() {
var variation_id = $(this).closest('form.cart').find('.variation_id').val();
var quantity = $(this).closest('form.cart').find('.quantity').val();
var product_id = $(this).closest('form.cart').find('input[name="product_id"]').val();
var data = {
action: 'add_variation_to_cart',
product_id: product_id,
variation_id: variation_id,
quantity: quantity
};
$.ajax({
type: 'POST',
url: wc_add_to_cart_params.ajax_url,
data: data,
success: function(response) {
if (response) {
$(document.body).trigger('added_to_cart', [response.fragments, response.cart_hash, $(this)]);
}
},
dataType: 'json'
});
return false;
});
});
jQuery(document).ready(function($) {
$('body').on('click', '.add-to-cart-variation', function() {
var variation_id = $(this).closest('form.cart').find('.variation_id').val();
var quantity = $(this).closest('form.cart').find('.quantity').val();
var product_id = $(this).closest('form.cart').find('input[name="product_id"]').val();
var data = {
action: 'add_variation_to_cart',
product_id: product_id,
variation_id: variation_id,
quantity: quantity
};
$.ajax({
type: 'POST',
url: wc_add_to_cart_params.ajax_url,
data: data,
success: function(response) {
if (response) {
$(document.body).trigger('added_to_cart', [response.fragments, response.cart_hash, $(this)]);
}
},
dataType: 'json'
});
return false;
});
});
add_action('wp_ajax_add_variation_to_cart', 'add_variation_to_cart');
add_action('wp_ajax_nopriv_add_variation_to_cart', 'add_variation_to_cart');
function add_variation_to_cart() {
$product_id = intval($_POST['product_id']);
$variation_id = intval($_POST['variation_id']);
$quantity = intval($_POST['quantity']);
$variation = array(
'variation_id' => $variation_id,
'quantity' => $quantity
);
WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variation);
WC_AJAX::get_refreshed_fragments();
wp_die();
}