woocommerce add_to_cart - woocommerce

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.

Related

How to apply discount based on checkout page form fill up

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.

Add multiple cross sell products to cart from product page

I'm surprised im asking this as i would assume it is fairly easy to do but i've been trying and playing with plugins for days and not able to get the result i need.
I am trying to add multiple cross sell options to a woocommerce product so that a user can select multiple add-on products and add them to the cart with the main product from the product page.
Fore example: If the main product is a toy car, the add-on options might be batteries, extra remote control, etc. The customer could check the items they want and all would be separately added to the cart (by separately i mean so the customer could later decide to remove the toy car from the cart, but the batteries and extra remote would still remain in the cart until they were also removed).
I have tried various things including: product variations (core), product bundles (woo and yith), product addons (woo and yith), composite products (woo and yith). None can do all of the above.
If anyone can suggest a way to do it with either core or a plugin i would be VERY grateful!
Thanks for reading :)
It's been a while and I assume you already found the answer, but in case you haven't, you may add (manually) all the products that matches the product in the cart. You can do so in the product's page (admin panel) in "linked products", where "Cross-Sells" are the ones that will show in the cart.
The only thing is that by default, WooCommerce allows only 2 products to be linked. You may override that limit by inserting in your theme's "functions.php" file, the following line.
add_filter( 'woocommerce_cross_sells_columns', 'change_cross_sells_columns' );
function change_cross_sells_columns( $columns ) {
return 6;
}
In the example above, "6" is the limit. You may use any number, but you should use it wisely, as too many products shown in the cart will only confuse customers.
I hope this helps.

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

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

Resources