Empty function clears grouped product - php

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 ?

Related

Update quantity based on variation to cart in WooCommerce

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?

Creating a customer price in woocommerce via API

I have found a plugin that creates customer specific pricing, but seems to be an external API call that seem to not be the solution I am looking for.
https://woocommerce.com/products/wisdm-customer-specific-pricing/
I was confused to how the cart works and how products are stored. But based on this post it showed me how to replace cart prices with customer ID and sku's being passed to the API.
I used this this ticket to start off my answer
So the big thing I needed to figure out is how to call the cart, loop through each product and grab it's product SKU then overwrite the price.
I added the following to my functions.php in my theme to change my cart to display the correct pricing based off of my customer API pricing. This does a loop through all my cart items then make a call to the API and returns a price for the specific customer and sku.
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $key => $value ) {
$response = get_api_response($value['data']->get_sku(), $customer_id);
$custom_price = $response->price;
$value['data']->set_price($custom_price);
}
}

WooCommerce - Empty the current cart on add-to-cart event

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.

How Can I set initial quantities on woocommerce product without changing the on product page cart?

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.)

Woocommerce: Custom field per product on the cart page

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.

Categories