How to detect reorder of old order in woocommerce? - wordpress

How to detect reorder of old order in woocommerce?
I want to remove any product if customer is reordering any old order, please help.
I know how to remove any product but I am unable to detect that customer is re-ordering any old order.
eg. Suppose user has purchased any order that's have 2 products and when user is trying to purchase same order I want to remove 1 product from the order. I don't want that user remove this product from cart.

After searching a lot and after reading some woocommerce core files I found below solution:
add_action("woocommerce_ordered_again", "dbr_remove_all_product_except_the_subscription_func");
function dbr_remove_all_product_except_the_subscription_func()
{
$cart_items = WC()->cart->get_cart(); /* if needed get cart like this */
/* code here as per your requirement. */
}

Related

Woocommerce How do I add kilojoules from products to each other in the cart

I have a website where each product (food items), lists all the Caloric quantity in KJ. (added as a custom field on the single product page via Woocommerce Nutrition Info plugin)
As the products get added to the cart, I need a total KJ section near the totals, so my customer can see if the mix they are making is appropriate.
Eg. add 1 from A, and 0 to 3 of B, C, D E F. Letters being food items to make their own mix,
Knowing that WooCommerce adds prices, it would make sense that it could somehow do this too in the cart but I'm not smart enough to begin to know how to do it.
I have only limited PHP and Javascript knowledge, so I am totally out of my league and doing my head in.
Does anyone know how to I would add (I'm guessing) custom fields quantities to each other and display a running total to the cart?
I tried writing some code and big surprise it doesn't work, (trying not to be hard on myself but failing abysmally because I'm not a php coder YET!) and I don't know how to make it add up exponentially as products are added to the cart. I have spent weeks googling and Google doesn't understand my question either, hence me reaching out.
add_action( 'nutrition-table-kj', 'kj-value-add-to_cart_function' );
kj-amount-add_to_cart_function () {
$product_id = all;
if ( WC()->cart->get_cart_nutrition-table-kj-value () {
WC()->cart->add_to_cart( $nutrition-table-values );
}
}

Drupal 8: Only add to cart if user has enough credits

I've a site where transactions are based purely on credits. No actual money is exchanged. In such a case, I want to restrict users from adding products to cart if they do not have credits to do so.
In steps of how this should be accomplished, here is the sequence of events which I think should work :
Whenever somebody tries to add a product :
Use Rule "Before adding product to cart"
I need to get the value of the following :
Get user credits
Get sum of current product + cart value
Check for condition if credits is greater than product + cart.
If true, add product to cart else display "Not enough credits".
I use the loyalty points module for the user credits.
How can I set up the rules for this scenario?
Instead of using a module I just used a custom_validation function on commerce_add_to_card_form as suggested by lamp5

How to set a maximum limit in WooCommerce orders per item & per user & per date

I need to limit user's orders in WooCommerce in a way that each user would be able to order some specific products for a limited amount per day. this way the limitation is "per user"/"per product"/"per day" !
for example each user could buy only 10 items of a product each day, and the next day he would be able to buy 10 more.
I have checked almost all plugins related to limiting orders but none of them had all these conditions. I also read some sample codes here like this one. but since I'm new to php and wordpress coding I couldn't figure out how to change it to my preference.
I appreciate any help
I would start with adding a function to the woocommerce_add_to_cart hook.This is a basically a hook that get called when a new product is added to the cart. Then I will work with the users meta where I will check if the user has bought this product already when this has happened. If there is no such data I will then add it to the users meta.
Mind that you have two scenarios here - first, the product is just added to the cart and not purchased, the second one is when the product is purchased. This is important because you will have also the case when the user deletes product from the cart and you will need also a hook there so you can update the user meta. Here you can use woocommerce_remove_cart_item and woocommerce_after_cart_item_quantity_update (check for references here )
And finally you will need to hook on order completion -> check those hooks.
But still, one of the most important parts is how you are going to build the array for the user meta. I would suggest creating an associative array with key value -> the product id. As value of the array you can have another array with date of purchase and products quantity. Something like this:
$users_products = [
product_id1 => [
'date' => timestamp,
'quantity' => 3
],
product_id2 => [
'date' => timestamp,
'quantity' => 3
]
];
Where product_id1 and product_id2 are actual products ids. This way it will be easier for you to loop through the user meta and check if the current product is in the list. Also to avoid making this user meta data too big check if the date of each item is not outdated and if so - delete(unset) this element from the array and update it the user meta with the refreshed data.
There is much more that you will need to code but I think this will be a good start for you.

WooCommerce - Change customer order after it has been placed and paid

Using three plugins:
WooCommerce
Subscriptio
WooEvents
I have created a product which allows for a customer to pay for an event over five instalments.
A customer has placed an order and made their first of the five payments via PayPal and has then contacted me to advise they have booked the wrong event.
The correct event is exactly the same except the dates are a month sooner.
I have added the following snippet to be able to edit 'processing' orders:
add_filter( 'wc_order_is_editable', 'wc_make_processing_orders_editable', 10, 2 );
function wc_make_processing_orders_editable( $is_editable, $order ) {
if ( $order->get_status() == 'processing' ) {
$is_editable = true;
}
return $is_editable;
}
From the order screen within the back-end of WordPress I can see that the the order can be edit by way of removing the product from the order or editing the meta data and cost.
The correct product that should have been ordered has a different product/variation ID.
My question is simply:
Should I remove the incorrectly ordered product from the order and add the correct product (both have the same properties with the exception of the courses dates); or
Should I just change the meta data and increase the stock levels for the incorrectly product back to X and reduce the stock level for the correct product?
Your second option is much better. Change the meta from database if its accessible and then manage the stock accordingly.
I have not yet done this, but according to this exchange ...
How can I add a product to an existing and paid Woocommerce order?
... you can set the order status to "On Hold" and make changes to the order itself. I presume you would then return the status to "Processing".
I would expect that if the application permits those changes, then it would be doing the background meta changes to inventory levels, etc., and spare you the problem of doing so (and the potential errors that may occur when messing with the data tables outside of the application).
Like I said, I haven't done this. But it might be worthwhile doing a little test to make sure it works as it seems to be described in the answer to the other question.
J

Wordpress/WooCommerce - Unique Product Name

Is it possible to add a unique name to a specific product when it's added to the shopping cart using the woocommerce API?
For example suppose I sell pets. The user decides he wants a dog named spot. He then clicks on a particular dog breed (the product) say border collie and that product should appear in the shopping cart as a border collie named spot.
To answer your question, yes. You can change things in your own code by adding filters. So for example, say you want to change the name of the product in the cart. In wp-content/plugins/woocommerce/templates/cart/cart.php there is a filter called 'woocommerce_cart_item_name' where the title is outputted. You can modify this in your own code like so:
function your_function( $title, $cart_item, $cart_item_key ){
/*code to modify title*/
return $title;
}
add_filter('woocommerce_cart_item_name','your_function',10,3);
You could add this to the functions.php of your theme.
How you get the name spot in this function depends on your situation.
Read more about filters at https://codex.wordpress.org/Function_Reference/add_filter

Resources