WooCommerce get_attribute返回null

时间:2017-09-22 21:19:56

标签: php wordpress woocommerce attributes product

我正在尝试获取2个特定自定义产品属性的值,但是我无法返回任何值。

以下是我的代码的精简版本,因为很多与此问题无关。

在我的产品中,我设置了名为carrier的自定义属性和值AA12345。我还将属性template设置为值BB67890。我真的不确定我在这里做错了什么。

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 个答案:

答案 0 :(得分:1)

您的代码大部分都在运行,只有2个小错误和冗余:

  • ]数组中缺少结束$postData
  • 要获得产品SKU,您应使用$product->get_sku()代替$item_data['sku']
  • $item_values->get_product()
  • 中已经提供了WC_Product对象

获取产品属性值的部分是正确的。

所以你的工作(测试)代码应该是:

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