WooCommerce에서 최소 장바구니 금액으로 무료 선물 제품 추가

Nov 17 2020

$ 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' );

모든 제품과 함께 작동하도록 해당 코드를 변경하고 카트 합계로만 제한하는 방법이 있습니까?

답변

2 LoicTheAztec Nov 17 2020 at 10:06

업데이트 됨

다음과 같이 소계가 특정 금액에 도달하면 무료 선물 제품이 장바구니에 추가됩니다.

// 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") :

미니 카트 :