使用 WP ALL EXPORT 单独导出 WooCommerce 订单的自定义代码

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

我使用导出插件 “WP ALl IMPORT” 从 Woocommerce 导出某些订单。

目标: 每个订单必须通过 FTP 作为 XML 文件单独发送。

当前状态: 使用我使用的相关产品准确筛选正确的订单 插件部分中的自定义代码:

XML 编辑器

<?xml version="1.0" encoding="UTF-8"?>
<order>
    <!-- BEGIN LOOP -->
        <commission>{Bestell ID}</commission>
        <production>1</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>
    <!-- END LOOP -->
</order>

功能 PHP 编辑器

<?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(), 'SWEATSHIRT') !== false
        || strpos($item->get_name(), 'HOODIE') !== 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 object

        $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**" . $product->get_attribute('pa_groesse')  . "**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**";

        if(get_field( 'groupid_115', $product_id )) {             
             $order_details .= "**LT**Option**GT****LT**ID**GT**" . maybe_serialize( get_field( 'groupid_115', $product_id ) ) . "**LT**/ID**GT****LT**Value**GT**" . maybe_serialize( get_field( 'value_115', $product_id ) ) . "**LT**/Value**GT****LT**/Option**GT**"; 
        }
        
        if(get_field( 'groupid_117', $product_id )) {             
             $order_details .= "**LT**Option**GT****LT**ID**GT**" . maybe_serialize( get_field( 'groupid_117', $product_id ) ) . "**LT**/ID**GT****LT**Value**GT**" . maybe_serialize( get_field( 'value_117', $product_id ) ) . "**LT**/Value**GT****LT**/Option**GT**"; 
        }
        
        if(get_field( 'groupid_118', $product_id )) {             
             $order_details .= "**LT**Option**GT****LT**ID**GT**" . maybe_serialize( get_field( 'groupid_118', $product_id ) ) . "**LT**/ID**GT****LT**Value**GT**" . maybe_serialize( get_field( 'value_118', $product_id ) ) . "**LT**/Value**GT****LT**/Option**GT**"; 
        }
    } 
    return $order_details;
}
?>

日程安排: 插件的“手动调度”功能使用 cronjobs 按时间间隔通过 FTP 导出相关订单。

要解决的问题: 不幸的是,每个订单都不会单独导出为 XML 文件,而是将时间间隔内的所有订单一起导出到一个 XML 文件中。

问题: 能解决吗?有没有办法修改我现有的自定义代码,以便每个订单都可以单独导出? 你有什么编码想法吗?

截图:

Generated xml

php woocommerce orders wpallimport export-to-xml
1个回答
0
投票

您的代码中缺少的是每个订单的一些结束标签,这些标签涉及代码末尾的商品详细信息和商品选项。

此外,您应该在每个订单至少获得一个 ACF 字段选项之前进行检查。

尝试以下操作:

<?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

    // 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(), 'SWEATSHIRT') !== false
        || strpos($item->get_name(), 'HOODIE') !== 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 object

        // Item start
        $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**" . $product->get_attribute('pa_groesse')  . "**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**";

        $order_details .= "**LT**category**GT**" . maybe_serialize( get_field( 'category', $product_id ) ) . "**LT**/category**GT**";

        // Get your options values first
        $groupid_115 = get_field( 'groupid_115', $product_id );
        $groupid_117 = get_field( 'groupid_115', $product_id );
        $groupid_118 = get_field( 'groupid_115', $product_id );

        // Options start
        if ( $groupid_115 || $groupid_117 || $groupid_118 ) {
            $order_details .= "**LT**Options**GT**";

            if ( $groupid_115 ) { 
                $groupid_115    =  maybe_serialize( $groupid_115 );           
                $order_details .= "**LT**Option**GT****LT**ID**GT**" . $groupid_115 . "**LT**/ID**GT****LT**Value**GT**" . $groupid_115 . "**LT**/Value**GT****LT**/Option**GT**"; 
            }
        
            if ( $groupid_117 ) {
                $groupid_117    =  maybe_serialize( $groupid_117 );          
                $order_details .= "**LT**Option**GT****LT**ID**GT**" . $groupid_117 . "**LT**/ID**GT****LT**Value**GT**" . $groupid_117 . "**LT**/Value**GT****LT**/Option**GT**";
            }
        
            if ( $groupid_118 ) { 
                $groupid_118    =  maybe_serialize( $groupid_118 );      
                $order_details .= "**LT**Option**GT****LT**ID**GT**" . $groupid_118 . "**LT**/ID**GT****LT**Value**GT**" . $groupid_118 . "**LT**/Value**GT****LT**/Option**GT**"; 
            }
            // Closing tag for "Options" (Options end)
            $order_details .= "**LT/**Options**GT**"; 
        }
        // Closing tag for "item" (item end)
        $order_details .= "**LT/**item**GT**"; 
    }
    
    return $order_details;
}
?>

未经测试,应该可以解决您的问题。

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