source

WooCommerce 모든 제품을 카트에서 삭제하고 현재 제품을 카트에 추가합니다.

bestscript 2023. 2. 12. 18:07

WooCommerce 모든 제품을 카트에서 삭제하고 현재 제품을 카트에 추가합니다.

저는 WooCommerce에 처음이라서 장바구니에 하나의 제품만 추가할 수 있어야 합니다.[ Add to cart ]버튼을 클릭하면 모든 제품을 클리어하고 현재 제품을 카트에 추가합니다.

내가 어떻게 그럴 수 있을까?

나는 이것에 대한 정확한 해결책을 가지고 있다.다음 코드를 사용해 보십시오.

add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10,  3);

function wdm_empty_cart( $cart_item_data, $product_id, $variation_id ) 
{

    global $woocommerce;
    $woocommerce->cart->empty_cart();

    // Do nothing with the data and return
    return $cart_item_data;
}

위의 필터는 함수 add_to_cart() 내의 class-wc-cart.php에 정의되어 있습니다.
http://docs.woothemes.com/wc-apidocs/source-class-WC_Cart.html#774-905
따라서 Add to Cart 버튼을 누르면 카트를 비운 다음 제품을 추가합니다.

이거 드셔보세요.

//For removing all the items from the cart
global $woocommerce;
$woocommerce->cart->empty_cart();
$woocommerce->cart->add_to_cart($product_id,$qty);

클래스 파일은wp-content/plugins/woocommerce/classes/class-wc-cart.php

도움이 되었으면 좋겠는데..

언급URL : https://stackoverflow.com/questions/21181911/woocommerce-delete-all-products-from-cart-and-add-current-product-to-cart