获取可变产品的 WooCommerce 订单项目 ACF 字段值

问题描述 投票:0回答:1

我尝试了多种方法从 WooCommerce 中筛选出包含相关简单和可变产品(带有 ACF 信息)的特定订单,然后通过 XML 将它们导出到我的履行合作伙伴。

为此,我使用 WP ALL EXPORT PRO 和 ACF 插件以及自定义代码:

用于 WP ALL 导出插件的 XML 编辑器

<?xml version="1.0" encoding="UTF-8"?>
<orders>
    <!-- BEGIN LOOP -->
    <order>
        <commission>{Bestell ID}</commission>
        <production>2</production>

        <receiver>
            <line1>{Shipping First Name} {Shipping Last Name}</line1>
            <line2>{Shipping Company}</line2>
            <street>{Shipping Address 1}</street>
            <streetnumber>{Shipping Address 2}</streetnumber>
            <country_code>{Shipping Country}</country_code>
            <zip>{Shipping Postcode}</zip>
            <city>{Shipping City}</city>
            <email>{Customer Account Email Address}</email>

        </receiver> 

        <items> 
            [my_get_order_items({Bestell ID})]
        </items>
    </order>
    <!-- END LOOP -->
</orders>

用于WP ALL导出插件的功能PHP编辑器

function my_get_order_items($Bestell_id) {
    // Check if the Bestell ID is valid
    if (!$Bestell_id) {
        return false;
    }
    
    // Fetch the order
    $order = wc_get_order($Bestell_id);
    if (!$order) {
        return false;
    }

    // Initialize the string to store order details
    $order_details = "";

    // Loop through each order item
    foreach ($order->get_items() as $item_id => $item) {
        // Get the product object
        $product = $item->get_product();

        // Skip if there's no product associated with the item
        if (!$product) 
        { continue; }
        
        
        if (!(
strpos($item->get_name(), 'KERAMIKTASSE') !== false ||
strpos($item->get_name(), 'BAUMWOLLTASCHE') !== false ||
strpos($item->get_name(), 'T-SHIRT') !== false))
{ continue; }
        
        $order_details .= "**LT**item**GT**";
        $order_details .= "**LT**ID**GT**" . $product->get_sku() . "**LT**/ID**GT**";
        $order_details .= "**LT**produktname**GT**" . $item->get_name() . "**LT**/produktname**GT**";
        $order_details .= "**LT**amount**GT**" . $item->get_quantity() . "**LT**/amount**GT**";
        $order_details .= "**LT**upload**GT**" . maybe_serialize( get_field( 'upload', $product->get_id() ) ) . "**LT**/upload**GT**";
        $order_details .= "**LT**size**GT**" . maybe_serialize( get_field( 'size', $product->get_id() ) ) . "**LT**/size**GT**";
        $order_details .= "**LT**groesse**GT**" . maybe_serialize( get_field( 'produktgröße', $product->get_id() ) )  . "**LT**/groesse**GT**";
        $order_details .= "**LT**material**GT**" . maybe_serialize( get_field( 'material', $product->get_id() ) ) . "**LT**/material**GT**";
        $order_details .= "**LT**print**GT**" . maybe_serialize( get_field( 'print', $product->get_id() ) ) . "**LT**/print**GT**";
        $order_details .= "**LT**variante**GT**" . maybe_serialize( get_field( 'variante', $product->get_id() ) ) . "**LT**/variante**GT**";
        $order_details .= "**LT**category**GT**" . maybe_serialize( get_field( 'category', $product->get_id() ) ) . "**LT**/category**GT**";
        
        //add options to the output
        $order_details .= "**LT**Options**GT**";
            $order_details .= "**LT**Option**GT****LT**ID**GT**" . "1996" . "**LT**/ID**GT****LT**Value**GT**" . maybe_serialize( get_field( 'value_115', $product->get_id() ) ) . "**LT**/Value**GT****LT**/Option**GT**";
        
            $order_details .= "**LT**Option**GT****LT**ID**GT**" . "2489" . "**LT**/ID**GT****LT**Value**GT**" . maybe_serialize( get_field( 'value_117', $product->get_id() ) ) . "**LT**/Value**GT****LT**/Option**GT**";
        
            $order_details .= "**LT**Option**GT****LT**ID**GT**" . "2056" . "**LT**/ID**GT****LT**Value**GT**" . maybe_serialize( get_field( 'value_118', $product->get_id() ) ) . "**LT**/Value**GT****LT**/Option**GT**";
        
        $order_details .= "**LT**/Options**GT**";
        
        $order_details .= "**LT**/item**GT**";
        
    }

    return $order_details;
}

