I'm trying to add a data-attribute to WooCommerce's cart page.

The cart page populates a table with each row being a product that has been added to the cart.

I'm able to add a data attribute into the HTML for this row like so:


  // For each item in cart
  foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

    // Get product
    $_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );

    // Get custom attribute
    $foobar = $_product->get_attribute( 'myCustomAttribute' );
    $foobar == true ? $foo = "true" : $foo = "false";

    // Add table row with custom attribute as a data attribute
    <tr data-foo=<?php echo "$foo"; ?>>...Content in here</tr>

I know this is bad practise as it'll be overwritten when the plugin is updated.

I'm trying to add the same functionality into my template's functions.php file but after looking through the WooCommerce support docs I can't see anything that would help.

Any ideas?

  • You can copy the template file to your theme folder. So you'd have wp-content/themes/your-theme/woocommerce/cart/cart.php - and you don't need to copy all templates - just the ones you want to modify/customize.
    – Sally CJ
    Dec 13, 2018 at 12:04
  • Thanks @SallyCJ! I didn't know that! Would upvote if I could!
    – Eli Nathan
    Dec 13, 2018 at 12:36
  • You're welcome! And for further details, check out this article. :)
    – Sally CJ
    Dec 13, 2018 at 13:11

1 Answer 1


You want to override the file, not edit it directly.

How to Edit Files

Edit files in an upgrade-safe way using overrides. Copy the template into a directory within your theme named /woocommerce keeping the same file structure but removing the /templates/ subdirectory.

Example: To override the cart.php file...

copy: wp-content/plugins/woocommerce/templates/cart/cart.php

to wp-content/themes/yourtheme/woocommerce/cart/cart.php

The copied file will now override the WooCommerce default template file. Make your updates in the new file.

Here is more detailed information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.