Use IF statements to display data
-
Hello,
I am using your plugin for my automotive site. And was wondering if there was a way to use IF statements to have data shown.
-
Hi @crombiecrunch,
Thanks for reaching out! Based on your question, I’m imagining two scenarios:
1. I want a tab shown or hidden based on some condition.
2. I want the content of a tab to change based on some condition.For scenario 1, you can use the filter and approach that is outlined here. Inside of the filter function, you’ll want to check to make sure you’re on the correct product, etc.
For scenario 2, I’d recommend making a shortcode to display the data, and in the shortcode callback function you can add the conditional.
Let me know if that helps, or if you have any trouble with the implementation.
Thanks,
JonSo it looks like I might want to use both. Ive been trying to play with the code but no luck, if maybe you can help Ill give both uses I would like.
So A. for the tabs themselves – this one is fixed I found code from Ebonie that was posted 8 months ago that is working for hiding empty custom tabs.
Then B.
I have data that displays in the panel such as
color
style
warrantybut again say not all products have a value say for warranty so instead of it just displaying ‘Warranty: ‘ and nothing there Id want the whole warranty line removed.
And finally, is there a way to set a width for the text? It seems some of the data that gets imported runs past the margins while most wraps like it should.
-
This reply was modified 4 years, 9 months ago by
crombiecrunch.
oh and my order 34845 has been processing since August 11, 2021
Hi @crombiecrunch,
So for a scenario like the maybe undefined “Warranty” section, you could just wrap that section in a conditional.
In your shortcode:
$warranty = get_post_meta(get_the_ID(), 'warranty-field-name', true); // or however you are grabbing it if (!empty($warranty)) { echo "<div>Warranty: " . $warranty . "</div>"; }That probably won’t be exactly what you want, but it’s the general strategy you will follow.
The width will be set in your CSS. You can use your browser’s developer tools to see what’s going on with the page. If you wrap the whole shortcode output in some sort of container with a class (such as
<div class="data-tab">...</div>) you can make sure that the styles you write are specific to the shortcode.I checked the order. You may want to reach out to Paypal, it looks like the payment is still processing with them.
Let me know if that helps,
Jon-
This reply was modified 4 years, 9 months ago by
jpowersdev.
thanks, il check paypal.
so this code works
<?php global $product; $warranty = $product->get_attribute( 'warranty' ); if (!empty($warranty)) { echo "<div>Warranty: " . $warranty . "</div>"; }Ok me again, haha.
So Since I have so many attributes and will be adding more doing it as above is not right.
So I found `global $product;
echo wc_display_product_attributes( $product );`Which does display every attribute assigned to that product, but now I am trying to figure out how to filter some of the data out. So for example there are attributes like brand, brand logo, and base category that I do not want to be displayed.
And also if possible I would like it so that it just doesn’t make a huge list straight down but maybe in a couple of columns of data.
And I need to make sure to buy you a few coffees for all the help.
Hey @crombiecrunch,
It looks like you can filter the output of that function using the following filter:
add_filter( 'woocommerce_display_product_attributes', 'remove_product_information', 10, 2 ); function remove_product_information( $product_attributes, $product ) { // Remove an attribute from the array unset($product_attributes['any-field']); return $product_attributes; }That allows you to modify the list, but doesn’t allow you to change the markup. In order to do that, you’ll need to copy the
plugins/woocommerce/templates/single-product/product-attributes.phpfile into your theme and make modifications there.Here’s an article on overriding WooCommerce templates – https://docs.woocommerce.com/document/template-structure/. Be sure to use a child theme if your site’s theme is from the marketplace, otherwise, theme updates will remove your modifications.
Happy to help!
Thanks,
Jonhey @jpowersdev thanks for that, I tried the following but its still showing “color” for example.
global $product; add_filter( 'woocommerce_display_product_attributes', 'remove_product_information', 10, 2 ); function remove_product_information( $product_attributes, $product ) { // Remove an attribute from the array unset($product_attributes['color']); return $product_attributes; } echo wc_display_product_attributes( $product );Hi @crombiecrunch,
You’ll probably want to start by doing a
var_dump($product_attributes);to see what keys are set, then you’ll know what to remove.Jon
So the above code does work, I was just using the wrong attribute name. needed to add attribute_pa_
thanks again @jpowersdev
-
This reply was modified 4 years, 9 months ago by
The topic ‘Use IF statements to display data’ is closed to new replies.