Orko22
Orko22

Reputation: 55

WP ALL EXPORT: How to modify custom Code for exporting orders seperately

I use the export plugin “WP ALl IMPORT” to export certain orders from Woocommerce.

GOAL: Each order must be sent individually as an XML file via FTP.

CURRENT STATUS: To filter exactly the right orders with relevant products I use custom code in the plugin sections:

XML EDITOR

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

FUNCTION PHP EDITOR

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

?>

SCHEDULING: The “Manual Scheduling” function of the plugin exports relevant orders via FTP in a time interval using cronjobs.

PROBLEM TO SOLVE: Unfortunately, each order is not exported individually as an XML file, but all orders in the time interval are exported together in one XML FILE.

QUESTION: Can it be solved? Is there a way to modify my existing custom codes so that each order can be exported individually? Do you have any coding ideas?

Upvotes: 0

Views: 53

Answers (0)

Related Questions