I would like the following code (or something close to it) to only apply if the customers geolocation is from the UK? I've tried a few different options but have been unable to get it working when I test from another country.
function free_shipping_cart_notice() {
$min_amount = 30;
// Subtotal inc. Tax excl. Shipping
$current = WC()->cart->subtotal;
if ( $current < $min_amount ) {
$added_text = esc_html__('You will have FREE shipping if you add ', 'woocommerce' ) . wc_price( $min_amount - $current ) . esc_html__(' more in your order!', 'woocommerce' );
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
$notice = sprintf( '%s %s', esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ), $added_text );
wc_print_notice( $notice, 'notice' );
} else {
wc_print_notice( 'Congrats! You have free shipping with more than £30 in your order', 'notice' );
}
}
add_action( 'woocommerce_before_cart', 'free_shipping_cart_notice' );
Ok, have tried another method and have got this working.
//free shipping qualification UK only
add_action( 'woocommerce_before_cart', 'md_free_shipping_cart_notice' );
function md_free_shipping_cart_notice() {
$threshold = 25;
$current = WC()->cart->subtotal;
$billing_country = WC()->customer->get_billing_country();
if ( $current < $threshold && WC()->customer->get_billing_country() == 'GB' ) {
$added_text = esc_html__('You will have FREE shipping if you add ', 'woocommerce' ) . wc_price( $threshold - $current ) . esc_html__(' more in your order!', 'woocommerce' );
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
$notice = sprintf( '%s %s', esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ), $added_text );
wc_print_notice( $notice, 'notice' );
} elseif ( $current > $threshold && WC()->customer->get_billing_country() == 'GB' ) {
wc_print_notice( 'Congrats! You have free shipping with more than £25 in your order', 'notice' );
}
}
Hope this helps someone else
Related
Right now i am displaying the tax for all hte prices like total and shipping, now i have the requirement to add the same tax values for the subtotals as well as shown in the following image
I have found nothing related to this but for the cart page to add new field with the others, SO actually i want to add this tax information with the subtotal field.
Show subtotal excl. tax, add subtotal tax as separate row on Woocommerce checkout
To display in cart subtotal (orders and notifications) like "71,540.20€ (includes 11400,00€ VAT)", you can try to use the following:
// Cart and checkout (for display including taxes - not compound)
add_filter('woocommerce_cart_subtotal', 'wc_cart_subtotal_incl_tax_amount_filter', 10, 3 );
function wc_cart_subtotal_incl_tax_amount_filter( $cart_subtotal, $compound, $cart ) {
if ( wc_tax_enabled() && $cart->get_subtotal_tax() > 0 && ! $compound ) {
$subtotal_tax = wc_price( $cart->get_subtotal_tax() );
$cart_subtotal = wc_price( $cart->get_subtotal() + $cart->get_subtotal_tax() );
$tax_label = in_array( WC()->countries->get_base_country(), array_merge( WC()->countries->get_european_union_countries( 'eu_vat' ), array( 'NO' ) ), true ) ? __( 'VAT', 'woocommerce' ) : __( 'Tax', 'woocommerce' );
$cart_subtotal .= sprintf( ' <small>' . esc_html__( '(includes %s %s)', 'woocommerce' ) . '</small>', $subtotal_tax, $tax_label );
}
return $cart_subtotal;
}
// Orders and emails (for display including taxes - not compound)
add_filter('woocommerce_order_subtotal_to_display', 'wc_order_subtotal_incl_tax_amount_filter', 10, 3 );
function wc_order_subtotal_incl_tax_amount_filter( $subtotal, $compound, $order ) {
if ( wc_tax_enabled() && $order->get_total_tax() > 0 && ! $compound ) {
$subtotal_tax = $subtotal = 0; // Initializing
// Loop through order items
foreach ( $order->get_items() as $item ) {
$subtotal += $item->get_subtotal() + $item->get_subtotal_tax();
$subtotal_tax += $item->get_subtotal_tax();
}
$subtotal = wc_price( $subtotal, array( 'currency' => $order->get_currency() ) );
$subtotal_tax = wc_price( $subtotal_tax, array( 'currency' => $order->get_currency() ) );
$tax_label = in_array( WC()->countries->get_base_country(), array_merge( WC()->countries->get_european_union_countries( 'eu_vat' ), array( 'NO' ) ), true ) ? __( 'VAT', 'woocommerce' ) : __( 'Tax', 'woocommerce' );
$subtotal .= sprintf( ' <small>' . esc_html__( '(includes %s %s)', 'woocommerce' ) . '</small>', $subtotal_tax, $tax_label );
}
return $subtotal;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
I am trying to change the in stock text next to the quantity available in woocommerce. I am using the stock management in product variations.
I tried this code below:
// change stock text
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $variation ) {
// Change In Stock Text
if ( $variation->is_in_stock() ) {
$availability['availability'] = __('Available!', 'woocommerce');
}
// Change Out of Stock Text
if ( ! $variation->is_in_stock() ) {
echo '-------------------------';
echo __('Sold Out', 'woocommerce');
$availability['availability'] = __('Sold Out', 'woocommerce');
}
return $availability;
}
The code above changes the text but it does not pull in the stock quantity number from the variation stock manager.
The following code will handle all cases including the stock amount display with your custom texts:
add_filter( 'woocommerce_get_availability_text', 'customizing_stock_availability_text', 1, 2);
function customizing_stock_availability_text( $availability, $product ) {
if ( ! $product->is_in_stock() ) {
$availability = __( 'Sold Out', 'woocommerce' );
}
elseif ( $product->managing_stock() && $product->is_on_backorder( 1 ) )
{
$availability = $product->backorders_require_notification() ? __( 'Available on backorder', 'woocommerce' ) : '';
}
elseif ( $product->managing_stock() )
{
$availability = __( 'Available!', 'woocommerce' );
$stock_amount = $product->get_stock_quantity();
switch ( get_option( 'woocommerce_stock_format' ) ) {
case 'low_amount' :
if ( $stock_amount <= get_option( 'woocommerce_notify_low_stock_amount' ) ) {
/* translators: %s: stock amount */
$availability = sprintf( __( 'Only %s Available!', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount, $product ) );
}
break;
case '' :
/* translators: %s: stock amount */
$availability = sprintf( __( '%s Available!', 'woocommerce' ), wc_format_stock_quantity_for_display( $stock_amount, $product ) );
break;
}
if ( $product->backorders_allowed() && $product->backorders_require_notification() ) {
$availability .= ' ' . __( '(can be backordered)', 'woocommerce' );
}
}
else
{
$availability = '';
}
return $availability;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
I'm trying to add a button so users can go back to the current category they are buying a product. In order to do this, i would like this button to appear once they buy a product, specifically in the message, so my code goes like this:
function wc_add_to_cart_message( $product_id ) {
$titles = array();
if ( is_array( $product_id ) ) {
foreach ( $product_id as $id ) {
$titles[] = get_the_title( $id );
}
} else {
$titles[] = get_the_title( $product_id );
}
$titles = array_filter( $titles );
$added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );
// Output success messages
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
$message = sprintf( '%s %s', esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ), esc_html( $added_text ) );
} else {
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
$single_cat = array_shift( $product_cats );
$message = '<a class="button wc-forward" href="http://website/newstore/category/campamentos/' . $single_cat->slug . ' ">Continue Shopping</a>';
$message .= sprintf( '%s %s', esc_url( wc_get_page_permalink('cart') ), esc_html__( 'View Cart', 'woocommerce' ), esc_html( $added_text ) );
}
wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );
}
It works good, but i can't get the continue shopping button to have the last part of the link assigned here >
$message = '<a class="button wc-forward" href="http://website/newstore/category/campamentos/' . $single_cat->slug . ' ">Continue Shopping</a>';
Guess is because i'm not using echo with $single_cat->slug??
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want the text before the button, does anyone one how?
heres the code
function wc_add_to_cart_message( $product_id ) {
$titles = array();
if ( is_array( $product_id ) ) {
foreach ( $product_id as $id ) {
$titles[] = get_the_title( $id );
}
} else {
$titles[] = get_the_title( $product_id );
}
$titles = array_filter( $titles );
$added_text = sprintf( _n ( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );
// Output success messages
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
$message = sprintf( '%s %s', esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ), esc_html( $added_text ) );
} else {
$message = sprintf( '%s%s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ), esc_html( $added_text ) );
}
wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );
}
QUESTION #2
function wc_add_to_cart_message( $product_id ) {
$titles = array();
if ( is_array( $product_id ) ) {
foreach ( $product_id as $id ) {
$titles[] = get_the_title( $id );
}
} else {
$titles[] = get_the_title( $product_id );
}
$titles = array_filter( $titles );
$added_text = sprintf( _n( '**DIV HERE** %s has been added to your cart.', '**DIV HERE** %s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );
// Output success messages
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
$message = sprintf( '%s %s ', esc_html( $added_text ), esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ) );
} else {
$message = sprintf( '%s %s', esc_html( $added_text ), esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ) );
}
wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );
}
This should work. Try this
// Output success messages
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
$message = sprintf( '%s %s ', esc_html( $added_text ), esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ) );
} else {
$message = sprintf( '%s %s', esc_html( $added_text ), esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ) );
}
wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );
Question #2 Answer
// Output success messages
if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
$message = sprintf( '<div class="acmsg">%s</div> %s ', esc_html( $added_text ), esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ) );
} else {
$message = sprintf( '<div class="acmsg">%s</div> %s', esc_html( $added_text ), esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ) );
}
wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );
I need two buttons to show up after a product is added to a cart on an e commerce site.Here is how the code reads now:
// Output success messages
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) :
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
$message = sprintf('%s %s', $return_to, __( 'Continue Shopping', 'woocommerce' ), $added_text );
else :
$message = sprintf('%s %s', get_permalink( wc_get_page_id( 'cart' ) ), __( 'View Cart', 'woocommerce' ), $added_text );
endif;
wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );
}
I want both the "Continue Shopping" and View Cart buttons to show up.
So if I am understanding correctly, this would display the Cart and Continue shopping buttons together:
// Output success messages
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) :
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
$message = sprintf('%s %s', $return_to, __( 'Continue Shopping', 'woocommerce' ), $added_text );
$message = sprintf('%s %s', get_permalink( wc_get_page_id( 'cart' ) ), __( 'View Cart', 'woocommerce' ), $added_text );
else :
$message = sprintf('%s %s', get_permalink( wc_get_page_id( 'cart' ) ), __( 'View Cart', 'woocommerce' ), $added_text );
endif;
wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );