I have a variable product that has variations based on a meal selection, like chicken, beef. I also have a custom field called Badge Name. I want the user to be able to add the product multiple times to the cart as a single quantity, with a different variation and/or badge name. So the cart should look like this:
Variation
Badge Name
Quantity
Full Package - Chicken
Tina
1
Full Package - Chicken
Mike
1
I have added a custom validation, verifying that the quantity is 1. If the quantity is changed to 2 then I get the error as expected. However, I'd like the quantity to reset to 1 but it stays at 2. I can't set the max quantity to 1 because I want the user to be able to add the product to the cart multiple times per order. I can't seem to find the correct function for changing the quantity back to 1 on the product page after a validation error. Am I over-thinking this and the answer is right in front of my nose?
Here is my validation code:
function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
// Product id for 2021 Full Package - Online
$product_id_full = 3164;
// If passed
if ( $passed ) {
// If product is added with $quantity > 1
if ( $product_id_full == $product_id && $quantity > 1 ) {
// Set error message
$message = 'Full Package must be added to the cart individually with dinner selection and badge name for each attendee.';
wc_add_notice( __( $message, 'woocommerce' ), 'error' );
$passed = false;
}
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
Related
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 set the price of all related products on the products page to 10% less = 0.9
The goal is to provide a discount of all related products on the product page but when viewed as a product, give the normal price.
Overall, the idea is to provide incentive that generates up-selling of related products.
I am asking for two things here. ONE: change the product price for related products on the product page (10% off) and TWO: carry that discounted price into the cart and checkout when the related discounted product is added to cart from the product page.
I almost got the first part down, but the code I'm trying to get working is giving me an error saying:
Warning: A non-numeric value encountered
My code so far:
add_filter( 'woocommerce_get_price_html', 'related_product_price_discount', 100, 2 );
function related_product_price_discount( $price, $product ) {
global $woocommerce_loop;
// make sure this is a related product on product page
if( is_product() && $woocommerce_loop['name'] == 'related' ){
$price = $price * 0.9;
}
// return related product price with discount
return apply_filters( 'woocommerce_get_price', $price );
}
The rule in StackOverFlow is one question at the time, so I will answer only your first question, which is related to your code issue...
Note that the hook woocommerce_get_price_html is related to the displayed formatted price.
To avoid the error "Warning: A non-numeric value encountered", you will use the following:
add_filter( 'woocommerce_get_price_html', 'related_product_price_discount', 100, 2 );
function related_product_price_discount( $price_html, $product ) {
global $woocommerce_loop;
// make sure this is a related product on product page
if( is_product() && $woocommerce_loop['name'] == 'related' ){
$price_html = wc_price( wc_get_price_to_display( $product ) * 0.9 );
}
// return related product displayed discounted formatted price
return $price_html;
}
Code goes in functions.php file of your active child theme (active theme). Tested and works.
I set up a minimum quantity for a product category in WooCommerce. In my case I set the minimum quantity to 5 items. The code I use works fine but I would like to add two error messages for the customer:
1) If the customer tries to change the the quantity to less than the minimum by clicking the "-" symbol I would like to have something like: "The minimum quantity of this product is 5, please add at least 5 items to the basket"
2) If the customer clicks the "add to the basket" button I would like to have something like this: "There is a minimum quantity of 5 items for this product. Please check your basket"
Is there some code I can add to my actual code?
add_filter( 'woocommerce_quantity_input_args', 'min_qty_filter_callback', 20, 2 );
function min_qty_filter_callback( $args, $product ) {
$category = 'Noten'; // The targeted product category
$min_qty = 5; // The minimum product quantity
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
if( has_term( $category, 'product_cat', $product_id ) ){
$args['min_value'] = $min_qty;
}
return $args;
}
For add to cart validation add the follows code snippet to validate your item quantity -
function add_to_cart_validation( $flag, $product_id, $qunatity ) {
if( $qunatity < 5 ) { // if quantity less than 5
wc_add_notice( __( 'There is a minimum quantity of 5 items for this product. Please check your basket', 'text-domain' ), 'error' );
$flag = false;
}
return $flag;
}
add_filter( 'woocommerce_add_to_cart_validation', 'add_to_cart_validation', 10, 3 );
I need something like this :
It is possible to set that when we have last 5 pcs on stock , some user role (wholesale) cant order that product ?
For example if we set quantity to 6 pcs than wholesale customer can will order only 1 pcs to stay minimum there 5 pcs and if is lower than 5 pcs than wholesale customer cant order than product ?
I don't know if you requirement is feasible or not. But i have a logic (which may or mayn't be the proper way)
get stock quantity and user role on clicking add to cart button
Check if user role is wholesale customer and stock quantity is the required one
if yes then block product from being added to cart
This is the basic implementation of the above logic. I have just checked the code and found that product is not added to cart. You can build on the code to meet your requirements.
function stock_quantity_validation($valid, $product_id, $quantity){
$qty = 7; // Your required stock quantity
if( is_user_logged_in() ) {
$user = wp_get_current_user();
$roles = ( array ) $user->roles;
if ( in_array( 'wholesale_customer', $roles ) ) {
$stock = get_post_meta( $product_id, '_stock', true ); // Getting stock quantity of product
if($stock < $qty){
$valid = false;
}
}
}
return $valid;
}
add_filter('woocommerce_add_to_cart_validation','stock_quantity_validation', 10, 3);
I have a product added through woocommerce for my client's website.
Now, I want to show "ADD TO CART" only if the product's qty is 3 or below. If the Qty is more than 3, then the "ADD TO CART" button should be disabled and another button called "CONTACT" should be enabled.(Or, it is fine if it displays some error message if the user choose more than 3 quantities).
Simply says, For Quantities 3 or below - we can add to cart
For Quantities more than 3 - display error msg (or) CONTACT button
You can try something like this:
add_action( 'woocommerce_add_to_cart_validation', 'wc_add_to_cart_validation', 11, 3 );
function wc_add_to_cart_validation( $passed, $product_id, $quantity ) {
if ( $quantity > 3 ){
wc_add_notice( __( 'Only 3 or less quantities allowed, please contact us.', 'woocommerce' ), 'error' );
$passed = false;
}
return $passed;
}
this will check up quantity on add to cart validation.
code goes to your current active theme's functions.php