Woocommerce -- Adding Products with Javascript/PHP - php

I have a custom form that users make various selections before being presented with products they can buy. I need to add some logic that if an input has been selected when the user adds a product to the cart, it will also add an additional product to the cart.
The input is 3 radio check boxes. Somehow I need to hook into the add to cart click and check the page to see if one of the radios has been selected and then add the appropriate product but I'm not sure where to start.
I know I'll need to have an action that runs something like:
WC()->cart->add_to_cart( 'product_id', 'quantity');
But I'm not sure how to hook into the initial add to cart click and how to check for the radios on the page.
Thank you for your help.

Not a lot of information to go off of here, but here is a few useful WooCommerce hooks to give you a starting point/ order of operations. When overriding any native action in WordPress or its plugins. You should use the hooks if available.
Add the radio inputs to the single product before the add to cart is added (hook):
add_action("woocommerce_before_add_to_cart_button", "your_input_render_function_here");
Add the new input data from the radio buttons to the cart item (filter):
add_filter("woocommerce_add_cart_item_data", "your_filter_function_to_capture_data_here");
Capture the data and render in cart (filter):
add_filter("woocommerce_get_item_data", "your_cart_render_function");
Finally, save the newly aquired data to the order (hook):
add_action("woocommerce_checkout_create_order_line_item", "your_function_to_add_to_order")
This is one of many directions you can take, depending on your requirements. I recommend giving it a shot, and if you still have issues return with an example of your code.

Related

WooCommerce Meta-key for products in cart

First post here so I'll try to make it as good as possible.
For a WooCommerce / WordPress project, I'm trying to achieve the following. When the cart is empty, there should be a button displayed with the text "Return to store".
Now the button isn't the problem, I've added this with Elementor, now usually I use the JetEngine Dynamic Visibility option to show/hide things.
I've also done this with stock status. There I used the _stock option to only display when there are 5 or more in stock left.
So I'm trying something similar with the cart, whenever there are 1 or more items in the cart, this button needs to be hidden because whenever someone has 1 item in their cart we want them to proceed to the checkout and not return to the shop, because the average product bought per order is 1 for this particular shop.
But the one thing I can figure out is, what key do I need to use to check items in the cart?
I've been trying several options I found online but none seem to work.
Does anyone know how to do this?
Thanks!

Change main price when add to cart using opencart2.3

I have OpenCart v2.3 and I need make customize on product details page.
Explain what I need:
The product take price from the admin panel by default.
I need add custom price when user click add to cart button.
Explain how OpenCart adds product to cart:
When user click on add to cart button, ajaxFunction is called and goes to checkout/cart/add function with all input fields.
Inside function add() in cart controller
$this->cart->add($this->request->post['product_id'], $quantity, $option, $recurring_id);
And system/library/cart/cart.php contains all functions
How can apply this idea?
"How can apply this idea?"
With difficulty, because it will affect a lot of parts of the system.
Perhaps you should consider having a second copy of the product with a different price and your code adjusts just the template so that clicking "Add to Cart" actually adds the second product instead.

Woocommerce separate orders when add to cart is clicked

I put a few custom fields as part of the add to cart form and I'm trying to separate the orders every time the add to cart button is clicked.
Right now when I order an item and fill up the form I get 1 item added to cart and if I fill up the form again with different data and click add to cart the new data gets lost and the count of the first product is increased to 2.
At the same time when viewing the cart page I want to be able to increase the count of separate orders when changing the count and clicking update chart.
What hook can I use to accomplish this? Thanks.
Don't add fields directly to add to cart form.
From admin panel make your product variant product. There you can add product variants like color, size etc. You can add your own variations also. This will automatically create extra fields to add to cart field.
Then, your products will be added separately.
Check out the link:
https://docs.woothemes.com/document/variable-product/

Woocommerce create order from form outside of woocommerce

I have a need to create an advanced product with various discounts and conditional statements. I have tried several plugins including Gravity Forms Product Add Ons and various others but they dont seem to do what I need them to.
My need is I want to have a form the user fills out and I can handle all conditionals and when they hit the Add to cart button it will add their order to cart. I could use create order but not sure because it needs a product ID and I am kind of creating the product on the fly for each customer. I could also create the product as this thread suggests then add to cart but I fear if I manually create a product for each customer that we will have an absurd amount of products.
What would be the best way to create a form and then once filled out and submitted the total and details are sent to cart?
This link was a good resource but doesnt go into detail about the product id or how to do the form. Any guidance on the best way to tackle this?
UPDATE:
Here is a wireframe of what I want to do demonstrating the dynamic text boxes and discount rules. The deposits I am using the Woocommerce Deposits plugin. Just not sure the best way to do this.

disable add to cart if custom options are required.Magento

in my current product view http://goo.gl/pgdoUl I have all required custom options with a pop up windows that show up in case the value is wrong eg. Track/Pole Length - Finished Curtain Drop. If in the text file all the information are wrong it's still possible to add to cart the product.
How in this case it's possible to disable add to cart?
Currently magento uses prototype/validation.js which is the default way magento uses validations. What you need to do here is write jQuery or plain JavaScript function to validate all your entries and on the bind this function to the click event of add to cart button, only if the validation succeeds the add to cart (which is a form) should get submitted.

Categories