Add product short description to Woocommerce admin orders preview

前端 未结 2 988
误落风尘
误落风尘 2021-01-15 02:55

I\'m trying to add my product short description into my order page as a new tab so we have an easier way to order products without having to go inside of them.

I see

相关标签:
2条回答
  • 2021-01-15 03:42

    @LoicTheAztec answer does not work for me, i modified his answer and replace the custom_admin_order_item_values function with below, then it work.

    // Custom column content in the order "line items" html table
    function custom_admin_order_item_values( $item_name, $item, $is_visible ){
        $product_id = $item->get_product_id(); // Get the product Id
        $excerpt = get_the_excerpt( $product_id ); // Get the short description
        // Output
        echo '<td class="line_custom-description">' . $excerpt . '</td>';
    }
    
    0 讨论(0)
  • 2021-01-15 03:50

    Try the following instead (Code is commented):

    // Add a custom column to the order "line items" html table
    add_action( 'woocommerce_admin_order_item_headers', 'custom_admin_order_items_headers', 20, 1 );
    function custom_admin_order_items_headers( $order ){
    
        echo '<th class="line_custom-title sortable" data-sort="your-sort-option">';
        echo __('Short description', 'woocommerce') . '</th>';
    }
    
    // Custom column content in the order "line items" html table
    add_action( 'woocommerce_admin_order_item_values', 'custom_admin_order_item_values', 20, 3 );
    function custom_admin_order_item_values( $_product, $item, $item_id ) {
        // Only for "line_item" items type
        if( ! $item->is_type('line_item') ) return;
    
        // For product variation, we get the parent variable product (in case of)
        if( $_product->is_type('variation') ){
            $parent_product = $_product->get_parent();
            // The product variation description (as short description doesn't exist)
            $excerpt        = $_product->get_description(); 
            // If product variation description doesn't exist we display the short description of the parent variable product
            $excerpt        = empty($excerpt) ? $parent_product->get_short_description() : $excerpt;
        }
        // For other product types
        else {
            $excerpt        = $_product->get_short_description();
        }
    
        // Output
        echo '<td class="line_custom-description">' . $excerpt . '</td>';
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works

    0 讨论(0)
提交回复
热议问题