fixed price for specific categories - wordpress

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

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 );
}
}

Woocommerce Subscription - add coupon to existing subscription

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
}
}

WooCommerce Subscriptions - Automatic switching of cost and interval

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();
}

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