Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FrancoStino/9d923947a03a94703b0dda1d610a4437 to your computer and use it in GitHub Desktop.
Save FrancoStino/9d923947a03a94703b0dda1d610a4437 to your computer and use it in GitHub Desktop.
TOT remaining to Free Shipping Progress Bar @ WooCommerce Cart - Widget Mini Cart
<?php
/**
* @snippet TOT remaining to Free Shipping Progress Bar @ WooCommerce Cart - Widget Cart
*/
function goya_custom_bar_free_shipping()
{
$current = WC()->cart->get_cart_contents_total();
// Ottieni tutti i metodi di spedizione disponibili
$packages = WC()->cart->get_shipping_packages();
$package = reset( $packages );
$zone = wc_get_shipping_zone( $package );
foreach ( $zone->get_shipping_methods( true ) as $method ) {
if ( 'free_shipping' === $method->id ) {
$limit = $method->get_option( 'min_amount' );
}
}
//echo $found_free_shipping ? 'Free shipping is available.' : 'Free shipping is not available.';
if ( $current < $limit && 'free_shipping' === $method->id ) {
$percent = ($current / $limit) * 100;
$notice = "Aggiungi soltanto <strong>" . wc_price($limit - $current, array(
'decimals' => 2
)) . "</strong> per ricevere la <strong>Spedizione Gratuita!</strong>";
wc_print_notice($notice, 'notice');
} else {
$percent = 100;
$notice = "<strong>Hai la possibilità di usufruire della spedizione gratuita!</strong>";
wc_print_notice($notice, 'success');
}
?>
<div class="progress">
<div class="progress-bar">
<?php
if ($percent !== 0) {
?>
<span class="percentuale-testo"><?php
//if($percent >= 50){
echo number_format((float) $percent, 0, ',', '.') . '%';
//}
?></span>
<?php
}
?>
</div>
</div>
<?php
if ($current < $limit && 'free_shipping' === $method->id) {
$return_to = wc_get_page_permalink( 'shop' );
?><a href="<?php echo $return_to ?>" class="btn btn-color-primary button alt wc-forward" style="margin:20px 0 20px 0">Continua gli acquisti</a><?php }
?>
<style>
.progress {
margin: 10px 0 10px 0;
padding: 5px;
background: var(--wd-primary-color);
border-radius: 6px;
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.25),
0 1px rgba(255, 255, 255, 0.08);
}
.progress-bar {
height: 20px;
border-radius: 4px;
transition: 0.4s linear;
transition-property: width, background-color;
background: #fff;
width: <?php
echo number_format((float) $percent, 2);
?>%;
animation: progressAnimationStrike 3.5s;
display: flex;
align-items: center;
justify-content: center;
}
@keyframes progressAnimationStrike {
from {
width: 0;
}
to {
width: <?php echo number_format((float) $percent, 2, ',', '.'); ?>;
}
}
.percentuale-testo {
font-weight: 700;
animation: fadeIn ease 3s;
}
@keyframes fadeIn {
0% {
opacity:0;
}
20%{
opacity:0;
}
100% {
opacity:1;
}
}
</style>
<?php
}
// add to mini cart panel
//add_action('woocommerce_widget_shopping_cart_before_buttons', 'goya_custom_bar_free_shipping');
// add to cart page
add_action('woocommerce_before_cart_contents', 'goya_custom_bar_free_shipping');
// before add to cart button
//add_action('woocommerce_before_add_to_cart_button', 'goya_custom_bar_free_shipping', 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment