You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需覆盖模板文件调整WooCommerce购物车列顺序方案咨询

Reorder WooCommerce Cart Columns Without Copying cart.php

Absolutely! You don’t have to mess with copying and modifying the core cart.php template to reorder your cart columns—WooCommerce provides native hooks that let you do this cleanly, keeping your customizations separate from core files (so no update-related headaches later). Here’s how to pull it off:

The Core Idea

WooCommerce uses the woocommerce_cart_item_columns filter to define the order and labels of cart table columns. The default cart template loops through this array to render each column, so we can simply reorder the array to match your desired sequence.

Step 1: Reorder Columns with a Filter

Add this code to your theme’s functions.php file (or a custom plugin, for even better portability):

// Customize cart column order
add_filter( 'woocommerce_cart_item_columns', 'custom_reorder_woocommerce_cart_columns' );
function custom_reorder_woocommerce_cart_columns( $columns ) {
    // Define your preferred column order here
    // Default columns are: product-remove, product-thumbnail, product-name, product-price, product-quantity, product-subtotal
    $new_column_order = array(
        'product-remove'    => $columns['product-remove'],
        'product-name'      => $columns['product-name'], // Move product name first
        'product-thumbnail' => $columns['product-thumbnail'], // Thumbnail after name instead of before
        'product-price'     => $columns['product-price'],
        'product-quantity'  => $columns['product-quantity'],
        'product-subtotal'  => $columns['product-subtotal'],
    );

    return $new_column_order;
}

Just adjust the $new_column_order array to match the sequence you want. For example, if you want the quantity column before price, swap their positions in the array.

Step 2: (Optional) Customize Column Content

If you need to tweak what’s displayed in a column (not just its order), you can use the woocommerce_cart_item_{$column_id} action. For example, to modify the thumbnail column output:

// Customize the cart thumbnail column
add_action( 'woocommerce_cart_item_product-thumbnail', 'custom_cart_thumbnail_render', 10, 3 );
function custom_cart_thumbnail_render( $cart_item, $cart_item_key, $cart ) {
    $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
    
    // Use a smaller thumbnail size
    $thumbnail = $_product->get_image( 'thumbnail' );
    
    if ( $product_permalink = $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '' ) {
        printf( '<a href="%s" class="cart-item-thumbnail">%s</a>', esc_url( $product_permalink ), $thumbnail );
    } else {
        echo $thumbnail;
    }
}

Why This Works

The default cart.php template uses a loop over the $columns array returned by woocommerce_cart_item_columns to build the table. By modifying this array’s order, we’re telling WooCommerce exactly how to arrange the columns—no template overrides needed.

Key Notes

  • This method works with the default WooCommerce cart template. If your theme has already overridden cart.php, check if it still uses the woocommerce_cart_item_columns filter (most well-built themes do).
  • Using a custom plugin instead of functions.php is better if you switch themes often—your customizations will stay intact.

内容的提问来源于stack exchange,提问作者Cray

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:45:51