无需覆盖模板文件调整WooCommerce购物车列顺序方案咨询
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 thewoocommerce_cart_item_columnsfilter (most well-built themes do). - Using a custom plugin instead of
functions.phpis better if you switch themes often—your customizations will stay intact.
内容的提问来源于stack exchange,提问作者Cray

