如何在管理订单列表的订单状态列中显示订单项目的元数据键和值?

huangapple go评论116阅读模式
英文:

How to display the meta key and values of the order item in the admin order list column order status?

问题

我正在使用WooCommerce,并使用来自automatic的付费插件“product addon”,该插件会将一些元数据添加到订单项目表中。

我想在管理员订单列表中显示2个或更多特定的元数据键值。

  • 1 Funghi
    Topup: Salami
    Oil: chilli

我已经使用以下代码使数量和产品名称正常工作,但订单项目元数据不正常,请问有什么建议吗?

add_action('manage_shop_order_posts_custom_column', 'orders_list_preview_items', 20, 2 );
function orders_list_preview_items($column, $post_id) {
    
    global $the_order, $post;
    
    if ('order_status' === $column) {
        
        // Start list
        echo '<ul class="orders-list-items-preview">';
        
        // Loop through order items
        foreach($the_order->get_items() as $item) {
            
            $product = $item->get_product();
         
            
            $name    = $item->get_name();
            $qty     = $item->get_quantity();
	    $meta    = $item->get_meta();
            
            echo "<li>
                
               <label>$qty</label> $name $meta
            </li>";
        }
        
        // End list
        echo '</ul>';
    }
    
    
}
英文:

I´m using Woocommerce with paid addon from automatic "product addon" which adds some meta values to the order item table

I want to show 2 or more specific meta key values in the admin order list.

  • 1 Funghi
    Topup: Salami
    Oil: chilli

I got the quantity and product name working with this code but not the order item meta,
any ideas?

