Answer the question
In order to leave comments, you need to log in
How to make it possible to buy only one product?
Good afternoon, I was looking for a code in the public domain that allows only one product to be added to the cart, but they all do not work, here is one of the many that I tried:
add_filter( 'woocommerce_add_to_cart_validation', 'custom_only_one_in_cart', 99, 2 );
function custom_only_one_in_cart( $passed, $added_product_id ) {
// empty cart first: new item will replace previous
wc_empty_cart();
// display a message if you like
wc_add_notice( 'Max number of items in cart reached!', 'notice' );
return $passed;
}
$('.s1_oneclick_btn').click(function(e) {
e.preventDefault();
$(this).addClass('adding-cart');
var product_id = '14';
var ajax_url = "/wp-admin/admin-ajax.php"
$.ajax ({
url: ajax_url,
type:'POST',
data:'action=oneclick&product_id=' + product_id + '&quantity=1',
})
.done(function() {
var url = "http://a0368503.xsph.ru/checkout/";
$(location).attr('href',url);
});
});
function oneclick() {
$product_id = $_POST['product_id'];
$variation_id = $_POST['variation_id'];
$quantity = $_POST['quantity'];
if ($variation_id) {
WC()->cart->add_to_cart( $product_id, $quantity, $variation_id );
} else {
WC()->cart->add_to_cart( $product_id, $quantity);
}
$items = WC()->cart->get_cart();
global $woocommerce;
$item_count = $woocommerce->cart->cart_contents_count;
die();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question