在 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;

        }

    }

}


catspeake
浏览 124回答 1
1回答

侃侃无极

为了向 中添加变体admin order list,我创建了一个新列。以下代码不包含任何错误,并且可以根据需要进行进一步调整// Add a Headerfunction custom_shop_order_column( $columns ) {&nbsp; &nbsp; // Add new column&nbsp; &nbsp; $columns['variation_name'] = 'Variation name';&nbsp; &nbsp; return $columns;}add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 10, 1 );// Populate the Columnfunction custom_shop_order_list_column_content( $column, $post_id ) {&nbsp; &nbsp;&nbsp; &nbsp; // Compare&nbsp; &nbsp; if ( $column == 'variation_name' ) {&nbsp; &nbsp; &nbsp; &nbsp; // Get an instance of the WC_Order object from an Order ID&nbsp; &nbsp; &nbsp; &nbsp; $order = wc_get_order( $post_id );&nbsp; &nbsp; &nbsp; &nbsp; // Loop though order "line items"&nbsp; &nbsp; &nbsp; &nbsp; foreach( $order->get_items() as $item ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get an instance of the WC_Product object (can be a product variation&nbsp; too)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product = $item->get_product();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Only for product variation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( $product->is_type( 'variation' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product_id = $item->get_product_id(); //Get the product ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $quantity = $item->get_quantity(); //Get the product QTY&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product_name = $product->get_name(); //Get the product NAME&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get the product description (works for product variation)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $description = $product->get_description();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get the variation attributes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $variation_attributes = $product->get_variation_attributes();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Loop through all product attributes in the variation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ( $variation_attributes as $variation_attribute => $term_slug ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $term_slug . '<br>';&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Taxonomy&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $taxonomy = str_replace('attribute_', '', $variation_attribute);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // The WP_Term object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $term = get_term_by( 'slug', $term_slug, $taxonomy );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_list_column_content', 10, 2 );
打开App,查看更多内容
随时随地看视频慕课网APP