如何排除将订单导出为 XML 文件的空标签?

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

我在插件中使用 PHP 代码

WP ALL EXPORT PRO
过滤掉 WooCommerce 中包含相关产品及相关详细信息和选项的特定订单,然后通过 XML / FTP 将其导出到我的履行合作伙伴。

每种产品的详细信息和选项的值都不同。因此,我需要一个代码片段来在 XML 文件中导出时通常排除空标签(关于下面概述中的粉红色标记区域)。

How to exclude empty tags

使用当前的 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**";
            $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**";
            $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**";
            $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**";
            $order_details .= "**LT**/Options**GT**";
            $order_details .= "**LT**/item**GT**";
        }
        return $order_details;
    }
?>
php xml wordpress export orders
1个回答
0
投票
  1. 添加到之前检查每个字段是否有内容 $order_details 字符串。

  2. 仅向 XML 字符串添加非空字段。

函数 my_get_order_items($order_id) { $order = wc_get_order(absint($order_id)); // 获取 WC_Order 对象

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 ID

    $order_details .= "**LT**item**GT**";
    
    // Conditionally add each field only if it's not empty
    if ($sku = $product->get_sku()) {
        $order_details .= "**LT**ID**GT**" . $sku . "**LT**/ID**GT**";
    }
    
    if ($name = $item->get_name()) {
        $order_details .= "**LT**produktname**GT**" . $name . "**LT**/produktname**GT**";
    }

    if ($quantity = $item->get_quantity()) {
        $order_details .= "**LT**amount**GT**" . $quantity . "**LT**/amount**GT**";
    }
    
    if ($upload = maybe_serialize(get_field('upload', $product_id))) {
        $order_details .= "**LT**upload**GT**" . $upload . "**LT**/upload**GT**";
    }

    if ($size = maybe_serialize(get_field('size', $product_id))) {
        $order_details .= "**LT**size**GT**" . $size . "**LT**/size**GT**";
    }

    if ($groesse = $product->get_attribute('pa_groesse')) {
        $order_details .= "**LT**groesse**GT**" . $groesse . "**LT**/groesse**GT**";
    }

    if ($material = maybe_serialize(get_field('material', $product_id))) {
        $order_details .= "**LT**material**GT**" . $material . "**LT**/material**GT**";
    }

    if ($print = maybe_serialize(get_field('print', $product_id))) {
        $order_details .= "**LT**print**GT**" . $print . "**LT**/print**GT**";
    }

    if ($variante = maybe_serialize(get_field('variante', $product_id))) {
        $order_details .= "**LT**variante**GT**" . $variante . "**LT**/variante**GT**";
    }

    if ($category = maybe_serialize(get_field('category', $product_id))) {
        $order_details .= "**LT**category**GT**" . $category . "**LT**/category**GT**";
    }
    
    // Add options conditionally
    $order_details .= "**LT**Options**GT**";
    
    if ($groupid_115 = maybe_serialize(get_field('groupid_115', $product_id))) {
        $order_details .= "**LT**Option**GT****LT**ID**GT**" . $groupid_115 . "**LT**/ID**GT**";
        $order_details .= "**LT**Value**GT**" . maybe_serialize(get_field('value_115', $product_id)) . "**LT**/Value**GT****LT**/Option**GT**";
    }
    
    if ($groupid_117 = maybe_serialize(get_field('groupid_117', $product_id))) {
        $order_details .= "**LT**Option**GT****LT**ID**GT**" . $groupid_117 . "**LT**/ID**GT**";
        $order_details .= "**LT**Value**GT**" . maybe_serialize(get_field('value_117', $product_id)) . "**LT**/Value**GT****LT**/Option**GT**";
    }
    
    if ($groupid_118 = maybe_serialize(get_field('groupid_118', $product_id))) {
        $order_details .= "**LT**Option**GT****LT**ID**GT**" . $groupid_118 . "**LT**/ID**GT**";
        $order_details .= "**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;

} ?>

每个字段仅在包含数据(即不为空)时才会添加。这可以防止空 XML 标签包含在导出的 XML 中。

每个选项组(例如,groupid_115、groupid_117、groupid_118)仅在有内容时才会添加。

此方法可确保最终 XML 输出中仅包含非空标签。

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