• Resolved Phil

    (@owendevelopment)


    I’m trying to output the values of product meta and have it working, but they are wrapped in dd and dt tags and include the label. I would want to strip these to ideally, just leave the value.

    Current output:

    <dl class="variation">
    <dt class="variation-WebsiteDomain">Website Domain:</dt>
    <dd class="variation-WebsiteDomain">
    <p>
    <a rel="nofollow" href="https://mytest.com">https://mytest.com</a>
    </p>
    </dd>
    </dl>

    I would just like the value on it’s own, namely ‘https://mytest.com&#8217;.

    Anyone had an experience with this before? The code I’m using to generate and display the variation data is:

    $_product  = apply_filters( 'woocommerce_subscriptions_order_item_product', $subscription->get_product_from_item( $item ), $item );
    $item_meta = wcs_get_order_item_meta( $item, $_product );
    if ( apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
    $item_meta->display();
    }

    I traced the function in Woo, in

    woocommerce/includes/class-wc-order-item-meta.php

    and this is the bit I need to edit:

    /**
    	 * Display meta in a formatted list.
    	 *
    	 * @param bool $flat (default: false)
    	 * @param bool $return (default: false)
    	 * @param string $hideprefix (default: _)
    	 * @param  string $delimiter Delimiter used to separate items when $flat is true
    	 * @return string|void
    	 */
    	public function display( $flat = false, $return = false, $hideprefix = '_', $delimiter = ", \n" ) {
    		$output         = '';
    		$formatted_meta = $this->get_formatted( $hideprefix );
    
    		if ( ! empty( $formatted_meta ) ) {
    			$meta_list = array();
    
    			foreach ( $formatted_meta as $meta ) {
    				if ( $flat ) {
    					$meta_list[] = wp_kses_post( $meta['label'] . ': ' . $meta['value'] );
    				} else {
    					$meta_list[] = '
    						<dt class="variation-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( $meta['label'] ) . ':</dt>
    						<dd class="variation-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( wpautop( make_clickable( $meta['value'] ) ) ) . '</dd>
    					';
    				}
    			}
    
    			if ( ! empty( $meta_list ) ) {
    				if ( $flat ) {
    					$output .= implode( $delimiter, $meta_list );
    				} else {
    					$output .= '<dl class="variation">' . implode( '', $meta_list ) . '</dl>';
    				}
    			}
    		}
    
    		$output = apply_filters( 'woocommerce_order_items_meta_display', $output, $this );
    
    		if ( $return ) {
    			return $output;
    		} else {
    			echo $output;
    		}
    	}

    Any way to override this function?

    https://www.ads-software.com/plugins/woocommerce/

