Display extra product data in Loop and on single product pages
There are times when you might want to display data from a product either in the Loop (either Shop Front page or Category Page) or on the single product page.
Here's how you can display any Datafeedr product data on those pages.
- 1
-
Create a custom plugin
If you haven't done so already, create a custom plugin.
- 2
-
Display data in the Loop
To display extra product data in the Loop add the following code:
add_action( 'woocommerce_before_shop_loop_item_title', function () { global $product; /** * Display the product's color if available. */ if ( $color = dfrps_get_product_field( $product->get_id(), 'color' ) ) { printf( '<div>Color: %s</div>', esc_html( $color ) ); } /** * Display the product's material if available. */ if ( $material = dfrps_get_product_field( $product->get_id(), 'material' ) ) { printf( '<div>Material: %s</div>', esc_html( $material ) ); } /** * Display the product's condition if available. */ if ( $condition = dfrps_get_product_field( $product->get_id(), 'condition' ) ) { printf( '<div>Condition: %s</div>', esc_html( $condition ) ); } /** * Display the product's brand attribute if available. */ if ( $brand = $product->get_attribute( 'brand' ) ) { printf( '<div>Brand: %s</div>', esc_html( $brand ) ); } /** * Display the product's merchant attribute if available. */ if ( $merchant = $product->get_attribute( 'merchant' ) ) { printf( '<div>Merchant: %s</div>', esc_html( $merchant ) ); } /** * Display the product's network attribute if available. */ if ( $network = $product->get_attribute( 'network' ) ) { printf( '<div>Network: %s</div>', esc_html( $network ) ); } }, 20 );
- 3
-
Display data on single product's page
To display extra product data on a single product page, add the following code:
add_action( 'woocommerce_before_add_to_cart_button', function () { global $product; /** * Display the product's color if available. */ if ( $color = dfrps_get_product_field( $product->get_id(), 'color' ) ) { printf( '<div>Color: %s</div>', esc_html( $color ) ); } /** * Display the product's material if available. */ if ( $material = dfrps_get_product_field( $product->get_id(), 'material' ) ) { printf( '<div>Material: %s</div>', esc_html( $material ) ); } /** * Display the product's condition if available. */ if ( $condition = dfrps_get_product_field( $product->get_id(), 'condition' ) ) { printf( '<div>Condition: %s</div>', esc_html( $condition ) ); } /** * Display the product's brand attribute if available. */ if ( $brand = $product->get_attribute( 'brand' ) ) { printf( '<div>Brand: %s</div>', esc_html( $brand ) ); } /** * Display the product's merchant attribute if available. */ if ( $merchant = $product->get_attribute( 'merchant' ) ) { printf( '<div>Merchant: %s</div>', esc_html( $merchant ) ); } /** * Display the product's network attribute if available. */ if ( $network = $product->get_attribute( 'network' ) ) { printf( '<div>Network: %s</div>', esc_html( $network ) ); } }, 20 );
- 4
-
Save your changes
After you have added the code and edited to fit your needs, save your changes.
Now you will see custom Datafeedr product data in your Loop and/or on your single product pages.