Advertisement
majeedraza

WooCommerce - allow html in product attribute

Jun 7th, 2021
2,034
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.88 KB | None | 0 0
  1. <?php
  2.  
  3. // custom product-attributes.php for a child theme
  4.  
  5. /**
  6.  * Product attributes
  7.  *
  8.  * Used by list_attributes() in the products class.
  9.  *
  10.  * This template can be overridden by copying it to yourtheme/woocommerce/single-product/product-attributes.php.
  11.  *
  12.  * HOWEVER, on occasion WooCommerce will need to update template files and you
  13.  * (the theme developer) will need to copy the new files to your theme to
  14.  * maintain compatibility. We try to do this as little as possible, but it does
  15.  * happen. When this occurs the version of the template file will be bumped and
  16.  * the readme will list any important changes.
  17.  *
  18.  * @see https://docs.woocommerce.com/document/template-structure/
  19.  * @package WooCommerce\Templates
  20.  * @version 3.6.0
  21.  */
  22.  
  23. // customized to allow html in attribute values
  24. // custom attributes only
  25. // does not work with global attributtes
  26. // ensure the source of the html attributes is trusted
  27.  
  28. defined( 'ABSPATH' ) || exit;
  29.  
  30. if ( ! $product_attributes ) {
  31.     return;
  32. }
  33. ?>
  34. <table class="woocommerce-product-attributes shop_attributes">
  35.     <?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
  36.         <tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
  37.             <th class="woocommerce-product-attributes-item__label topcode"><?php echo wp_kses_post( $product_attribute['label'] ); ?></th>
  38.  
  39. <?php
  40. $product_id = $product->get_id();
  41. $ats = get_post_meta( $product_id, '_product_attributes', true );
  42. foreach( $ats as $at ) {
  43.   if( $product_attribute['label'] == $at['name'] ) {    
  44.     $product_attribute['value'] = html_entity_decode( $at['value'], ENT_COMPAT | ENT_HTML5 );
  45.   }
  46. }
  47. ?>
  48.             <td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $product_attribute['value'] ); ?></td>
  49.         </tr>
  50.     <?php endforeach; ?>
  51. </table>
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement