This question already exists:
How to Set Purchase Limits which a minimum weight requirement before checking out in WordPress Woo commerce/?
Closed 3 years ago.
How to get Woo Commerce product variation values in WordPress .I've three values 250g,500g and 1kg.
I've restrict on checkout process 500g.
Add this code to functions.php. You may check article here
add_action( 'woocommerce_check_cart_items', 'cldws_set_weight_requirements' );
function cldws_set_weight_requirements() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Set the minimum weight before checking out
$minimum_weight = 500;
// Get the Cart's content total weight
$cart_contents_weight = WC()->cart->cart_contents_weight;
// Compare values and add an error is Cart's total weight
if( $cart_contents_weight < $minimum_weight ) {
// Display our error message
wc_add_notice( sprintf('<strong>A Minimum Weight of %s%s is required before checking out.</strong>'
. '<br />Current cart weight: %s%s',
$minimum_weight,
get_option( 'woocommerce_weight_unit' ),
$cart_contents_weight,
get_option( 'woocommerce_weight_unit' ),
get_permalink( wc_get_page_id( 'shop' ) )
),
'error' );
}
}
}
Related
Found a code for my exact problem and used it , while it works perfect for minimum order for some reason it does not work for when choosing local pickup.
my current shipping setup:
and the website is papabross.gr
I used this code:
add_action( 'woocommerce_check_cart_items', 'wc_minimum_required_order_amount' ); function wc_minimum_required_order_amount() {
// HERE Your settings
$minimum_amount = 25; // The minimum cart total amount
$shipping_method_id = 'local_pickup:10'; // The targeted shipping method Id (exception)
// Get some variables
$cart_total = (float) WC()->cart->total; // Total cart amount
$chosen_methods = (array) WC()->session->get( 'chosen_shipping_methods' ); // Chosen shipping method rate Ids (array)
// Only when a shipping method has been chosen
if ( ! empty($chosen_methods) ) {
$chosen_method = explode(':', reset($chosen_methods)); // Get the chosen shipping method Id (array)
$chosen_method_id = reset($chosen_method); // Get the chosen shipping method Id
}
// If "Local pickup" shipping method is chosen, exit (no minimun is required)
if ( isset($chosen_method_id) && $chosen_method_id === $shipping_method_id ) {
return; // exit
}
// Add an error notice is cart total is less than the minimum required
if ( $cart_total < $minimum_amount ) {
wc_add_notice( sprintf(
__("Η ελάχιστη παραγγελία για αποστολή είναι %s (Η παραγγελία σας μέχρι στιγμής είναι %s).", "woocommerce"), // Text message
wc_price( $minimum_amount ),
wc_price( $cart_total )
), 'error' );
}
}
Not really sure if I am choosing the correct shipping id? Can I use another hook maybe?
I have used a working code I found here and everything works but the local pickup. It still asks for a minimum order.
I wonder if I have used the shipping id wrongly?
After a long search, I found a code that works! I am sharing this in case someone gets the same problem:
// Set a minimum amount of order based on shipping zone & shipping method before checking out.
add_action( 'woocommerce_check_cart_items', 'cw_min_num_products' );
// Only run in the Cart or Checkout pages.
function cw_min_num_products() {
if ( is_cart() || is_checkout() ) {
global $woocommerce;
// Set the minimum order amount, shipping zone & shipping method before checking out.
$minimum = 25;
$county = array( 'GR' );
$chosen_shipping = WC()->session->get( 'chosen_shipping_methods' )[0];
$chosen_shipping = explode( ':', $chosen_shipping );
// Defining var total amount.
$cart_tot_order = WC()->cart->subtotal;
// Compare values and add an error in Cart's total amount.
// happens to be less than the minimum required before checking out.
// Will display a message along the lines.
if ( $cart_tot_order < $minimum && in_array( WC()->customer->get_shipping_country(), $county ) && $chosen_shipping[0] != 'local_pickup' ) {
// Display error message.
wc_add_notice(
sprintf(
'Δεν έχετε φτάσει ακόμη το ελάχιστο ποσό παραγγελίας των %s€.' . '<br/>Δεν υπάρχει ελάχιστη παραγγελία εάν επιλέξετε την παραλαβή από το κατάστημα.'
. '<br />Το τρέχον ποσό της παραγγελίας σας είναι : %s€.',
$minimum,
$cart_tot_order
),
'error'
);
}
}
}
I would like to add a function that is triggered every time that the stock quantity of a product will be changed in the admin product page, such that this function will not allow any reduce of the stock value - but only increase.
This is to prevent an admin user to reduce the stock quantity of the products.
Of course, this function should not be triggered if a product will be in an order, since then of course I would like the stock quantity to be reduced.
I tried the following function in the functions.php but unfortunately did not work.
Since I'm new to woocommerce and php, any ideas that could provide a solid solution to the problem?
// get old and new product stock quantity
function get_old_and_new_product_quantity_stock( $sql, $product_id_with_stock, $new_stock, $operation ) {
$product = wc_get_product( $product_id_with_stock );
$old_stock_quantity = $product->get_stock_quantity();
$new_stock_quantity = $new_stock;
echo $old_stock_quantity, $new_stock_quantity;
if ($new_stock_quantity < $old_stock_quantity) {
$new_stock = $old_stock_quantity;
$new_stock_quantity = $old_stock_quantity;
}
return $sql;
}
add_filter( 'woocommerce_update_product_stock_query', 'get_old_and_new_product_quantity_stock', 10, 4 );
You can use the update_post_meta action hook to check if the new value is less than the previous value and display error message.
This will work for quick edit and for product edit page. But the wp_die on product page will look bad so use the javascript to prevent submitting on product edit page (there was another question about it yesterday)
Be sure to test this snippet and create some orders that will reduce the stock automatically. I added is_admin() check but please do a good test.
add_action( 'update_post_meta', 'prevent_reducing_stock_metadata', 10, 4 );
function prevent_reducing_stock_metadata( $meta_id, $post_id, $meta_key, $meta_value ) {
// Check if the meta key is _stock and the new value is less than the previous value
if ( '_stock' == $meta_key && $meta_value < get_post_meta( $post_id, '_stock', true ) ) {
// Check if this is an update from the WordPress admin area
if ( is_admin() ) {
wp_die( __( 'Error: You cannot reduce the stock level for this product.' ), 'error' );
}
}
}
I am trying to change product price in cart using the following function:
add_action( 'woocommerce_before_shipping_calculator', 'add_custom_price'
);
function add_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = 400;
}
}
It was working correctly in WooCommerce version 2.6.x but not working anymore in version 3.0+
How can I make it work in WooCommerce Version 3.0+?
Thanks.
Update 2021 (Handling mini cart custom item price)
With WooCommerce version 3.0+ you need:
To use woocommerce_before_calculate_totals hook instead.
To use WC_Cart get_cart() method instead
To use WC_product set_price() method instead
Here is the code:
// Set custom cart item price
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);
function add_custom_price( $cart ) {
// This is necessary for WC 3.0+
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Avoiding hook repetition (when using price calculations for example | optional)
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
$cart_item['data']->set_price( 40 );
}
}
And for mini cart (update):
// Mini cart: Display custom price
add_filter( 'woocommerce_cart_item_price', 'filter_cart_item_price', 10, 3 );
function filter_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
if( isset( $cart_item['custom_price'] ) ) {
$args = array( 'price' => 40 );
if ( WC()->cart->display_prices_including_tax() ) {
$product_price = wc_get_price_including_tax( $cart_item['data'], $args );
} else {
$product_price = wc_get_price_excluding_tax( $cart_item['data'], $args );
}
return wc_price( $product_price );
}
return $price_html;
}
Code goes in functions.php file of your active child theme (or active theme).
This code is tested and works (still works on WooCommerce 5.1.x).
Note: you can increase the hook priority from 20 to 1000 (or even 2000) when using some few specific plugins or others customizations.
Related:
Set cart item price from a hidden input field custom price in Woocommerce 3
Change cart item prices based on custom cart item data in Woocommerce
Set a specific product price conditionally on Woocommerce single product page & cart
Add a select field that will change price in Woocommerce simple products
With WooCommerce version 3.2.6, #LoicTheAztec's answer works for me if I increase the priority to 1000.
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 1000, 1);
I tried priority values of 10,99 and 999 but the price and total in my cart did not change (even though I was able to confirm with get_price() that set_price() had actually set the price of the item.
I have a custom hook that adds a fee to my cart and I'm using a 3rd party plugin that adds product attributes. I suspect that these WooCommerce "add-ons" introduce delays that require me to delay my custom action.
I am working on a WooCommerce website and I am trying to restrict a product to be purchased only if a coupon is applied for it, so it should not be processed without adding a coupon code.
User must enter a coupon code to be able to order this specific product (not on all other products).
We don't need it to target a specific coupon to allow checkout, we need it to require any coupon because for this specific product we have around 150+ coupons.
Based on Allow specific products to be purchase only if a coupon is applied in Woocommerce code thread:
add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_for_specific_items' );
function mandatory_coupon_for_specific_items() {
$targeted_ids = array(37); // The targeted product ids (in this array)
$coupon_code = 'summer2'; // The required coupon code
$coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );
// Loop through cart items
foreach(WC()->cart->get_cart() as $cart_item ) {
// Check cart item for defined product Ids and applied coupon
if( in_array( $cart_item['product_id'], $targeted_ids ) && ! $coupon_applied ) {
wc_clear_notices(); // Clear all other notices
// Avoid checkout displaying an error notice
wc_add_notice( sprintf( 'The product"%s" requires a coupon for checkout.', $cart_item['data']->get_name() ), 'error' );
break; // stop the loop
}
}
}
How to handle: prevent checkout if no coupon has been applied when certain products in cart.
It is a matter of removing or adjusting a few conditions. For example, checking whether coupons have been applied with
empty - Determine whether a variable is empty
So you get:
function action_woocommerce_check_cart_items() {
// The targeted product ids (in this array)
$targeted_ids = array( 813, 30 );
// Get applied coupons
$coupon_applieds = WC()->cart->get_applied_coupons();
// Empty coupon applieds
if ( empty ( $coupon_applieds ) ) {
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
// Check cart item for defined product Ids
if ( in_array( $cart_item['product_id'], $targeted_ids ) ) {
// Clear all other notices
wc_clear_notices();
// Avoid checkout displaying an error notice
wc_add_notice( sprintf( 'The product "%s" requires a coupon for checkout.', $cart_item['data']->get_name() ), 'error' );
// Optional: remove proceed to checkout button
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
// Break loop
break;
}
}
}
}
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10, 0 );
How Can ı set this situation ?
How can i add minimum order limit to flat rate product by zone ?
we can only free shipping products.
You can use the following for this, based on the zone id
function zone_based_minimum_amount() {
// Only run it in Cart or Checkout pages
if( is_cart() || ( is_checkout() && ! is_wc_endpoint_url() ) ) {
// HERE below your targeted zone IDs
$targeted_zones_ids = array( 1, 2 );
$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] );
$zone_name = $shipping_zone->get_zone_name(); // Get the zone name
$zone_id = $shipping_zone->get_id(); // Get the zone ID
// Total (before taxes and shipping charges)
$total = WC()->cart->subtotal;
// HERE Set minimum cart total amount
$min_total = 100;
// Add an error notice is cart total is less than the minimum required
if( $total <= $min_total && in_array( $zone_id, $targeted_zones_ids ) ) {
// Display an error message
wc_add_notice( '<strong>' . sprintf(
__("A minimum total purchase amount of %s is required to checkout."),
wc_price($min_total)
) . '<strong>', 'error' );
// Removing the Proceed to checkout button from the Cart page
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
}
}
add_action( 'woocommerce_check_cart_items', 'zone_based_minimum_amount', 10, 0 );