到目前为止,此代码运行良好,所有相关订单和所有相关产品都被过滤。我唯一的问题是只导出简单产品的ACF字段信息。 对于可变产品,所有 ACF 字段均保留为空。

SEE OVERVIEW

我如何最终通过 xml 传输可变产品 + 订购的产品变体 – 类似于“简单产品”?

我如何更改我的 PHP 代码和/或 ACF 设置?

php xml woocommerce advanced-custom-fields wpallimport
1个回答
0
投票

要解决 ACF 字段为空的可变产品问题,需要使用带有 ACF get_field() 功能的

父变量产品 ID
,而不是产品变体 ID。

此外,你的代码中还有一些不必要的东西。

尝试以下 PHP 替换代码:

function my_get_order_items( $order_id ) {
    $order = wc_get_order( absint($order_id) ); // Get the WC_Order object
    
    if ( ! is_a($order, 'WC_Order') ) {
        return false; // Exit if not an order
    }
    $order_details = ""; // Initialize variable as string to store order details

    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        if ( ! ( strpos($item->get_name(), 'KERAMIKTASSE') !== false
        || strpos($item->get_name(), 'BAUMWOLLTASCHE') !== false
        || strpos($item->get_name(), 'T-SHIRT') !== false ) ) { 
            continue; 
        }
        $product    = $item->get_product(); // Get the product object
        $product_id = $item->get_product_id(); // Get the product ID or the parent product ID

        $order_details .= "**LT**item**GT**";
        $order_details .= "**LT**ID**GT**" . $product->get_sku() . "**LT**/ID**GT**";
        $order_details .= "**LT**produktname**GT**" . $item->get_name() . "**LT**/produktname**GT**";
        $order_details .= "**LT**amount**GT**" . $item->get_quantity() . "**LT**/amount**GT**";
        $order_details .= "**LT**upload**GT**" . maybe_serialize( get_field( 'upload', $product_id ) ) . "**LT**/upload**GT**";
        $order_details .= "**LT**size**GT**" . maybe_serialize( get_field( 'size', $product_id ) ) . "**LT**/size**GT**";
        $order_details .= "**LT**groesse**GT**" . maybe_serialize( get_field( 'produktgröße', $product_id ) )  . "**LT**/groesse**GT**";
        $order_details .= "**LT**material**GT**" . maybe_serialize( get_field( 'material', $product_id ) ) . "**LT**/material**GT**";
        $order_details .= "**LT**print**GT**" . maybe_serialize( get_field( 'print', $product_id ) ) . "**LT**/print**GT**";
        $order_details .= "**LT**variante**GT**" . maybe_serialize( get_field( 'variante', $product_id ) ) . "**LT**/variante**GT**";
        $order_details .= "**LT**category**GT**" . maybe_serialize( get_field( 'category', $product_id ) ) . "**LT**/category**GT**";

        //add options to the output
        $order_details .= "**LT**Options**GT**";
        $order_details .= "**LT**Option**GT****LT**ID**GT**" . "1996" . "**LT**/ID**GT****LT**Value**GT**" . maybe_serialize( get_field( 'value_115', $product_id ) ) . "**LT**/Value**GT****LT**/Option**GT**";
        $order_details .= "**LT**Option**GT****LT**ID**GT**" . "2489" . "**LT**/ID**GT****LT**Value**GT**" . maybe_serialize( get_field( 'value_117', $product_id ) ) . "**LT**/Value**GT****LT**/Option**GT**";
        $order_details .= "**LT**Option**GT****LT**ID**GT**" . "2056" . "**LT**/ID**GT****LT**Value**GT**" . maybe_serialize( get_field( 'value_118', $product_id ) ) . "**LT**/Value**GT****LT**/Option**GT**";
        $order_details .= "**LT**/Options**GT**";
        $order_details .= "**LT**/item**GT**";
    }
    return $order_details;
}

现在,可变产品上的 ACF 字段不应再为空。

© www.soinside.com 2019 - 2024. All rights reserved.