WooCommerce管理订单列表中的变量名称

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

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

我试过这个代码,但它给我一个错误。

// 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;
        }
    }
}
php woocommerce backend orders
1个回答
0
投票

要添加变化到 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 );
© www.soinside.com 2019 - 2024. All rights reserved.