John Davidson

php - Add a popup notice on woocommerce checkout and cart page

0 comments
Message:


I would like to add a popup when the customer is less that $100 if zone is Australia and $250 if somewhere else


This is the code I am testing at the moment


add_action( 'woocommerce_cart_totals_after_shipping' , 'skyverge_add_checkout_content' );
add_action( 'woocommerce_review_order_after_shipping' , 'skyverge_add_checkout_content' );

function skyverge_add_checkout_content() {


$targeted_zones_names = array('Australia'); // <====== <====== <====== <====== <======

// Get the customer shipping zone name
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); // The chosen shipping mehod
$chosen_method = explode(':', reset($chosen_methods) );
$shipping_zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $chosen_method[1] );
$current_zone_name = $shipping_zone->get_zone_name();

if ( $current_zone_name == 'Australia' ) {
$min_amount = 100;
} else {
$min_amount = 250;
}

$current = WC()->cart->subtotal;

if ( $current < $min_amount ) {
$added_text = 'We are delighted to offer free global shipping for orders '.wc_price( $min_amount ).' or more. Would you like to add more to your cart?';
//$added_text = 'Get free shipping if you order ' . wc_price( $min_amount - $current ) . ' more!';
$return_to = wc_get_page_permalink( 'shop' );
$notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), 'Continue Shopping', $added_text );
wc_print_notice( $notice, 'notice' );
}

}

This runs fine on the default woocommerce error notice/alert but I want it to popout


I think some of the code belongs to @LoicTheAztec and I just modified it



My Answer:
$targeted_zones_names = array('Australia'); // Define the targeted zones
$minimum_amount = 100; // Define the minimum amount for Australia
$minimum_amount_other = 250; // Define the minimum amount for other zones

$zone = WC()->customer->get_shipping_country(); // Get the shipping country of the customer
$total = WC()->cart->get_cart_contents_total(); // Get the total amount in the cart

if (in_array($zone, $targeted_zones_names)) {
if ($total < $minimum_amount) {
echo '

Your order total must be at least $100 for Australia

';
}
} else {
if ($total < $minimum_amount_other) {
echo '

Your order total must be at least $250 for other zones

';
}
}
}

This code will add a popup notice on the checkout and cart page if the customer's total order amount is less than $100 for Australia or less than $250 for other zones. You can customize the message and styling of the popup as needed.

Rate this post

4 of 5 based on 8910 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us