Viewing 15 replies - 1 through 15 (of 18 total)
  • Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    No but you can use the filter woocommerce_order_items_meta_display to change the returned strings.

    Thread Starter Phil

    (@owendevelopment)

    Like this?

    add_filter('woocommerce_order_items_meta_display', 'wps_order_items_meta_display');
    function wps_order_items_meta_display( $flat = false, $return = false, $hideprefix = '_', $delimiter = ", \n" ) {
        $output         = '';
        $formatted_meta = $this->get_formatted( $hideprefix );
    
        if ( ! empty( $formatted_meta ) ) {
          $meta_list = array();
    
          foreach ( $formatted_meta as $meta ) {
            if ( $flat ) {
              $meta_list[] = wp_kses_post( $meta['label'] . ': ' . $meta['value'] );
            } else {
              $meta_list[] = '
                <dt class="variationz-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( $meta['label'] ) . ':</dt>
                <dd class="variationz-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( wpautop( make_clickable( $meta['value'] ) ) ) . '</dd>
              ';
            }
          }
    
          if ( ! empty( $meta_list ) ) {
            if ( $flat ) {
              $output .= implode( $delimiter, $meta_list );
            } else {
              $output .= '<dl class="variationz">' . implode( '', $meta_list ) . '</dl>';
            }
          }
        }
    
        $output = apply_filters( 'woocommerce_order_items_meta_display', $output, $this );
    
        if ( $return ) {
          return $output;
        } else {
          echo $output;
        }
    }

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Not quite. woocommerce_order_items_meta_display gives you the HTML (with the DTs) and a reference to the class where this method is located.

    With that you should be able to re-run most of the logic, but remove the DT/DD elements.

    https://pippinsplugins.com/a-quick-introduction-to-using-filters/

    Thread Starter Phil

    (@owendevelopment)

    Ok, I’ve adapted to this but it still isn’t working.

    function wps_order_items_meta_display($meta_list) {
              $meta_list[] = '
                <dt class="variation-test-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( $meta['label'] ) . ':</dt>
                <dd class="variation-test-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( wpautop( make_clickable( $meta['value'] ) ) ) . '</dd>
              ';
    
      return $meta_list;
    }
    add_filter('woocommerce_order_items_meta_display', 'wps_order_items_meta_display');

    Do I need to deregister the original filter?

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Please read the guide I posted to – there is no ‘original filter’ and your usage is wrong.

    Look again at what it gives you:

    $output = apply_filters( 'woocommerce_order_items_meta_display', $output, $this );

    $output is a HTML string.

    Thread Starter Phil

    (@owendevelopment)

    Ok, I’ve figured out the hook:

    function filter_wps_order_items_meta_display( $output, $this ) {
          return $output;
        };
    add_filter( 'woocommerce_order_items_meta_display', 'filter_wps_order_items_meta_display', 10, 2 );

    which I can see works, but how do I strip the HTML tags off this?

    $meta_list[] = '<dt class="variation-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( $meta['label'] ) . ':</dt>
    						<dd class="variation-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( wpautop( make_clickable( $meta['value'] ) ) ) . '</dd>
    					';

    I’ve tried numerous ways of doing this trying to add a modified $meta_list array but it just seems to add it to the end of the default output rather than replacing it.

    Thread Starter Phil

    (@owendevelopment)

    // define the woocommerce_order_items_meta_display callback
        function filter_wps_order_items_meta_display( $output, $instance ) { 
    
              $instance = '
                <dt class="variationz-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( $meta['label'] ) . ':</dt>
                <dd class="variationz-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( wpautop( make_clickable( $meta['value'] ) ) ) . '</dd>
              ';
    
              $output .= '<dl class="variation">' . implode( '', $instance ) . '</dl>';
    
          return $output;
    
        };
        // add the filter
    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    So now you can either return strip_tags( $output ); or you can loop over the items in the same way the function which calls this function does, and just not include the tags in the first place.

    Thread Starter Phil

    (@owendevelopment)

    Just can’t get this working. It either adds the new ones in addition to the existing or just doesn’t display at all.

    // define the woocommerce_order_items_meta_display callback
    function filter_wps_order_items_meta_display( $output, $this, $formatted_meta ) { 
    
      foreach ( $formatted_meta as $meta ) {
              $meta_list[] = '
                <dd class="variation">' . wp_kses_post( wpautop( make_clickable( $meta['value'] ) ) ) . '</dd>
              ';
          }
    
          $output .= '<dl class="variation">' . implode( '', $meta_list ) . '</dl>';
    
          return $output;
    
    };
    // add the filter
    add_filter( 'woocommerce_order_items_meta_display', 'filter_wps_order_items_meta_display', 10, 2 );

    Am I missing something here?

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Change $output .= to $output = and you should be good.

    Thread Starter Phil

    (@owendevelopment)

    Hi Mike,

    It just outputs it empty:

    <dl class="variation"></dl>
    <dl class="variation"></dl>

    So the filter works, it’s just not outputting the meta values. Here’s the filter:

    // define the woocommerce_order_items_meta_display callback
    function filter_wps_order_items_meta_display( $output, $this, $formatted_meta ) { 
    
      foreach ( $formatted_meta as $meta ) {
              $meta_list[] = '
                <dd class="variation">' . wp_kses_post( wpautop( make_clickable( $meta['value'] ) ) ) . '</dd>
              ';
          }
    
          $output = '<dl class="variation">' . implode( '', $meta_list ) . '</dl>';
    
          return $output;
    
    };
    // add the filter
    add_filter( 'woocommerce_order_items_meta_display', 'filter_wps_order_items_meta_display', 10, 2 );
    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Wrong variable count:

    add_filter( 'woocommerce_order_items_meta_display', 'filter_wps_order_items_meta_display', 10, 3 );
    Thread Starter Phil

    (@owendevelopment)

    Hi,

    Still just outputs empty:

    <dl class="variation"></dl>
    <dl class="variation"></dl>

    Seems to be outputting the <dl> as foreach, rather than it being the container and inner <dd>’s, which should be the foreach loop, no?

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    Are you sure you’ve have not added more blocks of code? Because there is no foreach wrapping the DL output in the code you pasted above…

    Thread Starter Phil

    (@owendevelopment)

    Nope, this is literally the entire thing:

    // define the woocommerce_order_items_meta_display callback
    function filter_wps_order_items_meta_display( $output, $this, $formatted_meta ) {
      $meta_list = array();
      foreach ( $formatted_meta as $meta ) {
              $meta_list[] = '<dd class="variation">' . wp_kses_post( wpautop( make_clickable( $meta['value'] ) ) ) . '</dd>';
          }
    
          $output = '<dl class="variation">' . implode( '', $meta_list ) . '</dl>';
    
          return $output;
    
    };
    // add the filter
    add_filter( 'woocommerce_order_items_meta_display', 'filter_wps_order_items_meta_display', 10, 3 );
Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Woocommerce Product Meta – Strip dd and dt labels’ is closed to new replies.