2016-06-10 4 views
0

나는 WooCommerce의 계산대에서 장바구니에있는 항목의 카테고리를 가져오고 싶습니다. 추출한 다음 사용자 정의 계산대의 필드에 넣고 싶습니다.WooCommerce에서 결제시 주문에서 카테고리를 얻는 방법?

내가 WooCommerce MultiStep Checkout Wizard 프리미엄 플러그인과 specific hook 사용하고 있습니다 :

add_action('woocommerce_multistep_checkout_before_order_info', 'destinationStep'); 

내가 조금 잃었어요 훨씬 documentation 내가 그걸 얻기 위해 사용해야하는 것에 대해를 찾을 수 있습니다.

나는 항목을 표시하려고하고 있지만 빈 배열이 나타납니다.

$order = new WC_Order($order_id); 
$items = $order->get_items(); 
var_dump($items); 

답변

1

당신은 당신의 접근 방식을 처음 시도 할 수 "new WC_Order($order_id);",이 방법 :

function destinationStep($order_id) 

    global $woocommerce; 

    $order = new WC_Order($order_id); 
    $items = $order->get_items(); 
    // echo var_dump($items); 

    //---- 
    foreach ($items as $key => $item) { 
     $product_name = $item['name']; 
     $product_id = $item['product_id']; 
     $terms = get_the_terms($product_id, 'product_cat'); 
     // echo var_dump($terms); 

     foreach ($terms as $term) { 
      // Categories by slug 
      $product_cat_slug= $term->slug; 
     } 
    } 

add_action('woocommerce_multistep_checkout_before_order_info', 'destinationStep', 10, 1); 

여전히 "new WC_Order($post->ID)" 접근 방식을 시도 작동하지 않는 경우 :

function destinationStep() 

    global $woocommerce, $post; 

    $order = new WC_Order($post->ID); 
    $items = $order->get_items(); 
    // echo var_dump($items); 

    //---- 
    foreach ($items as $key => $item) { 
     $product_name = $item['name']; 
     $product_id = $item['product_id']; 
     $terms = get_the_terms($product_id, 'product_cat'); 
     // echo var_dump($terms); 

     foreach ($terms as $term) { 
      // Categories by slug 
      $product_cat_slug= $term->slug; 
     } 
    } 

add_action('woocommerce_multistep_checkout_before_order_info', 'destinationStep'); 

업데이트 - 몇 가지 생각 후 :

당신 아직 존재하지 않기 때문에, ''post_type '=>'shop_order '에 대한 주문 ID를 얻을 수 없습니다. 이 주문 ID는 고객이 주문을 제출할 때 생성되지만 결제 전 페이지에는 생성되지 않습니다.
그래서이 경우 빈 배열을 얻는 것이 정상입니다.

+0

안녕하십니까, 죄송합니다. 답장을 보내 주시겠습니까? 나는 처음에는 코드의 첫 번째 비트만을 사용하고 var 덤프를 얻으려고한다. 두 var 덤프 모두 빈 배열입니다. 어떤 충고? 우리가 이걸 갈 수 있다면 나는 데킬라를 사줄거야. –

+0

그래, 그걸 시도해 봤어. 루프가없는 첫 번째 부분. 그냥 빈 배열. –

+0

그냥 다른 사람이 읽은 것입니다 도움을 upvoted, 그것은 나를 위해 일하는 것이 아닙니다. –