How to apply discount based on checkout page form fill up - wordpress

I have developed an eCommerce site based on a Wocommerce theme. I have only one product. I am trying to apply a 10% discount if anyone just fills up/answers some certain form fields of questions in the checkout page. I need to grab those important answer hence offering a 10% discount.
Now my question is how can I achieve the checkout page form customization and apply discount conditionally on the checkout page.

the simplest way that come in my mind is this:
create a one time use per user coupon code
ask the user to fill the form
after the form is submitted print the coupon code
Extra:
If you want you can have an exclusive coupon for every single user, to do this create a coupon programmatically and limit the use (only one time)
https://docs.woocommerce.com/document/create-a-coupon-programatically/#
Is it possibile to grow in complexity by programmatically apply coupon in cart, here a little example that need to be customized to fit your needs:
https://businessbloomer.com/woocommerce-apply-coupon-programmatically-product-cart/

I am not 100% sure but you can do it like this:
1 - Write a PHP function and add filter for it with woocommerce_after_checkout_billing_form hook
2 - Add the questions to the checkout form with relevant input fields
3 - Assign unique id and onchange attributes to those input fields
4 - Write a JS function inside the PHP function to check if those fields are filled before submission (onchange functions)
5 - If yes, return the total value with 10% discount using woocommerce_calculate_totals hook and a simple PHP function for 10% discount
I hope it works, if you need more help I can try these after several hours and post the results.

Related

Woocommerce user defined product meta

I was wondering if it where possible to allow users to fill in product meta using an input fields on the products page. The values of these input fields only have to be saved in that specific order.
I started using product attributes but I couldnt find a way of saving the inputs to the attributes, without affecting the global attributes of that product.
Currently i've created post meta fields, but I'm still stuck at the process of saving these values to the current order.
Any ideas anyone?
I recommend you use this plugin https://wordpress.org/plugins/wc-fields-factory/
It does what you are asking for.
If you are more inclined to write your own code, then take a look at this answer from a year ago https://stackoverflow.com/a/27443698/1456997

BIGCOMMERCE - Add required field based on quantity

I am designing a T-shirt store for a client and need to be able to add custom fields to a BigCommerce product based on the user requesting more than one product (tshirt)
So - if the user has 2 products - they would be asked to complete fields for reverse name and size. Is this possible?
There aren't conditional options out of the box in Bigcommerce. You'd have to have unrequired fields that were hidden unless multiple quantities were selected (using javascript to accomplish the hide/show).
Alternatively, you can limit the item so only one can be added to the cart at a time. This could be a negative customer experience though.

WooCommerce: Override product info before adding to cart

I've found the following example (WooCommerce: Add product to cart with price override?) which lets the user add a product to the cart. However, it hard codes the prices directly on the cart itself.
Is there a way to make adjustments to a product instance i.e. costs, append additional user input, etc. and then add the product to the cart?
Thank you very much for everyones help.
Edit:
Having another crack at explaining, as doublesharp has pointed out it's not quite clear.
Joe clicks on a product, he then chooses various options on the product form which then calculates a custom cost after the 'add to cart' button is clicked but before it is added to the cart.
These options need to be completely custom as the calculations involved are outside of scope of a woocommerce 'variable product'. What I'm trying to achieve is the customer being able to add multiple instances of the same product with various costs and info associated with it.
With regards to the first part of your question where you want to add multiple instances of a product with various costs, you can have a look at the following plugins to achieve that objective
Dynamic Pricing
Pricing Deals for WooCommerce
As for the second part of your question where you want to append additional user input to the cart. This can be done using the following steps
Create a custom session and add the custom data to this session.
Add the data from the custom session to the WooCommerce session.
Extract custom data from WooCommerce session and insert it into cart
object.
Display custom data in Cart and Checkout page.
Add custom data as metadata to the order items.
Remove the associated custom data if an item is removed from the
cart.
For a more detailed on the above steps you have a look at the following link,
How to Add Custom Data to WooCommerce Order

woocommerce: how to add new fields to variations form and show them on frontend

using last versions of wordpress, woocommerce and cheope (theme).
the question: how to
add some extra fields in variations form (text and image field types)
show them on frontend so that their values change accordingly on changing of attributes-select values.
e.g.: the site sells candles and uses variable products, for each kind of candle has several dimensions (with specific different prices), each of which has different duration.
The duration is an important element that I want the user to be informed about.
The same to show the available colors all together.
Unluckily in the standard variation form there is no field where I can insert these or other features.
some help please?
updated 07-August-2013
I succeeded in adding to variations form a new field displayed on back-end, as you can see here, by inserting the code you can find here (please see the comments too) in the function.php file of the theme.
Now, what I’m not able to do is get this field to display with each proper value on front-end (please take a look to the last comment in the above linked page).
Any tip or trick?
thanks
1)Add product attribute and check checkbox Used for variations and Visible on the product page
2)go to variations and add new and fill all fields price and stock must add on them
Your producr display variations and their data

woocommerce add_to_cart

When I program:
$woocommerce->cart->add_to_cart( $group_product_id, 1);
with $group_product_id being the product post ID (e.g. 300), the shopping cart shows a quantity of 2 not 1.
When I directly enter the URL (e.g.http://www.mystore.com/shop/stuff?add_to_cart=300), the shopping cart shows a quantity of 1 (the correct expected behavior).
Any suggestions would be really helpful.
Thank you.
P.S. Bonus question: is there any way to program adding multiple items to the cart (not grouped or variations) before taking the user to the cart page?
Grouped products cannot be added to cart directly. Group product is actually a group of simple products. So $group_product_id should be the id of any of the simple products which constitute the grouped product.
The first two parameters you must use on $woocommerce->cart->add_to_cart, are, $product->id and/or quantity to add. The post id has nothing to do here.
You can yes add multiple products at once using ajax calls, but is not so simple to describe on a comment, will depend on your configuration, your theme, etc, but yes, it´s posible, in fact i recently did something like that. The point is to use syncronized ajax calls that actualy call the add_to_cart function. You can use jquery for triggering many add to cart buttons at same time.

Resources