E
E
Evgeniy2020-05-21 17:31:28
PHP
Evgeniy, 2020-05-21 17:31:28

How to set a rule for this function?

Good afternoon dear colleagues. There was one problem that could not be solved. I wrote a code that if the price of the product is equal to zero or simply not recorded, then the phrase By request should be displayed. This idea turned out, but I can’t add a button to the product page. That is, the button should be only on those pages where there is a by request. The button should be below the Tags line. Under the price it turns out to be done, but not under Tags.

function product_price_free_zero_empty( $price, $product ){
if ( '' === $product->get_price() || 0 == $product->get_price() ) {
    $price = '<span class="woocommerce-Price-amount amount">By request</span>';
}
return $price;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kontrael, 2020-05-21
@Evdokim001

Your approach is fundamentally wrong. You are mixing logic and presentation, but that is a separate issue.
This:

if ( '' === $product->get_price() || 0 == $product->get_price() )

In general, it can be replaced by
if (!$product->get_price())

And then the need for a function
product_price_free_zero_empty
disappears.
In the code that collects the html of the page, write something like:
if (!$product->get_price()){
    echo '<span class="woocommerce-Price-amount amount">By request</span>';
}

And under the tags where the button is needed:
if (!$product->get_price()){
    echo '<input type="button" value="Кнопка" /> ';
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question