• 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 3 replies - 16 through 18 (of 18 total)
  • Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    // define the woocommerce_order_items_meta_display callback
    	function filter_wps_order_items_meta_display( $output, $class ) {
    	  $meta_list      = array();
    	  $formatted_meta = $class->get_formatted( '_' );
    
    	  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_filter( 'woocommerce_order_items_meta_display', 'filter_wps_order_items_meta_display', 10, 2 );
    Thread Starter Phil

    (@owendevelopment)

    <3

    So it came down to it requiring this line:

    $formatted_meta = $class->get_formatted( '_' );

    Thanks dude, much appreciated.

    Hi, I’m trying to implement this solution but I get this error:

    Fatal error: Call to a member function get_formatted() on null in /var/www/wp-content/themes/theme-name/functions.php on line 163

    This is the snippet in my functions.php

    function filter_wps_order_items_meta_display( $output, $class ) {
        $meta_list      = array();
        $formatted_meta = $class->get_formatted( '_' );
        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_filter( 'woocommerce_order_items_meta_display', 'filter_wps_order_items_meta_display', 10, 2 );

    And here is the part printing the cart product meta data in my mini-cart.php:
    <?php echo apply_filters( 'woocommerce_order_items_meta_display', WC()->cart->get_item_data( $cart_item ), $this ); ?>

    Any idea? As I see the problem is in the class, that’s not being passed correctly or something related.

    Thank you in advance.
    Antonio.

Viewing 3 replies - 16 through 18 (of 18 total)
  • The topic ‘Woocommerce Product Meta – Strip dd and dt labels’ is closed to new replies.