将自定义属性添加到产品的HTML到woocommerce购物车页面

时间:2018-12-13 11:28:11

标签: php wordpress woocommerce cart

我正在尝试向WooCommerce的购物车页面添加data-attribute

购物车页面会填充一个表格,每一行都是已添加到购物车的产品。

我可以为该行的HTML添加数据属性,如下所示:

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

我知道这是一个不好的做法,因为在更新插件时,它将被覆盖。

我试图将相同的功能添加到模板的functions.php文件中,但是在浏览了WooCommerce支持文档后,我看不到任何有帮助的内容。

有什么想法吗?

0 个答案:

没有答案