【问题标题】:WooCommerce get_attribute returning nullWooCommerce get_attribute 返回 null
【发布时间】:2017-09-22 21:19:56
【问题描述】:

我正在尝试获取 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;

【问题讨论】:

    标签: php wordpress woocommerce attributes product


    【解决方案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

    【讨论】:

    • 感谢您回答这个@LoicTheAztec。这帮助很大!我确实考虑了自定义字段路由,但是我不想向插件添加额外的依赖项,因为它将在许多站点中使用。
    猜你喜欢
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    相关资源
    最近更新 更多