Search code examples
wordpresswoocommerceproductcart

Add column with product numbering on WooCommerce cart page


I'm trying to add numbers to each row in WooCommerce cart page, but i'm unable to get it working or even showing up.

My current code:

add_action( 'woocommerce_before_add_to_cart_quantity', 'quadlayers_woocommerce_hooks');
function quadlayers_woocommerce_hooks() {
global $woocommerce;
    for ($x = 1; $x <= 0; $x++)
{
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

   //$product = $cart_item['data'];
   $product_id = $cart_item['product_id'];
   
}
}
    echo $x;
}

It should be something like this:

enter image description here

Any advice on how to achieve this?


Solution

  • The adjustments you want are not possible via hooks, for this you will have to overwrite the /cart/cart.php file.

    • This template can be overridden by copying it to yourtheme/woocommerce/cart/cart.php.
    • These adjustments are provided for the template file with version number @version 3.8.0
    • CSS adjustments may be required depending on the theme you are using

    Replace line 28 - 29

    <th class="product-remove">&nbsp;</th>
    <th class="product-thumbnail">&nbsp;</th>
    

    With

    <th class="product-remove">&nbsp;</th>
    <th class="product-number">&nbsp;</th>
    <th class="product-thumbnail">&nbsp;</th>
    

    Replace line 39 - 40

    <?php
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    

    With

    <?php
    // Counter
    $i = 1;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    

    Replace line 65

    <td class="product-thumbnail">
    

    With

    <td class="product-number">
        <?php
        echo $i;
        $i++;
        ?>
    </td>
    
    <td class="product-thumbnail">
    

    Result:

    enter image description here