Woocommerce - 使用Javascript / 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.