gpt4 book ai didi

php - 在 WooCommerce 中添加最低购物车金额的免费赠品产品

转载 作者:行者123 更新时间:2023-12-02 02:32:15 29 4
gpt4 key购买 nike

我想为订单超过 50 美元的客户提供免费礼物。如果购物车中有特定产品,则不会(stackoverflow 和下面有一些示例)。

经过一番研究,我发现以下代码可以在将另一个特定产品添加到购物车时添加免费产品。

add_action( 'template_redirect', 'bbloomer_add_gift_if_id_in_cart' );

function bbloomer_add_gift_if_id_in_cart() {

if ( is_admin() ) return;
if ( WC()->cart->is_empty() ) return;

$product_bought_id = 32;
$product_gifted_id = 57;

// see if product id in cart
$product_bought_cart_id = WC()->cart->generate_cart_id( $product_bought_id );
$product_bought_in_cart = WC()->cart->find_product_in_cart( $product_bought_cart_id );

// see if gift id in cart
$product_gifted_cart_id = WC()->cart->generate_cart_id( $product_gifted_id );
$product_gifted_in_cart = WC()->cart->find_product_in_cart( $product_gifted_cart_id );

// if not in cart remove gift, else add gift
if ( ! $product_bought_in_cart ) {
if ( $product_gifted_in_cart ) WC()->cart->remove_cart_item( $product_gifted_in_cart );
} else {
if ( ! $product_gifted_in_cart ) WC()->cart->add_to_cart( $product_gifted_id );
}
}

在这里找到:https://www.businessbloomer.com/woocommerce-buy-1-product-add-free-product-cart-programmatically/

我也尝试过此代码,但如果购物车发生更改,它不会更新:

function aapc_add_product_to_cart() {
global $woocommerce;

$cart_total = 50;

if ( $woocommerce->cart->total >= $cart_total ) {
if ( ! is_admin() ) {
$free_product_id = 12989; // Product Id of the free product which will get added to cart
$found = false;

//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->get_id() == $free_product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $free_product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $free_product_id );
}
}
}
}

add_action( 'template_redirect', 'aapc_add_product_to_cart' );

有什么方法可以更改该代码以适用于任何产品并且仅限制购物车总数?

最佳答案

已更新

通过以下方式,如果小计达到特定金额,免费赠送的产品将添加到购物车:

// Add free gifted product for specific cart subtotal
add_action( 'woocommerce_before_calculate_totals', 'check_free_gifted_product' );
function check_free_gifted_product( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;

// Settings
$free_product_id = 37;
$targeted_subtotal = 50;

$cart_subtotal = 0; // Initializing

// Loop through cart items (first loop)
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){
// When free product is is cart
if ( $free_product_id == $cart_item['product_id'] ) {
$free_key = $cart_item_key;
$free_qty = $cart_item['quantity'];
$cart_item['data']->set_price(0); // Optionally set the price to zero
} else {
$cart_subtotal += $cart_item['line_total'] + $cart_item['line_tax'];
}
}

// If subtotal match and free product is not already in cart, add it
if ( ! isset($free_key) && $cart_subtotal >= $targeted_subtotal ) {
$cart->add_to_cart( $free_product_id );
}
// If subtotal doesn't match and free product is already in cart, remove it
elseif ( isset($free_key) && $cart_subtotal < $targeted_subtotal ) {
$cart->remove_cart_item( $free_key );
}
// Keep free product quantity to 1.
elseif ( isset($free_qty) && $free_qty > 1 ) {
$cart->set_quantity( $free_key, 1 );
}
}

// Display free gifted product price to zero on minicart
add_filter( 'woocommerce_cart_item_price', 'change_minicart_free_gifted_item_price', 10, 3 );
function change_minicart_free_gifted_item_price( $price_html, $cart_item, $cart_item_key ) {
$free_product_id = 27;

if( $cart_item['product_id'] == $free_product_id ) {
return wc_price( 0 );
}
return $price_html;
}

代码位于事件子主题(或事件主题)的functions.php 文件中。经过测试并有效。


购物车页面(免费赠送的产品是“Beanie”):

enter image description here

在迷你购物车上:

enter image description here

关于php - 在 WooCommerce 中添加最低购物车金额的免费赠品产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64872198/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com