Created
April 25, 2023 06:28
-
-
Save cargabsj175/a40e7d18185a06df9f9ca68076cbba73 to your computer and use it in GitHub Desktop.
Establecer un importe minimo en la compra y deshabilitar botón de finalizar compra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Habilitar la actualización automática del carrito en WooCommerce | |
*/ | |
function actualizar_carrito_al_vuelo() { | |
if (is_cart()) { | |
?> | |
<script> | |
jQuery('div.woocommerce').on('change', 'input.qty', function(){ | |
jQuery("[name='update_cart']").trigger("click"); | |
}); | |
</script> | |
<?php | |
} | |
} | |
add_action('wp_footer', 'actualizar_carrito_al_vuelo'); | |
// // Establecer un importe minimo en la compra y deshabilitar botón de finalizar compra | |
function woocommerce_importe_minimo() { | |
$minimum = 300000; | |
if ( WC()->cart->total < $minimum ) { | |
if( is_cart() ) { | |
wc_print_notice( | |
sprintf( ' Debes realizar un pedido mínimo de %s para finalizar su compra.' , | |
wc_price( $minimum ), | |
wc_price( WC()->cart->total ) | |
), 'error' | |
); | |
} else { | |
wc_add_notice( | |
sprintf( 'No puedes finalizar tu compra. Debes realizar un pedido mínimo de %s para finalizar su compra.' , | |
wc_price( $minimum ), | |
wc_price( WC()->cart->total ) | |
), 'error' | |
); | |
} | |
// deshabilitar el enlace de finalizar compra | |
?> | |
<script> | |
jQuery(document).ready(function($){ | |
$('a.checkout-button').addClass('disabled').attr('href', '#'); | |
$('#order_review').on('change', function(){ | |
if ( $('div.cart_totals span.amount').text() >= '<?php echo $minimum; ?>' ) { | |
$('a.checkout-button').removeClass('disabled').attr('href', $('a.checkout-button').data('href')); | |
} else { | |
$('a.checkout-button').addClass('disabled').attr('href', '#'); | |
} | |
}); | |
// Guardar los valores de cantidad antes de recargar la página | |
var qtyFields = $('input.input-text.qty.text'); | |
var qtyValues = []; | |
qtyFields.each(function(){ | |
qtyValues.push($(this).val()); | |
}); | |
$('button[name="update_cart"]').on('click', function(){ | |
// Restaurar los valores de cantidad después de recargar la página | |
setTimeout(function(){ | |
qtyFields.each(function(index){ | |
$(this).val(qtyValues[index]); | |
}); | |
}, 100); | |
location.reload(); | |
}); | |
}); | |
</script> | |
<?php | |
} else { | |
// habilitar el enlace de finalizar compra | |
?> | |
<script> | |
jQuery(document).ready(function($){ | |
$('a.checkout-button').removeClass('disabled').attr('href', $('a.checkout-button').data('href')); | |
// Guardar los valores de cantidad antes de recargar la página | |
var qtyFields = $('input.input-text.qty.text'); | |
var qtyValues = []; | |
qtyFields.each(function(){ | |
qtyValues.push($(this).val()); | |
}); | |
$('button[name="update_cart"]').on('click', function(){ | |
// Restaurar los valores de cantidad después de recargar la página | |
setTimeout(function(){ | |
qtyFields.each(function(index){ | |
$(this).val(qtyValues[index]); | |
}); | |
}, 100); | |
location.reload(); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
} | |
add_action( 'woocommerce_checkout_process', 'woocommerce_importe_minimo' ); | |
add_action( 'woocommerce_before_cart' , 'woocommerce_importe_minimo' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment