I want to create custom add to cart button to add my 3 product into cart with 2 quantity for each..
我想创建自定义添加到购物车按钮,将我的3个产品添加到购物车中,每个产品有2个数量。
For add three product into cart I have done using like this:
为了将三个产品添加到购物车中,我使用了这样的方法:
<a id="buy" class="single_add_to_cart_button shop-skin-btn shop-flat-btn alt" href="#">ADD MY PRODUCT</a>
$p_id = my product id eg: 45,99,152
$ p_id =我的产品ID,例如:45,99,152
<script>
jQuery('#buy').click(function(e) {
e.preventDefault();
var myStringArray = [<?php echo $p_id; ?>];
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
addToCart(myStringArray[i]);
}
return true;
});
function addToCart(p_id) {
$.get('/glassful/?post_type=product&add-to-cart=' + p_id, function() {
$(".show_success").show();
});
}
</script>
It will add my resulted product into cart but with only 1 quantity Please let me know how I can add quantity? I want to add 2 quantity of each product.
它会将我的产品添加到购物车中,但只有1个数量请告诉我如何添加数量?我想每个产品添加2个数量。
Mean when click on add to cart three product will added to cart with 2 quantity each.
意思是当点击添加到购物车时,三个产品将被添加到购物车中,每个数量为2个。
Thanks for help in advance..
在此先感谢您的帮助..
3 个解决方案
#1
4
You need to pass quantity in query string like:
您需要在查询字符串中传递数量,如:
?post_type=product&add-to-cart=100&quantity=2
I have modify you code
我修改了你的代码
<script>
jQuery('#buy').click(function(e) {
e.preventDefault();
var myStringArray = [<?php echo $p_id; ?>];
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
addToCart(myStringArray[i],2);
}
return true;
//window.location.href = "http://seoexpertiser.ca/glassful/cart/";
});
function addToCart(p_id,qu) {
$.get('/glassful/?post_type=product&add-to-cart=' + p_id +'&quantity='+qu, function() {
// success
$(".show_success").show();
});
}
</script>
I think this will solve your problem.
我想这会解决你的问题。
#2
1
Please check this link , This is works for me :)
请检查此链接,这对我有用:)
WooCommerce: Allow adding multiple products to the cart via the add-to-cart query string
WooCommerce:允许通过添加到购物车查询字符串将多个产品添加到购物车
functions.php
的functions.php
function woocommerce_maybe_add_multiple_products_to_cart() {
// Make sure WC is installed, and add-to-cart qauery arg exists, and contains at least one comma.
if ( ! class_exists( 'WC_Form_Handler' ) || empty( $_REQUEST['add-to-cart'] ) || false === strpos( $_REQUEST['add-to-cart'], ',' ) ) {
return;
}
// Remove WooCommerce's hook, as it's useless (doesn't handle multiple products).
remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );
$product_ids = explode( ',', $_REQUEST['add-to-cart'] );
$count = count( $product_ids );
$number = 0;
foreach ( $product_ids as $product_id ) {
if ( ++$number === $count ) {
// Ok, final item, let's send it back to woocommerce's add_to_cart_action method for handling.
$_REQUEST['add-to-cart'] = $product_id;
return WC_Form_Handler::add_to_cart_action();
}
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $product_id ) );
$was_added_to_cart = false;
$adding_to_cart = wc_get_product( $product_id );
if ( ! $adding_to_cart ) {
continue;
}
$add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->product_type, $adding_to_cart );
/*
* Sorry.. if you want non-simple products, you're on your own.
*
* Related: WooCommerce has set the following methods as private:
* WC_Form_Handler::add_to_cart_handler_variable(),
* WC_Form_Handler::add_to_cart_handler_grouped(),
* WC_Form_Handler::add_to_cart_handler_simple()
*
* Why you gotta be like that WooCommerce?
*/
if ( 'simple' !== $add_to_cart_handler ) {
continue;
}
// For now, quantity applies to all products.. This could be changed easily enough, but I didn't need this feature.
$quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] );
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity ) ) {
wc_add_to_cart_message( array( $product_id => $quantity ), true );
}
}
}
// Fire before the WC_Form_Handler::add_to_cart_action callback.
add_action( 'wp_loaded', 'woocommerce_maybe_add_multiple_products_to_cart', 15 );
and you can use for your link.
你可以使用你的链接。
$product_ids = implode( ',', array( 1, 2, 55 ) );
$url = esc_url_raw( add_query_arg( 'add-to-cart', $product_ids, wc_get_checkout_url() ) );
Thanks !
谢谢 !
#3
0
Save products id in array and make ajax request
将产品ID保存在数组中并发出ajax请求
jQuery("#addSelected").click(function() { var arrayLength =
arrayOfAddedProducts.length; jQuery.ajax({
type: "POST",
url: ajaxurl,
data: {action : 'add_item_from_cart','getIDtoAdd' : arrayOfAddedProducts},
success: function (res) {
wc_load_all_products();
}
}); });
function.php
function.php
function add_item_from_cart() {
foreach ($_POST['getIDtoAdd'] as $productId) {
WC()->cart->add_to_cart( intval($productId), 1, 0, array(), array() );
// WC()->cart->add_to_cart( $product_id = 0, $quantity = 1, $variation_id = 0, $variation = array(), $cart_item_data = array() );
}
}
add_action('wp_ajax_add_item_from_cart', 'add_item_from_cart');
add_action('wp_ajax_nopriv_add_item_from_cart', 'add_item_from_cart');
#1
4
You need to pass quantity in query string like:
您需要在查询字符串中传递数量,如:
?post_type=product&add-to-cart=100&quantity=2
I have modify you code
我修改了你的代码
<script>
jQuery('#buy').click(function(e) {
e.preventDefault();
var myStringArray = [<?php echo $p_id; ?>];
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
addToCart(myStringArray[i],2);
}
return true;
//window.location.href = "http://seoexpertiser.ca/glassful/cart/";
});
function addToCart(p_id,qu) {
$.get('/glassful/?post_type=product&add-to-cart=' + p_id +'&quantity='+qu, function() {
// success
$(".show_success").show();
});
}
</script>
I think this will solve your problem.
我想这会解决你的问题。
#2
1
Please check this link , This is works for me :)
请检查此链接,这对我有用:)
WooCommerce: Allow adding multiple products to the cart via the add-to-cart query string
WooCommerce:允许通过添加到购物车查询字符串将多个产品添加到购物车
functions.php
的functions.php
function woocommerce_maybe_add_multiple_products_to_cart() {
// Make sure WC is installed, and add-to-cart qauery arg exists, and contains at least one comma.
if ( ! class_exists( 'WC_Form_Handler' ) || empty( $_REQUEST['add-to-cart'] ) || false === strpos( $_REQUEST['add-to-cart'], ',' ) ) {
return;
}
// Remove WooCommerce's hook, as it's useless (doesn't handle multiple products).
remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );
$product_ids = explode( ',', $_REQUEST['add-to-cart'] );
$count = count( $product_ids );
$number = 0;
foreach ( $product_ids as $product_id ) {
if ( ++$number === $count ) {
// Ok, final item, let's send it back to woocommerce's add_to_cart_action method for handling.
$_REQUEST['add-to-cart'] = $product_id;
return WC_Form_Handler::add_to_cart_action();
}
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $product_id ) );
$was_added_to_cart = false;
$adding_to_cart = wc_get_product( $product_id );
if ( ! $adding_to_cart ) {
continue;
}
$add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->product_type, $adding_to_cart );
/*
* Sorry.. if you want non-simple products, you're on your own.
*
* Related: WooCommerce has set the following methods as private:
* WC_Form_Handler::add_to_cart_handler_variable(),
* WC_Form_Handler::add_to_cart_handler_grouped(),
* WC_Form_Handler::add_to_cart_handler_simple()
*
* Why you gotta be like that WooCommerce?
*/
if ( 'simple' !== $add_to_cart_handler ) {
continue;
}
// For now, quantity applies to all products.. This could be changed easily enough, but I didn't need this feature.
$quantity = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( $_REQUEST['quantity'] );
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity ) ) {
wc_add_to_cart_message( array( $product_id => $quantity ), true );
}
}
}
// Fire before the WC_Form_Handler::add_to_cart_action callback.
add_action( 'wp_loaded', 'woocommerce_maybe_add_multiple_products_to_cart', 15 );
and you can use for your link.
你可以使用你的链接。
$product_ids = implode( ',', array( 1, 2, 55 ) );
$url = esc_url_raw( add_query_arg( 'add-to-cart', $product_ids, wc_get_checkout_url() ) );
Thanks !
谢谢 !
#3
0
Save products id in array and make ajax request
将产品ID保存在数组中并发出ajax请求
jQuery("#addSelected").click(function() { var arrayLength =
arrayOfAddedProducts.length; jQuery.ajax({
type: "POST",
url: ajaxurl,
data: {action : 'add_item_from_cart','getIDtoAdd' : arrayOfAddedProducts},
success: function (res) {
wc_load_all_products();
}
}); });
function.php
function.php
function add_item_from_cart() {
foreach ($_POST['getIDtoAdd'] as $productId) {
WC()->cart->add_to_cart( intval($productId), 1, 0, array(), array() );
// WC()->cart->add_to_cart( $product_id = 0, $quantity = 1, $variation_id = 0, $variation = array(), $cart_item_data = array() );
}
}
add_action('wp_ajax_add_item_from_cart', 'add_item_from_cart');
add_action('wp_ajax_nopriv_add_item_from_cart', 'add_item_from_cart');