WooCommerce Subscriptions - Automatic switching of cost and interval - wordpress

I'm using WooCommerce Subscriptions/Memberships and am pretty certain what I'm trying to achieve isn't possible out of the box. Essentially I want three options of billing intervals:
$15 per week for 12 weeks.
$60 per month for 3 months.
$180 up front.
I know how to do the above using Variable Subscriptions, however the problem is that I need it to change over to $9.95 per week for all three of the above after 12 weeks is up. Does anyone have any ideas about how to achieve this?

I've done something similar, but on different trigger. Anyway, I changed the product inside the subscription. All you need to do is to create another product, with the $9.95/week, and when the time comes, change the base product in the subscription, recalculate totals, setup the period and interval and save.
You need to hook woocommerce_subscription_status_expired which passes the subscription, and then manipulate the subscription. something like:
add_action( 'woocommerce_subscription_status_expired', 'change_subscription_base_product', 10, 1 );
function change_subscription_base_product($subscription){
$subscription->remove_order_items();
$args = array(); //you can change here to total etc..
$productId = 123;/Change this to be dynamic or to hard coded product ID
$product = wc_get_product($productId);
$subscription->add_product($product, 1, $args);
$subscription->calculate_totals();
$subscription->set_billing_period(WC_Subscriptions_Product::get_period($productId));
$subscription->set_billing_interval(WC_Subscriptions_Product::get_interval($productId));
$subscription->save();
}

Related

fixed price for specific categories

I would like to know some questions. We have a school where in a short time we will begin to enroll people online, especially with the Covid-19. We only charge 1 tuition even if the same student enrolls in several options. So I would like the final price to be the same regardless of the quantity of items. Well, I have found a code that solves me in part since the coupons do not work for me, discounts do not apply this for me, as a last option I could be worth it temporarily:
add_filter( 'woocommerce_calculated_total', 'change_calculated_total', 10, 2 );
function change_calculated_total( $total, $cart ) {
return $total = 40;
}
So if there are 3/4 (maximum number of items admitted to the basket) they will only pay 40€
This code is from here: Change Cart total using Hooks in Woocommerce 3.2+
This option is not the most correct, since it does not show a discount, it simply "tricks the system". But I would like this rule to affect the articles of a specific categories, only the enrollment, but the rest of the items in my store in other categories are charged at normal price. Could you please help me those 2 questions? Thanks in advance

Woocommerce - Change tax class programmatically on Order and recalcluate

I´m working on a solution to change the tax class based on the country of the customer AND if he can provide a VAT-ID or not.
The store is EU based and providing services to B2B and B2C.
So for all EU based companies that can provide a VAT-ID, no taxes will be charged unless the origin country is the same.
For all customers who cannot provide a VAT-ID, our local taxes will be added.
And so on...
All solutions I have found are only interacting with the cart, but as I´m creating the order programmatically I need another solution.
Is there a way (a hook maybe) to change the tax_class while creating the order?
For testing I tried to add this method but that did not work. It fires the filter but does not change the tax class in the order.
function wc_change_tax_class( $tax_class, $product ) {
$tax_class = 'Zero rate';
return $tax_class;
}
add_filter( 'woocommerce_product_get_tax_class', 'wc_change_tax_class', 1, 2 );
add_filter( 'woocommerce_product_variation_get_tax_class', 'wc_change_tax_class', 1, 2 );
Also I tried this method in the function that creates the order but with the same result. Nothing happend.
$woocommerce->customer->set_is_vat_exempt( true );
Thanks for any help!
After hours of trying to find the reason, the solution was quite easy:
All solutions I found suggested to write 'Zero rate' or 'Zero Rate'. Depending on what the Name was given in the Woocommerce settings. But for me, it does only work if you write it this way 'zero-rate'. So change the table class name and remove any spaces in the woocommerce settings and of course in the variable.
$tax_class = 'zero-rate';
Hope that saves someone's time!

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.

Making the +/- Plus and Minus quantity button go up in increments of 12 in WooCommerce

I have been trying to solve what I thought might be an easy fix but I can't seem to find any information anywhere.
My problem is this:
I am setting up a wholesale store using WooCommerce with the min/max quantity and wholesale store plugins. I want to show a simple product with its single unit price (no problem) but as all products in the store are wholesale they can only be sold in (multiples) cartons of 6 or 12.
The min/max plugin allows me to set a minimum order quantity (say 12 items) but when I click on the -/+ Minus and Plus quantity selector to add another carton (another 12 items) it only adds one number (single item) at a time... E.g. 13, 14, 15, 16 and so on.
So my question is... "Is it possible to modify the 'quantity.php' file so the order quantities only go up in increments of 12?" (E.g. 12, 24, 36, 48, etc.)
I know I could simply set up and show the single carton cost as a simple product or do variables but my client wants to show a per unit price.
Thanks in advance for any feedback you may have.
There's no need to change core, nor even to use WooCommerce's template override. As Rashid points out, there is a filter in place for modifying this value. We just need to use it. Put this in a site-specific plugin.
add_filter( 'woocommerce_quantity_input_step', 'kia_quantity_input_step', 10, 2 );
function kia_quantity_input_step( $step, $product ){
return 6; // the will be the new step value
}
I'm sure you've moved on by now, but for posterity's sake this plug in might do what you are looking for:
http://www.woothemes.com/products/minmax-quantities/
I wanted similar functionality for my cart section which I achieved like this.
In the woocommerce template file forlders navigate to cart->cart.php file then search for something like this
apply_filters( 'woocommerce_quantity_input_step', '1', $_product );
Then change '1' to '12'. I believe it should be similar if you want to change on single product page.
This is quite hard to do without any hackery. If you don't mind changing a core file (which you will have to do again each time you update WooCommerce), you can do the following:
open wp-content/plugins/woocommerce/assets/js/frontend/woocommerce.js
go to line 34 "$qty.val(currentVal + 1);" and change '1' into '12'.
do the same for the minus function on line 52
note that this affects ALL products!

Resources