2017-09-22 10 views
1

私は2つの特定のカスタム商品属性の値を取得しようとしていますが、値を返すことはできません。WooCommerce get_attribute returns null

以下は、この問題とは関係がないため、私のコードのトリムダウンバージョンです。

私の製品では、carrierの名前とAA12345という名前のカスタム属性が設定されています。また、BB67890という値を持つ属性templateを設定しました。ここで間違っていることは本当に分かりません。

foreach($order-> get_items() as $item_key => $item_values): 

    $item_id = $item_values->get_id(); 
    $item_name = $item_values->get_name(); 
    $item_type = $item_values->get_type(); 
    $product_id = $item_values->get_product_id(); // the Product id 
    $wc_product = $item_values->get_product(); // the WC_Product object 
    ## Access Order Items data properties (in an array of values) ## 
    $item_data = $item_values->get_data(); 
    $product_name = $item_data['name']; 
    $product_id = $item_data['product_id']; 
    $variation_id = $item_data['variation_id']; 
    $quantity = $item_data['quantity']; 
    $tax_class = $item_data['tax_class']; 
    $line_subtotal = $item_data['subtotal']; 
    $line_subtotal_tax = $item_data['subtotal_tax']; 
    $line_total = $item_data['total']; 
    $line_total_tax = $item_data['total_tax']; 
    $order_id = $item_data['order_id']; 

    if ($product_id == 37) { 
    } else if ($product_id == 50) { 
    // Get Poduct attributes 
    $p = wc_get_product($product_id); 
    $patt_carrier = $p->get_attribute('carrier'); 
    $patt_template = $p->get_attribute('template'); 
     $product_type = "PICKANDPACK"; 
     $postData['bundles'][] = [ 
      'type' => $product_type, 
      'items' => [ 
       'bom' => [ 
       [ 
        'type' => 'CARD', 
        'stockId' => $item_data['sku'], 
        [ 
         'type' => 'CARRIER', 
         'quantity' => '1', 
         'stockId' => $patt_carrier, 
         'metadata' => [ 
          'template' => $patt_template, 
         ] 
        ], 
        ], 
       ], 


     ]; 
    } 
endforeach; 

答えて

1

あなたのコードはほとんど働いていた、わずか2小さなエラーと冗長性があった:

  • 不足している製品を得るためにあなたの$postData配列、
  • ]を閉じるSKUは、あなたが$product->get_sku()を使用する必要があります$item_data['sku']の代わりに
  • WC_Productオブジェクトにはすでに$item_values->get_product()がありました。

商品の属性値を取得する部分が正しいです。

だからあなたの作業(テスト)のコードは次のようになります。CUSTOM FIELDS(代替)

それを行う別の方法、使用する必要がありますカスタムを使用した

foreach($order-> get_items() as $item_key => $item_values): 
    $product_id = $item_values->get_product_id(); // the product id 
    $product = $item_values->get_product(); // <== the WC_Product object 
    $product_sku = $product->get_sku(); // <== the product sku 

    if ($product_id == 37) { 
    } else if ($product_id == 50) { 

     // Get Poduct attributes (==> working) 
     $patt_carrier = $product->get_attribute('carrier'); 
     $patt_template = $product->get_attribute('template'); 

     $product_type = "PICKANDPACK"; 
     $postData['bundles'][] = [ 
      'type' => $product_type, 
      'items' => [ 
       'bom' => [ 
        [ 
         'type' => 'CARD', 
         'stockId' => $product_sku, // <== The sku 
         [ 
          'type' => 'CARRIER', 
          'quantity' => '1', 
          'stockId' => $patt_carrier, 
          'metadata' => [ 
           'template' => $patt_template, 
          ] 
         ], 
        ], 
       ], 
      ], // <== one was missing 
     ]; 
    } 
     // Testing the raw output 
     echo '<pre>'; print_r($postData); echo '</pre>'; 
endforeach; 

(「追加情報」の商品タブに表示する必要がない場合は)商品属性の代わりにそれを行うにはどのように

Add custom fields to WooComerce product setting pages in the shipping tab

+0

この@LoicTheAztecに答えるいただきありがとうございます。これはすごく助けになった!私はカスタムフィールドのルートを考慮しましたが、多くのサイトで使用されるようにプラグインに余分な依存関係を追加したくありませんでした。 –