Woocommerce Subscription - add coupon to existing subscription - wordpress

I am looking to add the function, where I can offer a user a discount code that affects their next subscription amount.
For example:
I give customer code 1234 to give them 10% off their next months renewal
They enter the code in their My Account area and then Stripe knows that next month to charge them 10% less then the usual amount.
I have seen a few questions on here asking something similar but no answers so thought I would ask myself and see if anyone can offer their assistance.

This is what worked for me. The coupon has a discount type of Recurring Product % Discount and it applies on the next renewal(s) . You can set for how many renewals it is applied to. The code to apply it is:
$subscription->apply_coupon('your-coupon-code');
And I use it like this on the form handler:
if(isset($_POST['sid']) && $_POST['sid'] != ""){
$subscription = wcs_get_subscription( $_POST['sid'] );
if($subscription->get_status()=="active"){
$subscription->apply_coupon($_POST['coupon']); // returns true on success
}
}

Related

Woocommerce - let customer pay for order when order column value is late payment

I have a difficult problem which i'm trying to solve for some time now.. We are using plugin called admin customer order fields, which let's to create a new column for order. For this part I have made second order status which is called "Payment status" - it can be not paid, paid, or late payment. And these statuses work depending on payment type. For example if customer pays via bank, status becomes "pending" and second column becomes "not paid". But the problem is that we are using Partial Payments plugin, which let's customer to pay for order after some time, for example 30 days. But during these 30 days the product will be shipped to his address and he can pay for order later. The logic is like this:
Customer orders product with option to pay after 30 days.
Order status becomes processing / payment status becomes not paid
And administrator can change the order status - if he ships it it becomes completed. So the statuses would be:
Order status - completed / payment status - not paid. And it should still let user to pay for the order even if order status is completed
Is it possible to make, that it customer could pay for order depending on payment status, not default order statuses? As the best what I have found is this hook:
woocommerce_valid_order_statuses_for_payment
But you can only make that the order statuses could be valid for payments, but I need to make it on payment status which is made with custom plugin.
Attaching screenshot to be clear as possible.
The idea is to make that if payment status is "APMOKĖTA" you couldn't pay anymore, and if it's "ATIDĖTAS MOKĖJIMAS" you could pay for your order.
You can try using the woocommerce_order_needs_payment hook like this
add_filter( 'woocommerce_order_needs_payment', 'custom_needs_payment', 10, 2 );
function custom_needs_payment( $needs_payment, $order ){
$payment_status = get_post_meta( $order->get_id(), 'payment_status', true );
if($payment_status == 'APMOKĖTA'){
return false;
}
return true;
}
You might need to change the names of the post meta to whatever it's named on your end

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

WooCommerce Order Status & reduce Stock

i write a payment gateway for woocommerce and i change the order status after payment is success. If status change to processing i reduce the order stock. Everything goes well.
By the way in the plugin settings the admin can change the status for success payments... processing or completed (for physical or virtual products)
function setOrderPaid($OrderID, $status){
$order = new WC_Order($OrderID);
if(!$order){
return false;
}else{
$order->update_status($status);
if($status=="processing") $order->reduce_order_stock();
WC()->cart->empty_cart();
return true;
}
}
After a success payment with changing the order status to processing, i go and change the status manual to completed (backoffice woocommerce->orders) and the system reduce the order stock again.
I have to reduce the stock after the success payment, to prevent problems with other orders on the same product. How can i fix this order reduce problem?
I find this Woocommerce set_status. Maybe this helps... bool $manual_update is this a manual order status change? So the system knows that stock is already reduced???
This has to do with what are considered as paid order statuses in woocommerce. You can customize the list of paid order statuses. The above function will trigger each time the order enters any of those paid order statuses.
Now, your first instinct would be to truncate the paid status list. This may be advisable in a few cases, but in this particular case the instinct is wrong.
What you need to do instead is check the current order status. If the status is not a paid status, only then you should trigger the reduce stock function. This assumes that the order won't jump between a paid and unpaid status repeatedly.

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

WooCommerce checkout minimum order based on city

I am new to woocommerce so I need you help to findout the issue i am trying to solve from last 3 days. In short, I have a pizza store at 5 locations. There is a minimum order per location. In checkout I made a custom city select option field based on it total bill will be accepted or rejected. I am using checkout manager. Its really confusing.
So my question is how can I do this and achieve this result. I don't want to hard code everything because In future locations might change.
Any solution for this problem. Thanks In advance.
function check_min_order($order_id) {
global $woocommerce;
$order = new WC_Order($order_id);
$total = $order->calculate_totals();
if (!$total > 'YOUR MINIMUM AMOUNT HERE') {
wc_add_notice( 'You did not meet the minimum order total.', 'error' );
exit;
}
}
add_action( 'woocommerce_checkout_order_processed', 'check_min_order', 1, 1 );
Add this to your functions.php
I might have the $total variable being set using the wrong command. But this should give you a general idea of how to acheive this. It interupts the order process and checks the total to see if it meets your minimum amount. You can create a series of if statements or a switch statement for each case in your form fields. Whatever the name of the request object for the city is you can use $_REQUEST['your_city_field_name'] to get the value.
If the condition is met then it will return a woocommerce formatted error and stop the processing of the transaction. If not it will continue as normal.

Resources