add_action(&#39;manage_shop_order_posts_custom_column&#39;, &#39;orders_list_preview_items&#39;, 20, 2 );
function orders_list_preview_items($column, $post_id) {
    
    global $the_order, $post;
    
    if (&#39;order_status&#39; === $column) {
        
        // Start list
        echo &#39;&lt;ul class=&quot;orders-list-items-preview&quot;&gt;&#39;;
        
        // Loop through order items
        foreach($the_order-&gt;get_items() as $item) {
            
            $product = $item-&gt;get_product();
         
            
            $name    = $item-&gt;get_name();
            $qty     = $item-&gt;get_quantity();
	    $meta    = $item-&gt;get_meta();
            
            echo &quot;&lt;li&gt;
                
               &lt;label&gt;$qty&lt;/label&gt; $name $meta
            &lt;/li&gt;&quot;;
        }
        
        // End list
        echo &#39;&lt;/ul&gt;&#39;;
    }
    
    
}

答案1

得分: 0

你可以循环遍历项目的元数据,并检查你想要显示的元键。检查下面的代码。

add_action('manage_shop_order_posts_custom_column', 'orders_list_preview_items', 20, 2);
function orders_list_preview_items($column, $post_id){
    global $the_order, $post;

    if ('order_status' === $column) {
        // 开始列表
        echo '<ul class="orders-list-items-preview">';

        // 循环遍历订单项目
        foreach ($the_order->get_items() as $item) {
            $product = $item->get_product();

            $name = $item->get_name();
            $qty = $item->get_quantity();
            $meta = $item->get_meta();

            echo "<li>
                <label>$qty</label> $name";

            // 循环遍历项目的元数据
            foreach ($meta as $meta_key => $meta_value) {
                // 显示特定的元键值
                if (in_array($meta_key, ['Funghi Topup', 'Salami Oil', 'chilli'])) {
                    echo "<br>$meta_key: $meta_value";
                }
            }

            echo "</li>";
        }

        // 结束列表
        echo '</ul>';
    }
}
英文:

You can Loop through the item's metadata. and check for only the meta keys that you want to display. check the below code.

add_action(&#39;manage_shop_order_posts_custom_column&#39;, &#39;orders_list_preview_items&#39;, 20, 2);
function orders_list_preview_items($column, $post_id){
    global $the_order, $post;

    if (&#39;order_status&#39; === $column) {
        // Start list
        echo &#39;&lt;ul class=&quot;orders-list-items-preview&quot;&gt;&#39;;

        // Loop through order items
        foreach ($the_order-&gt;get_items() as $item) {
            $product = $item-&gt;get_product();

            $name = $item-&gt;get_name();
            $qty = $item-&gt;get_quantity();
            $meta = $item-&gt;get_meta();

            echo &quot;&lt;li&gt;
                &lt;label&gt;$qty&lt;/label&gt; $name&quot;;

            // Loop through the item&#39;s meta data
            foreach ($meta as $meta_key =&gt; $meta_value) {
                // Display specific meta key values
                if (in_array($meta_key, [&#39;Funghi Topup&#39;, &#39;Salami Oil&#39;, &#39;chilli&#39;])) {
                    echo &quot;&lt;br&gt;$meta_key: $meta_value&quot;;
                }
            }

            echo &quot;&lt;/li&gt;&quot;;
        }

        // End list
        echo &#39;&lt;/ul&gt;&#39;;
    }
}

答案2

得分: 0

Here is the translated code:

'add_action('manage_shop_order_posts_custom_column', orders_list_preview_items', 20, 2);
function orders_list_preview_items($column, $post_id) {
    global $the_order, $post;

    if ('order_status' === $column) {
        // Start list
        echo '<ul class="orders-list-items-preview">';

        // Loop through order items
        foreach ($the_order->get_items() as $item) {
            $product = $item->get_product();
            $name = $item->get_name();
            $qty = $item->get_quantity();
            $meta = $item->get_meta_data(); // Use get_meta_data() to retrieve item meta data

            echo "<li><label>$qty</label> $name ";

            // Display Topup and Oil meta values
            foreach ($meta as $meta_item) {
                if ($meta_item->key === 'Topup') {
                    echo "<span style='color: green; line-height: 1.5;'>{$meta_item->value}</span> ";
                }
                if ($meta_item->key === 'Oil') {
                    echo "<span style='color: brown; line-height: 1.5;'>{$meta_item->value}</span>";
                }
            }

            echo "</li>";
        }

        // End list
        echo '</ul>';
    }
}
code works ;-)
英文:
&#39;add_action(&#39;manage_shop_order_posts_custom_column&#39;, orders_list_preview_items&#39;, 20, 2);

function orders_list_preview_items($column, $post_id) {
global $the_order, $post;

if (&#39;order_status&#39; === $column) {
    // Start list
    echo &#39;&lt;ul class=&quot;orders-list-items-preview&quot;&gt;&#39;;

    // Loop through order items
    foreach ($the_order-&gt;get_items() as $item) {
        $product = $item-&gt;get_product();
        $name = $item-&gt;get_name();
        $qty = $item-&gt;get_quantity();
        $meta = $item-&gt;get_meta_data(); // Use get_meta_data() to retrieve item meta data

        echo &quot;&lt;li&gt;&lt;label&gt;$qty&lt;/label&gt; $name &quot;;

        // Display Topup and Oil meta values
        foreach ($meta as $meta_item) {
            if ($meta_item-&gt;key === &#39;Topup&#39;) {
                echo &quot;&lt;span style=&#39;color: green; line-height: 1.5;&#39;&gt;{$meta_item-&gt;value}&lt;/span&gt; &quot;;
            }
            if ($meta_item-&gt;key === &#39;Oil&#39;) {
                echo &quot;&lt;span style=&#39;color: brown; line-height: 1.5;&#39;&gt;{$meta_item-&gt;value}&lt;/span&gt;&quot;;
            }
        }

        echo &quot;&lt;/li&gt;&quot;;
    }

    // End list
    echo &#39;&lt;/ul&gt;&#39;;
}

}
code works 如何在管理订单列表的订单状态列中显示订单项目的元数据键和值?

huangapple
  • 本文由 发表于 2023年7月18日 14:14:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定