First of all I want to say I have little coding experience. I want to show available sizes in the productbox on the shop page. I got this working after some searching around here. Now I want to only display the sizes when sizes are available. If there are no size attributes available I would like the text: No sizes available or no text at all.
I got the following code now:
add_action('woocommerce_after_shop_loop_item_title', 'display_size_attribute', 5);
function display_size_attribute() {
global $product;
if ($product->is_type('external')) {
$taxonomy = 'pa_maat';
echo "Maten:";
echo '<span class="attribute-size">'
. $product->get_attribute($taxonomy)
. '</span>';
}
}
First of all I want to say I have little coding experience. I want to show available sizes in the productbox on the shop page. I got this working after some searching around here. Now I want to only display the sizes when sizes are available. If there are no size attributes available I would like the text: No sizes available or no text at all.
I got the following code now:
add_action('woocommerce_after_shop_loop_item_title', 'display_size_attribute', 5);
function display_size_attribute() {
global $product;
if ($product->is_type('external')) {
$taxonomy = 'pa_maat';
echo "Maten:";
echo '<span class="attribute-size">'
. $product->get_attribute($taxonomy)
. '</span>';
}
}
Share
Improve this question
edited Nov 13, 2019 at 11:28
Kaperto
4783 silver badges10 bronze badges
asked Nov 13, 2019 at 10:14
SalespottersSalespotters
11 bronze badge
1 Answer
Reset to default 1to test is the text is not empty, try that :
function display_size_attribute() {
global $product;
if ($product->is_type('external')) {
$taxonomy = 'pa_maat';
$text = $product->get_attribute($taxonomy);
if (!empty($text)) {
echo "Maten:";
echo '<span class="attribute-size">'
. htmlspecialchars($text)
. '</span>';
}
} // END if ($product->is_type('external')) {
} // END function display_size_attribute() {