WooCommerce get_attribute 返回 null

WooCommerce get_attribute returning null

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

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

在我的产品中,我设置了一个名称为 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;

您的代码大部分都能正常工作,只有 2 个小错误和一个冗余:

  • $postData 数组中缺少结尾 ]
  • 要获取产品 SKU,您应该使用 $product->get_sku() 而不是 $item_data['sku']
  • WC_Product 对象已在 $item_values->get_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;

使用自定义字段(备选)

Another way to do it, should be using custom fields instead of product attributes (if you don't need them to be displayed in the "Additional information" product tab).

怎么做: