0

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:

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

<?php
  // 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>
  <?php 
  }

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?

Eli Nathan
  • 1,020
  • 2
  • 13
  • 35
  • 1
    In Woocommerce all files located in the "templates" folder [are made to be overridden via the theme](https://docs.woocommerce.com/document/template-structure/). So they will not be overwritten when the plugin is updated if you copy them to your active theme folder as explained on the provided link. – LoicTheAztec Dec 13 '18 at 12:00
  • Thanks @LoicTheAztec! I didn't know that, that's really useful! – Eli Nathan Dec 13 '18 at 12:36
  • There is [this related](https://stackoverflow.com/questions/39063958/woocommerce-action-hooks-and-overriding-templates/39064240#39064240) thread that explains a bit that. – LoicTheAztec Dec 13 '18 at 13:18

0 Answers0