So I'm selling a digital product with WooCommerce but I don't have a shop page or anything, I just have a big CTA button on my landing page which adds the given product ID to the cart and proceeds to the checkout page upon click.
But let's say the user wants to go back to the landing page to double-check something and complete the checkout process then the product quantity becomes 2 and they can't edit the cart because I've removed all of the shop pages except for the checkout.
So I need to write a hook/filter function that tells WC to empty the current cart before adding the new product to the cart but I also have this upsell plugin that promotes my upsell products on the checkout submit event, right before the payment is charged.
As you can see, I need to check the product ID so my function wouldn't empty the cart if it's an Upsell product, which would be something like the example below, in pseudo-code;
add_filter('woocommerce_add_to_cart_validation', function(...$filterArgs){
(int) $productID; // ID of the product being added to cart
(int) $upsellProductID; // ID of the Upsell product
(bool) $isCartEmpty; // check if the current cart is empty or not
if($productID !== $upsellProductID && $isCartEmpty) {
emptyCartFunctionOfWooCommerce();
return $cartVariableFromFilterArgs
}
});
I'm not fluent in WP hooks yet so do you have any idea how can I do this? Thank you so much.
EDIT: I just did it without ID checking and it didn't interfere with the Upsell function. If anyone has the same issue, you can use something like the code below.
// This will empty the current cart before adding the product to the cart.
add_filter( 'woocommerce_add_to_cart_validation', function($passed, $product_id, $quantity){
if (!WC()->cart->is_empty()) {
WC()->cart->empty_cart();
}
return $passed;
}, 20, 3 );
Edit the product, go to Inventory, and select "Sold individually" "Enable this to only allow one of this item to be bought in a single order". This will stop customers adding more than 1 of that product to the cart.
Once you have done this, change your button to an <a> tag.
Buy Now
This should take you to the cart page and add the product to your cart, but limit it to 1.
Related
I have custom coded Woocommerce template, and used variation to make a bulk discount for my site. For example i have like this one:
but when select Buy 2 for 159.99 variation and press "Add to Cart" button, into cart, for quantuty still stay 1 but should be 2 based on my selected variation. I want to modify my code or function to get updated quantity into cart, based on selected variation. I have hidden my quantity selector, on my site using this function:
function custom_remove_all_quantity_fields( $return, $product ) {return true;}
add_filter( 'woocommerce_is_sold_individually','custom_remove_all_quantity_fields', 10, 2 );
Some tip or idea, how to get this working?
I used the following function to stop people from adding multiple products to their cart, e.g. someone clicks the link and doesn’t buy, then clicks the link again and there’s x2 in their cart.
add_action( 'woocommerce_add_cart_item_data', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
This works for Single Product Checkout links (/checkout/?add-to-cart=53007) , but not for Grouped Products.
With a Grouped Product Checkout link, e.g. /checkout/?add-to-cart=53007&quantity[48582]=1&quantity[46646]=1&quantity[29667]=1, only 1 product from the group is in the checkout, the others have been removed by the above function.
How can I change this function to allow grouped products to work ?
I am using the following php function to set initial product values on a woocommerce store's products for wholesale buyers.
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 10, 2 );
function custom_quantity_input_args( $args, $product ) {
if( ! is_cart() && current_user_can( 'wholesale_buyer' ) )
$args['input_value'] = 2; // Not on cart page
return $args;
}
The code is working for the most part. The products on the product page are initially set to 2 and the products on the cart page are set to user selected values.
The issue is as follows:
The store has a products page with multiple products and a cart at the bottom of the page.
When the default values are set with this php function for the products on the products page, they are also set for the cart displayed at the bottom of the page.
How can I prevent my php function from targeting the initial quantities of the products in the cart at the bottom of the products page, while still changing the initial quantities of the products on the same page?
(I have read through all relevant questions on here that I could find.)
I'm building a website for a wholesale business that is selling plants. They want to provide their customers an option on the cart page to add labels to the plants. Therefore I want to add a custom input field per product in the cart where customers can type in the amount of plants they need. When the update cart button is clicked I want the amount of labels to be added to the cart item data.
I found that I can use woocommerce_add_cart_item_data hook this way:
add_filter( 'woocommerce_add_cart_item_data', 'add_labels', 10, 3 );
function add_labels( $cart_item_data, $product_id, $variation_id ) {
$cart_item_data['amount_of_labels'] = 200;
return $cart_item_data;
}
And when a product is added to the cart, a value of 200 is stored for the key 'amount_of_labels' in the product cart item data. This value is retrievable on the cart and checkout page and can be store in the order item meta data.
My question is:
Is there a same sort of hook/action available for updating cart data on the cart page itself?
I see that there is woocommerce_after_cart_item_quantity_update hook. But it is not working properly for my case. With that hook I can't update my custom cart item data (adding updating or removing).
Example screenshot:
Here you can see what I am trying to build. The inputs in the screenshot take the amount of labels and those should be added to the cart item data when updating the cart.
I wanted to implement a buy 3 free 1 feature, so I wrote a script that detect whether customer has 3 same items in cart and automatically add 1 more same item to the cart. Then using another hook, I overwrite the price of the product to 0.
I googled the solution and used the same approach found on:
WooCommerce: Add product to cart with price override?
woocommerce add custom price while add to cart
Here is the code sample:
function setGiftPriceToZero($cart_object){
foreach($cart_object->cart_contents as $k=>$item):
if(isset($item['variation']['promo']) && ($item['variation']['promo']) == 'buy 3 free 1'):
$item['data']->price = 0;
endif;
endforeach;
}
add_action('woocommerce_before_calculate_totals', 'setGiftPriceToZero');
When Woocommerce calculate the subtotal for the cart, it always add in the original price of the product that is supposed to be free. For example, when I added 3 $100 item to cart, the cart subtotal ends up with $400 instead of $300.
I digged deeper into the Woocommerce code and found that in https://docs.woocommerce.com/wc-apidocs/source-class-WC_Cart.html#1139, $item['data']->get_price() is used which always return the original price for the item.
Is there anyway to fix this using hooks/apis instead of editing Woocommerce core file?
I have found the culprit of this error. It's caused by conflict from another plugin called Woocommerce Role Based Price. This plugin overwrite the cart item price at the end of the cart total calculation flow. This is why get_price() function always return the specified price for the item.
Now I only have to edit the plugin file so that it plays nicely with my logic.