php - Conditionally display variation descriptions on woocommerce product pages

I'm trying to hide the variation description on product pages if the variation's stock is >=1. Following this I have made this code which is not working:
add_action ('woocommerce_after_single_variation', 'hide_descriptions', 10);
function hide_descriptions() {
$stock_qty = $product->get_stock_quantity();
if ($stock_qty>=1)
?>
<div class="woocommerce-variation-description" style="display: none!important;"></div>
<?php
}
I've also tried with the action hook'woocommerce_single_variation'
since theclass="woocommerce-variation-description"
is located inside theclass="woocommerce-variation single_variation"
- and the latter class is called by thefunction woocommerce_single_variation()
.
But how do you access a nested class with a hook that targets its parent?
Answer
Solution:
first of all, I see you didnt have opening and closing if brackets. And u wrote greater, not lower than 1, and try to use same priority as mentioned in the answer you looked up - 50.
add_action ('woocommerce_after_single_variation', 'hide_descriptions', 50);
function hide_descriptions() {
$stock_qty = $product->get_stock_quantity();
if ($stock_qty < 2) { ?>
<div class="woocommerce-variation-description" style="display:none"></div>
<?php }
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: target class [commandmakecommand] does not exist.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.