doulian1852 2019-04-22 16:53
浏览 116

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.

  • 写回答

1条回答 默认 最新

  • dongse7261 2019-04-22 21:15
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了