![](/img/trans.png)
[英]Add item to cart automatically when another product is present in magento cart
[英]Add item to cart automatically when another product is present
我正在使用動態定價來折扣商品以使彼此免費存在,我想避免讓客戶將免費產品作為一個單獨的步驟添加,而只是讓系統添加它。
我從代碼段開始,但是當該項目存在時我無法使它正常工作
這是我到目前為止所得到的:
<?php
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id_gift = 2287;
$found = false;
$product_id = 30;
$incart_free = false;
foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id ){
$incart_free = true;
}
return $incart_free;
}
if( $incart_free == true ) {
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id_gift )
$found = true;
}
// if product not found, add it
if ( $found != true )
$woocommerce->cart->add_to_cart( $product_id_gift );
} else {
// if no products in cart, add it
$woocommerce->cart->add_to_cart( $product_id_gift );
}
}
}
}
add_action( 'init', 'add_product_to_cart' );
?>
謝謝!
如果我正確地理解了您的問題,那么如果購物車中有某些物品,您正在尋找免費物品。 這是我的解決方案:
1.在Wordpress中為WooCommerce創建優惠券。 將優惠券金額設為100% ,並將折扣類型設為“產品%折扣 ”。 轉到“使用限制”->“產品”,然后指定要免費使用的特定產品,這將使優惠券僅適用於該特定產品 。
2.創建一個函數,該函數首先檢查購物車中是否存在特定物品,如果存在,則將要免費提供給購物車的物品添加並打折。 以下代碼可以解決問題(盡管這不是最干凈的解決方案,但我已經對其進行了測試,並且效果很好):
add_action( 'init', 'product_discount' );
function product_discount(){
//variable declerations.
global $woocommerce;
$product_id = 1; // product to add
$products= array('2', '3', '4'); //specific product(s) to be present in the cart
$coupon_code = 'abc'; // coupon code from wp
//get the cart contents.
$cart_items = $woocommerce->cart->get_cart();
//check if the cart is not empty.
if(sizeof($cart_items) > 0){
//loop through the cart items looking for the specific products.
foreach ($cart_items as $key => $item){
//check if the cart items match to any of those in the array and check if the desired product is in the cart.
if(in_array($item['product_id'], $products) && $item['product_id'] != $product_id){
//add course.
$woocommerce->cart->add_to_cart($product_id);
//discount course.
$woocommerce->cart->add_discount(sanitize_text_field($coupon_code));
}else{
break; //to prevent the product from being added again for the next loop.
}
}
}
}
希望這可以幫助!
您的邏輯在這里是完全錯誤的,因為if ( $_product->id == $product_id_gift )
將永遠不會為真,因為兩個product_id是不同的。
因此邏輯應為:
1.檢查添加到購物車中的所有產品
2.檢查購物車中是否有免費產品
3.如果是,則只需添加免費產品。
所以code
將是這樣的:
add_action( 'init', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 30; //Your product ID here
$free_product_id = 2287; //Free product ID here
$found = false;
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id ){
$found = true;
}
}
// if product not found, add it
if ( $found )
$woocommerce->cart->add_to_cart( $free_product_id );
}
}
}
注意:未經測試。所以也讓我知道輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.