This question already has an answer here:
Shipping cost based on cart total weight in Woocommerce 3
(1 answer)
Closed 2 years ago.
I'm trying to custom code the shopping cart in Woocommerce by changing the price of the delivery when a condition is met.
For example, if the product weighs 25kg, then simply add another price on top.
Here's my code in functions.php but it doesn't seem to work when i refresh the shopping cart.
$chosen_shipping_state = WC()->customer->get_shipping_postcode();
$chosen_state = $chosen_shipping_state;
// Get the selected shipping price
$chosen_shipping_method_price = WC()->session->get('cart_totals')['shipping_total'];
if ($cartweight >= 25 && $cartweight <= 50) {
WC()->session->set('shipping_total', '100');
do_action( 'woocommerce_cart_updated' );
}
So the current price of the selected courier is $5.50.
If the product weighs over 25 kgs, how do I made the courier price set to $11 and the shopping cart automatically refreshes with this amount?
First, you must choose shipping method ID.
Second, You need to put this code on your functions.php file inside your active theme directory. If your code is not working, please try to clear WooCommerce transient. In order to clear your WooCommerce transient, you can go to WooCommerce -> Status. Then click on Tools tab and you will see WooCommerce Transients. Click on the Clear Transients button.
add_filter( 'woocommerce_package_rates', 'override_ups_rates' );
function override_ups_rates( $rates ) {
foreach( $rates as $rate_key => $rate ){
// Check if the shipping method ID is UPS for example
if( ($rate->method_id == 'flexible_shipping_ups') ) {
// Set cost to zero
$rates[$rate_key]->cost = 5.50;
}
}
return $rates;
}
Related
I'm trying to change the shipping label based on whether any of the products in cart has an addon.
For that purpose, I am using How can I modify a Woocommerce shipping label answer code, with some changes to suit my needs.
The HTML:
<input type="hidden" name="shipping_method[0]" data-index="0" id="shipping_method_0_flat_rate5" value="flat_rate:5" class="shipping_method">
<label class="shipping__list_label" for="shipping_method_0_flat_rate5">Standard Shipping (2-3 days): <span class="woocommerce-Price-amount amount"><bdi>3,95<span class="woocommerce-Price-currencySymbol">€</span></bdi></span></label>
So the label Standard Shipping (2-3 days) must display Shipping with customization (4-6 days) if a certain condition is met (in this case: the field custom_text is not empty).
The functions.php:
//Change Standard Shipping Label if product is customized
add_filter( 'woocommerce_cart_shipping_method_full_label', 'change_shipping_label', 10, 2 );
function change_shipping_label( $full_label, $method ){
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( !empty( $cart_item['custom_text'] ) ) {
$full_label = str_replace( "Standard Shipping (2-3 days)", "Shipping with customization (4-6 days)", $full_label );
}
}
return $full_label;
}
This was a success. If the condition is met the label changes to Shipping with customization (4-6 days) prior checkout.
However, the label change does not apply after checkout (thank you page and emails, it displays the original "Standard Shipping (2-3 days)".
Can I display this label change also on thank you page and emails?
Update 2
Based on the provided code and informations, to change specific shipping label on orders, using shipping rate Id to target the desired shipping method:
add_action( 'woocommerce_checkout_create_order_shipping_item', 'action_wc_checkout_create_order_shipping_item', 10, 4 );
function action_wc_checkout_create_order_shipping_item( $item, $package_key, $package, $order ) {
// Targeting "flat_rate:5" by its instance ID
if( $item->get_instance_id() == 5 ) {
$item->set_method_title( "Shipping with customization (4-6 days)" );
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Related: Change specific shipping method title on WooCommerce orders after checkout
This question already has an answer here:
Enable free shipping for two or more cart items in Woocommerce
(1 answer)
Closed 2 years ago.
How to make the price of shipping is free of charge if
The requested quantity are more than 2 pieces.
Or the order cost more than $500.
For 2 > items try this.
Put this code in functions.php of your child-theme files.
add_filter( 'woocommerce_shipping_free_shipping_is_available',
'free_shipping_two_items', 10, 3 );
function free_shipping_two_items( $is_available, $package, $shipping_method )
{
$item_count = WC()->cart->get_cart_contents_count();
if ( $item_count == 1 ) {
$is_available = false;
} elseif ($item_count > 2) {
$is_available = true;
}
return $is_available;
}
For price free shipping - this is major function of woocommerce shipping settings. After you set your free shipping settings use this article here to hide all other shipping methods.
Link
This question already has an answer here:
Display message based on cart items count in WooCommerce cart
(1 answer)
Closed 3 years ago.
I want to display a message on my WooCommerce Cart Page, that tells my customers, how much they need to purchase to get a free gift.
I already got the following code which works, but I have one problem.
When customers update their cart or the quantity the following code does not update (because the page does not reload).
<?php $e_cart = WC()->cart->cart_contents_total * 1.25;?>
<?php $e_cart_remaining = 300 - $e_cart; ?>
<?php
if ( $e_cart < 300 ) {
echo "Get a free gift, when you purchase for ${e_cart_remaining} more.";
}?>
So the problem is, that if a customers has goods for 250 in his cart the message will say: "Purchase for 50 more to get a free gift". (Because you will get a free gift at 300). But if they change the quantity of one of the products the text still says 50. (Because the page have not updated)
How do i trigger this script or block of code every time the cart is updated?
Thank you very much.
To display a custom message on cart page based on cart amount, use the following:
// On cart page only
add_action( 'woocommerce_check_cart_items', 'custom_total_item_quantity_message' );
function custom_total_item_quantity_message() {
$e_cart = WC()->cart->cart_contents_total * 1.25;
$e_cart_remaining = 300 - $e_cart;
if( is_cart() && $e_cart < 300 ){
wc_print_notice( sprintf( __("Get a free gift, when you purchase for %s more.", "woocommerce"), $e_cart_remaining ), 'notice' );
}
}
Code goes in function.php file of your active child theme (or active theme).
This question already has answers here:
Replace product zero displayed price with a custom text in Woocommerce 3
(1 answer)
Customize "Add to cart" button for a specific product category in WooCommerce
(2 answers)
Closed 4 years ago.
I have a product X in several sizes (added as variation products of the main product X). For small sizes the price is available and customers can shop it online. However, for large sizes of this product X, it is not shopable online. Therefore for some variations of this product X I would like to remove the add to cart button and change it to a button who goes to my "request a quote" page.
My idea was that if I set the variable product price to 0 that the add to cart button goes away and the "request a quote" button appears.
Any ideas on how to do this in woocommerce (php)?
put this function in your function.php
add_filter('woocommerce_get_price_html', 'requestQuote', 10, 2);
function requestQuote($price, $product) {
if ( $price == wc_price( 0.00 ) ){
remove_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_add_to_cart', 30 );
return 'Request Quote';
}
else{
return $price;}
}
you just need to make some condition with if function
$price = 2000;
$response = 'Add To Cart;
if(size = XL)
{
$price = 0;
$response = "You can Request another Size";
}
In woocommerce, I am trying to find a way to only allow a product to be added a to cart only when a specific cart total amount is reached.
Example: We want to sell a bumper sticker for $1, but only if a user already has $25 worth of other products already in the cart. This is similar to Amazon's "add on" feature. However I can't find a similar WooCommerce plugin or function.
I have tried yet some code without success… Any help will be appreciated.
Can be done with a custom function hooked in woocommerce_add_to_cart_validation filter hook, where you will define:
a product Id (or multiple product Ids).
the threshold cart amount to be reached.
It will avoid for those defined products to be added to cart (displaying a custom notice) until that specific cart amount is reached.
The code:
add_filter( 'woocommerce_add_to_cart_validation', 'wc_add_on_feature', 20, 3 );
function wc_add_on_feature( $passed, $product_id, $quantity ) {
// HERE define one or many products IDs in this array
$products_ids = array( 37, 27 );
// HERE define the minimal cart amount that need to be reached
$amount_threshold = 25;
// Total amount of items in the cart after discounts
$cart_amount = WC()->cart->get_cart_contents_total();
// The condition
if( $cart_amount < $amount_threshold && in_array( $product_id, $products_ids ) ){
$passed = false;
$text_notice = __( "Cart amount need to be up to $25 in order to add this product", "woocommerce" );
wc_add_notice( $text_notice, 'error' );
}
return $passed;
}
Code goes in function.php file of your active child theme (active theme).
Tested and works.