简体   繁体   English

Woocommerce - 使用Javascript / PHP添加产品

[英]Woocommerce — Adding Products with 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. 输入是3个无线电复选框。 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. 这里没有太多的信息,但这里有一些有用的WooCommerce钩子可以给你一个起点/操作顺序。 When overriding any native action in WordPress or its plugins. 覆盖WordPress或其插件中的任何本机操作时。 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. 我建议给它一个镜头,如果你仍然有问题返回代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM