programing

Woocommerce에서 카트에서 무료 제품 자동 추가 또는 제거

elecom 2023. 6. 12. 21:06
반응형

Woocommerce에서 카트에서 무료 제품 자동 추가 또는 제거

저는 우커머스 매장을 운영하고 있습니다. 고객님께서 바구니에 상품을 추가하신 후 바구니에 담긴 사은품(전자책)을 모든 고객에게 드리고 싶습니다.

예:
바구니에 "product1"을 추가하면 바구니에 2개의 제품이 표시됩니다."제품 1"과 "공짜".바구니에서 상품을 제거하시면 사은품이 다시 제거됩니다.

일단 이 코드를 받았습니다.

add_action( 'woocommerce_add_to_cart', 'check_freebie_exists', 10, 6 );
function check_freebie_exists($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
    if($product_id == 1234) { // obviously replace this with the product that triggers the freebie
        /* or you could use
        if(has_term($cat_id, 'product_cat', $product_id)) { to check if a product is in a particular product_cat
        or you could check anything else related to the product
        */
        $hasfreebie = false;
        // loop through the cart to check the freebie is not already there
        global $woocommerce;
        $cart = $woocommerce->cart->get_cart();
        foreach($cart as $key => $values) {
            if($values['data']->id == $your_freebie_product_id) {
                $hasfreebie = true;
                break;
            }
        }
        if(!$hasfreebie) {
            $woocommerce->cart->add_to_cart($your_freebie_product_id);
        }
    }
}

add_action( 'woocommerce_cart_item_removed', 'remove_freebie', 10, 2 );
function remove_freebie( $cart_item_key, $cart ) {
    $hasmaster = false;
    $freebiekey = NULL;
    foreach($cart as $key => $values) {
        if($values['data']->id == 1234) { // check that we have the product that should trigger the freebie
            $hasmaster = true;
        } elseif($values['data']->id == $your_freebie_product_id) {
            $freebiekey = $key;
        }
    }
    if(!$hasmaster && $freebiekey) {
        $cart->remove_cart_item($freebiekey);
    }
}

하지만 아직은 효과가 없는 것 같습니다.

내가 뭘 잘못하고 있는 거지?

어떤 도움이든 정말 감사하겠습니다.

업데이트 2 - 2018년 10월 - 개선되고 향상된 코드(완전 재검토)

다음 코드는 카트에 처음 추가할 때 한 번만 무료 제품을 추가합니다.다른 모든 카트 항목이 제거되면 무료 항목도 제거됩니다.

add_action( 'woocommerce_before_calculate_totals', 'add_remove_freebie', 50, 1 );
function add_remove_freebie( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $freebie_id = 70; // <== HERE set the freebie product ID
    $has_others = false;

    // Loop through cart items
    foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // Added Woocommerce compatibility version
        $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();

        if( $product_id == $freebie_id ) {
            // Freebie is in cart
            $freebie_key = $cart_item_key;
        } else {
            // Other items are in cart
            $has_others = true;
        }
    }
    // If freebie product is alone in cart we remove it
    if( ! $has_others && isset( $freebie_key ) ){
        $cart->remove_cart_item( $freebie_key );
    } elseif ( $has_others && ! isset( $freebie_key ) ) {
        $cart->add_to_cart($freebie_id);
    }
}

코드가 작동합니다.활성 하위 테마(또는 활성 테마)의 php 파일입니다.테스트를 거쳐 작동합니다.

언급URL : https://stackoverflow.com/questions/39234409/auto-add-or-remove-a-freebie-product-from-cart-in-woocommerce

반응형