【问题标题】:How to get "items" column in orders panel in WooCommerce如何在 WooCommerce 的订单面板中获取“项目”列
【发布时间】:2017-05-01 12:25:17
【问题描述】:

自 WooCommerce 主要更新 3.0+ 起,后端订单列表面板中的“已购买”列已被删除。此列之前显示了按顺序显示的项目切换列表,以便快速查看。

如何取回订单面板中的“商品”栏?

如果有什么钩子呢?有什么想法吗?

谢谢

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    出于性能原因,这似乎是 removed,但您可以查看已删除的代码,然后通过 manage_shop_order_posts_columns 过滤器和 manage_shop_order_posts_custom_column 操作将其添加回来。

    /**
     * Modify the custom columns for orders.
     * @param  array $columns
     * @return array
     */
    function so_43719068_shop_order_columns( $columns ) {
    
        // the new column as an array for subsequent array manip
        $new_column = array( 'order_items' => __( 'Purchased', 'your-plugin' ) );
    
        $insert_after = 'order_title';
    
        // insert after specified column
        if( isset( $columns[ $insert_after ] ) ){
    
            // find the "title" column
            $index =  array_search( $insert_after, array_keys( $columns) );
    
            // reform the array
            $columns = array_merge( array_slice( $columns, 0, $index + 1, true ), $new_column, array_slice( $columns, $index, count( $columns ) - $index, true ) );
    
        // or add to end
        } else {
            $columns = array_merge( $columns, $new_column );
        }
    
        return $columns;
    }
    add_filter( 'manage_shop_order_posts_columns', 'so_43719068_shop_order_columns', 20 );
    
    
    
    /**
     * Output custom columns for orders.
     * @param string $column
     * @param int $post_id
     */
    function so_43719068_render_shop_order_columns( $column, $post_id ) {
        global $the_order;
    
        if ( empty( $the_order ) || $the_order->get_id() !== $post_id ) {
            $the_order = wc_get_order( $post_id );
        }
    
        switch ( $column ) :
    
            case 'order_items' :
    
                /* translators: %d: order items count */
                echo '<a href="#" class="show_order_items">' . apply_filters( 'woocommerce_admin_order_item_count', sprintf( _n( '%d item', '%d items', $the_order->get_item_count(), 'woocommerce' ), $the_order->get_item_count() ), $the_order ) . '</a>';
    
                if ( sizeof( $the_order->get_items() ) > 0 ) {
    
                    echo '<table class="show_order_items" cellspacing="0">';
    
                    foreach ( $the_order->get_items() as $item ) {
                        $product        = apply_filters( 'woocommerce_order_item_product', $item->get_product(), $item );
                        $item_meta_html      = wc_display_item_meta( $item, array( 'echo' => false ) );
                        ?>
                        <tr class="<?php echo apply_filters( 'woocommerce_admin_order_item_class', '', $item, $the_order ); ?>">
                            <td class="qty"><?php echo esc_html( $item->get_quantity() ); ?></td>
                            <td class="name">
                                <?php  if ( $product ) : ?>
                                    <?php echo ( wc_product_sku_enabled() && $product->get_sku() ) ? $product->get_sku() . ' - ' : ''; ?><a href="<?php echo get_edit_post_link( $product->get_id() ); ?>"><?php echo apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ); ?></a>
                                <?php else : ?>
                                    <?php echo apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ); ?>
                                <?php endif; ?>
                                <?php if ( ! empty( $item_meta_html ) ) : ?>
                                    <?php echo wc_help_tip( $item_meta_html ); ?>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php
                    }
    
                    echo '</table>';
    
                } else echo '&ndash;';
            break;
    
        endswitch;
    
    }
    add_action( 'manage_shop_order_posts_custom_column', 'so_43719068_render_shop_order_columns', 10, 2 );
    

    【讨论】:

      猜你喜欢
      • 2019-09-08
      • 2018-03-16
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 2017-07-20
      • 1970-01-01
      • 2014-03-05
      • 2018-11-17
      相关资源
      最近更新 更多