2016-10-26 2 views
1

작은 Abandon Cart Recovery Plugin으로 작업 중이며 woocommerce_sessions 테이블에서 장바구니를 복구해야합니다.WooCommerce 세션 데이터에서 장바구니를 복구하십시오.

카트에있는 1 개의 변수 제품에 대한 일련의 카트 데이터입니다.

array (size=1) 
    'cart' => 
    array (size=9) 
     'product_id' => int 22 
     'variation_id' => int 24 
     'variation' => 
     array (size=1) 
      'attribute_pa_color' => string 'green' (length=5) 
     'quantity' => int 1 
     'line_total' => float 20 
     'line_tax' => int 0 
     'line_subtotal' => int 20 
     'line_subtotal_tax' => int 0 
     'line_tax_data' => 
     array (size=2) 
      'total' => 
      array (size=0) 
       empty 
      'subtotal' => 
      array (size=0) 
       empty 

이 데이터로 전체 장바구니를 복구하려고합니다. 나는이 배열을 통해 루프로 가능하고

WC_Cart::add_to_cart($product_id, $quantity, $variation_id, $variation); 

를 통해 장바구니에 항목을 추가하지만 데이터가 woocommerce_sessions 테이블에 저장 완전히 WooCommerce 호환 될 때 그것을 할 수있는 더 우아한 방법이 알아?

답변

1

나는 이것에 관해 다른 해결책을 찾지 못했기 때문에 장바구니를 먼저 지운 다음 장바구니 데이터로 루프를 돌아가 프로그래밍 방식으로 장바구니에 항목을 추가했습니다. 여기에 코드가 있습니다.

if ($cart_data) { 
    WC()->cart->empty_cart(); 

    foreach ($cart_data as $product) { 

     // Validate Product data 
     $product_id = isset($product['product_id']) ? (int) $product['product_id'] : 0; 
     $quantity  = isset($product['quantity'])  ? (int) $product['quantity']  : 1; 
     $variation_id = isset($product['variation_id']) ? (int) $product['variation_id'] : 0; 
     $variation = isset($product['variation'])  ? $product['variation']   : array(); 

     WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variation); 
    } 
    WC()->cart->calculate_totals(); 
} 
관련 문제