在WooCommerce管理员订单列表中显示每个订单的变体名称

祖兹

我很难在每个订单的管理订单列表中显示变体名称。

我尝试了这段代码,但它给了我一个错误:

// Get an instance of the WC_Order object from an Order ID
$order = wc_get_order( $order_id ); 

// Loop though order "line items"
foreach( $order->get_items() as $item_id => $item_product ){
    $product_id = $item_product->get_product_id(); //Get the product ID
    $quantity = $item_product->get_quantity(); //Get the product QTY
    $product_name = $item_product->get_name(); //Get the product NAME

    // Get an instance of the WC_Product object (can be a product variation  too)
    $product = $item_product->get_product();

    // Get the product description (works for product variation)
    $description = $product->get_description();

    // Only for product variation
    if($product->is_type('variation')){
        // Get the variation attributes
        $variation_attributes = $product->get_variation_attributes();

        // Loop through each selected attributes
        foreach($variation_attributes as $attribute_taxonomy => $term_slug){
            $taxonomy = str_replace('attribute_', '', $attribute_taxonomy );

            // The name of the attribute
            $attribute_name = get_taxonomy( $taxonomy )->labels->singular_name;

            // The term name (or value) for this attribute
            $attribute_value = get_term_by( 'slug', $term_slug, $taxonomy )->name;
        }
    }
}
7uc1f3r

要将变体添加到中admin order list,我创建了一个新列。

以下代码不包含错误,并且可以根据需要进行进一步的调整

// Add a Header
function custom_shop_order_column( $columns ) {
    // Add new column
    $columns['variation_name'] = 'Variation name';

    return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 10, 1 );

// Populate the Column
function custom_shop_order_list_column_content( $column, $post_id ) {   
    // Compare
    if ( $column == 'variation_name' ) {

        // Get an instance of the WC_Order object from an Order ID
        $order = wc_get_order( $post_id );

        // Loop though order "line items"
        foreach( $order->get_items() as $item ) {
            // Get an instance of the WC_Product object (can be a product variation  too)
            $product = $item->get_product();

            // Only for product variation
            if( $product->is_type( 'variation' ) ) {

                $product_id = $item->get_product_id(); //Get the product ID
                $quantity = $item->get_quantity(); //Get the product QTY
                $product_name = $product->get_name(); //Get the product NAME

                // Get the product description (works for product variation)
                $description = $product->get_description();

                // Get the variation attributes
                $variation_attributes = $product->get_variation_attributes();

                // Loop through all product attributes in the variation
                foreach ( $variation_attributes as $variation_attribute => $term_slug ) {
                    echo $term_slug . '<br>';   

                    // Taxonomy
                    $taxonomy = str_replace('attribute_', '', $variation_attribute);

                    // The WP_Term object
                    $term = get_term_by( 'slug', $term_slug, $taxonomy );
                }
            }
        }
    }
}
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_list_column_content', 10, 2 );

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Woocommerce管理员订单列表中显示带有作者和日期的订单注释

在Woocommerce管理员订单列表自定义列中显示结帐字段值

在Woocommerce管理员订单列表自定义列中显示用户名

仅在WooCommerce管理员订单列表自定义列中显示特定订单状态的数据

在管理员-> Woocommerce->订单的“订单”列表中显示运输邮政编码。

如何在管理员销售订单列表中获得关联名称?

在Woocommerce管理员订单列表“订单”现有列中添加自定义字段

Woocommerce 3.3管理员订单列表中的自定义订单状态背景按钮颜色

在WooCommerce管理员订单列表中以批量操作添加自定义取消订单状态

在 WooCommerce 我的帐户订单列表中添加发送给客户的管理员订单备注

在优惠券设置中添加一个字段,并在Woocommerce管理员订单列表上显示该值

如何获取用户ID以在android studio中执行显示列表以供管理员查看订单列表?

在 WooCommerce 管理订单列表的自定义列中显示每个订单项目的库存数量

在Woocommerce管理员订单列表上处理多个自定义批量操作

更改Woocommerce管理员订单列表的时间和日期格式

从订单预览中删除WooCommerce管理员订单操作

将自定义订单状态添加到WooCommerce管理员订单列表中的过滤器菜单

如何在WooCommerce管理员订单列表中的买家姓名之后添加第一笔订单消息

在客户订单历史记录中显示最后的WooCommerce管理员订单记录

在Woocommerce管理员“编辑订单”页面中显示客户取消的订单数

在 WooCommerce 的管理员新订单电子邮件通知中显示订单下载

在WooCommerce 3.3的管理订单列表中显示回购订单注释

在Woocommerce管理订单列表中显示具有“全部”自定义状态的订单

WooCommerce在管理员订单详细信息概述中显示供应商商店名称(Dokan)

在Woocommerce的管理员编辑订单页面上显示每个特定订单的所有可用送货方式

在WooCommerce管理员订单预览上显示供应商商店名称(Dokan)

根据订单状态隐藏 WooCommerce 管理订单列表中的订单(表格行)

在 WooCommerce 管理订单列表的自定义列中显示私人和客户管理注释

在Woocommerce管理员,订单和电子邮件中显示自定义付款字段