I have a specific task, to make 'add-to-cart' button disabled if the order amount is less then 1500. I use this snippet to show the current price, it changes in quantity input by +- buttons. And simply add css class to make the button look disabled if the amount is less then 1500.
add_action( 'woocommerce_single_product_summary', 'woocommerce_total_product_price', 31 );
function woocommerce_total_product_price() {
global $woocommerce, $product;
// let's setup our divs
echo sprintf('<div id="product_total_price" style="margin-bottom:20px;">%s %s</div>',__('Your order amount:','woocommerce'),'<span class="price">'.$product->get_price().'</span><p style="font-weight: bold;">The minimum order amount is<span style="color:red;">1500 ₽</span></p>');
?>
<script>
jQuery(function($){
var price = <?php echo $product->get_price(); ?>,
currency = '<?php echo get_woocommerce_currency_symbol(); ?>';
$('[name=quantity]').change(function(){
var product_total = parseFloat(price * this.value);
if (( product_total < 1500) ) {
$('#product_total_price p').removeClass('d-none');
$('.button.single_add_to_cart_button.button.alt.fa-cart').addClass('minimum-disabled');
$('#product_total_price').html('Your order amount: '+ product_total.toFixed(2) + ' ' + currency + '<p style="font-weight: bold;">The minimum order amount is <span style="color:red;">1500 ₽</span></p>');
}
else{
var product_total = parseFloat(price * this.value);
$('.button.single_add_to_cart_button.button.alt.fa-cart').removeClass('minimum-disabled');
$('#product_total_price .price').html( product_total.toFixed(2) + ' ' + currency);
}
});
});
</script>
<?php
}
I've made it work on the single product page, but I have troubles in the loop. I use add to cart form from the single product on the shop page, not native from the archive. It shows the correct price from the start, but when I change it - the price in each produc is wrong. In colnsole.log I see prices of all products instead of the one I try to change. I tried this(), parent() and closest() to say ' Change it only for that product, that is your parent', but it doesn't work. I use the same code, only another hook.
add_action('woocommerce_shop_loop_item_title', 'woocommerce_archive_total_product_price',2);
And class .wrap-loop-item instead of .summary. How to show the item's price correctly when I change the quantity? Cause it globally shows all prices