简体   繁体   中英

WordPress/JS - Price depends on the product

I have a wp condition which displays me the price of the products.

if (empty($sales_price)) {
            echo '<span class="price"><span class="woocommerce-Price-amount amount">'.$regular_price.'&nbsp;<span class="woocommerce-Price-currencySymbol">zł</span></span> </span>';
        } else {
            echo '<span class="price"><del><span class="woocommerce-Price-amount amount">'.$regular_price.'&nbsp;<span class="woocommerce-Price-currencySymbol">zł</span></span></del> <ins><span class="woocommerce-Price-amount amount">'.$sales_price.'&nbsp;<span class="woocommerce-Price-currencySymbol">zł</span></span></ins></span>';
        }

Each product has several variations and different prices. My problem is that prices do not change when changing options in the select box. I tried to add JS in different ways but nothing wanted to reload my price. How can I solve it?

Well, PHP + JS should do the trick, you can locate and set a php script to respond to AJAX Request on WP. Though, I'd link you an answer i gave a few time ago for a different question, but it should work for you: https://stackoverflow.com/a/45490613/6654533

What you're supposed to do with this method is the following:

  • Use javascript to take the value of the select when the change event is fired and send an AJAX POST request as shown by the answer posted above;
  • Use the AJAX success callback to render, what your PHP returns
  • Use PHP to retrieve the price of the item, based on the param you posted;

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM