2017-02-03 1 views
2

WooCommerce에서 한 번에 여러 제품을 추가 할 수 있도록 @ jtsternberg의WooCommerce: Allow adding multiple products to the cart via the add-to-cart query string을 구현했지만 실제로 여러 제품 중 하나를 사용하려고 시도한 고객으로부터 많은 불만이 접수되었습니다. 여러 제품을 포함하는 링크.장바구니에 없으면 장바구니에 여러 제품 추가

고객이 체크 아웃을 클릭 한 다음 브라우저 "뒤로"버튼을 클릭하면 모든 항목 수량이 증가합니다. 나는 장바구니에 추가 동작이 완료된 후 추가 매개 변수가 제거 된 장바구니 URL로 사용자를 리디렉션하여이 문제를 해결했지만 이상적이지는 않습니다.

내가 정말로 원하는 것은 항목이 장바구니에 처음 있는지 확인하고 이미없는 경우 장바구니에만 추가하는 것입니다. 비슷한 일을 한 사람 있습니까?

작업 업데이트 : 나는 기본 추가 - 투 - 카트 동작과 충돌을 방지하기 위해 완전히 별도의 매개 변수 이름을 사용하는 @jtsternberg에서 코드를 수정 끝났다. 그런 다음, @ LoicTheAztec의 제안 된 코드를 사용하여 새 param이 있는지 확인하기 위해 동작을 래핑하여 사용할 수있었습니다. 당신이 뭔가에 이미 장바구니에 희귀 한 제품 여부를 확인하기 위해 사용자 정의 후크 기능에서 사용할 수 있습니다, 당신은 그 안에 woocommerce_add_to_cart_validation 필터 훅이 코드에서와 같이

function custom_product_link() { 
    if (empty($_REQUEST['multi-product-add'])) { 
    return; 
    } 

    $product_ids = explode(',', $_REQUEST['multi-product-add']); 

    foreach ($product_ids as $product_id) { 
    $product_id  = apply_filters('woocommerce_add_to_cart_product_id', absint($product_id)); 
    $was_added_to_cart = false; 
    $adding_to_cart = wc_get_product($product_id); 

    if (! $adding_to_cart) { 
     continue; 
    } 

    $add_to_cart_handler = apply_filters('woocommerce_add_to_cart_handler', $adding_to_cart->product_type, $adding_to_cart); 

    if ('simple' !== $add_to_cart_handler) { 
     continue; 
    } 

    // For now, quantity applies to all products.. This could be changed easily enough, but I didn't need this feature. 
    $quantity   = empty($_REQUEST['quantity']) ? 1 : wc_stock_amount($_REQUEST['quantity']); 
    $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity); 

    if ($passed_validation && false !== WC()->cart->add_to_cart($product_id, $quantity)) { 
     wc_add_to_cart_message(array($product_id => $quantity), true); 
    } 
    } 
    if (wc_notice_count('error') === 0) { 
    // If has custom URL redirect there 
    if ($url = apply_filters('woocommerce_add_to_cart_redirect', false)) { 
     wp_safe_redirect($url); 
     exit; 
    } elseif (get_option('woocommerce_cart_redirect_after_add') === 'yes') { 
     wp_safe_redirect(wc_get_cart_url()); 
     exit; 
    } 
    } 
} 

function check_product_added_to_cart($passed, $product_id, $quantity) { 
    if (!empty($_REQUEST['multi-product-add'])) { 
    foreach (WC()->cart->get_cart() as $cart_key => $cart_item){ 
     // if products are already in cart: 
     if($cart_item['product_id'] == $product_id) { 
     // Set the verification variable to "not passed" (false) 
     $passed = false; 
     // (Optionally) Displays a notice if product(s) are already in cart 
     // wc_add_notice('<strong>' . $btn['label'] . '</strong> ' . __('This product is already in your cart.', 'woocommerce'), 'error'); 
     // Stop the function returning "false", so the products will not be added again 
     return $passed; 
     } 
    } 
    } 
    return $passed; 
} 
add_action('wp_loaded', 'custom_product_link', 15); 
add_action('woocommerce_add_to_cart_validation', 'check_product_added_to_cart', 10, 3); 

답변

2

사용중인 : 여기에 전체 섹션입니다 :

add_action('woocommerce_add_to_cart_validation', 'check_product_added_to_cart', 10, 3); 
function check_product_added_to_cart($passed, $product_id, $quantity) { 
    foreach (WC()->cart->get_cart() as $cart_key => $cart_item){ 
     // if products are already in cart: 
     if($cart_item['product_id'] == $product_id) { 
      // Set the verification variable to "not passed" (false) 
      $passed = false; 
      // (Optionally) Displays a notice if product(s) are already in cart 
      wc_add_notice('<strong>' . $btn['label'] . '</strong> ' . __('This product is already in your cart.', 'woocommerce'), 'error'); 
      // Stop the function returning "false", so the products will not be added again 
      return $passed; 
     } 
    } 
    return $passed; 
} 

코드는 플러그인 파일도 function.php의 활성 자식 테마 (또는 테마)의 파일이나 간다.

코드를 테스트하고이 일반적으로해야 당신의 정의와 작품 ...

제품 수량을 위해, 당신은 몇 가지 조건 $cart_item['quantity']$quantity 인수 ...

+0

와우를 사용할 수 있습니다, 이것은 내가 작업했던 것보다 훨씬 더 간단합니다! 그래도 일부 테스트를 실행해야합니다. 정상적인 장바구니 추가 동작을 방해합니다 (예 : 사용자가 제품을 클릭하면? – bhamrick

+0

@bhamrick 당신은 테스트를해야합니다 ... 귀하의 제품, 설정에 따라 달라질 수는 없습니다.이 필터 코드는 장바구니에 같은 품목을 두 번 추가하는 고객을 피할뿐입니다. 일반적으로 고객은 2 배의 동일한 품목을 카트에 넣지 않으며, 수량을 설정/업데이트합니다 ... 그래서 나는 그것이 문제라고 생각하지 않습니다. – LoicTheAztec

+1

감사합니다! 이것은 훌륭하게 작동했습니다. param 이름을 디폴트'add-to-cart'에서'multi-product-add'로 바꾸는 작업을 마쳤습니다. 그리고 나서'foreach' 문을'if (! empty ($ _REQUEST [ 'multi -product-add '])))'블록을 사용하십시오. – bhamrick

관련 문제