在Woocommerce购物车页面上添加自定义输入字段
我一直在开发一个WooCommerce网站,我有一个任务,我卡住了。我需要在购物车物品表中添加额外的自定义输入字段。在Woocommerce购物车页面上添加自定义输入字段
像例如,如果一个人的订单“2000 YouTube的观看次数”包,然后用鼠标右键下方的项目名称,我希望用户输入他YouTube视频网址。
我知道我可以在产品页面添加自定义输入字段和可以简单地让他们在车页面显示。但我想将用户输入数据带到购物车页面。每个购物车项目都会有一个自定义输入字段。到目前为止,我已经研究了很多,但对我的解决方案没有任何成功。所有我发现要将自定义输入字段数据打印到产品页面上添加的购物车页面上。
任何帮助,将理解使用Google我发现这个教程的
30秒。
http://www.themelocation.com/how-to-add-custom-field-to-woocommerce-checkout-page/
这里是片段。
/**
* Add the field to the checkout page
*/
add_action('woocommerce_after_order_notes', 'some_custom_checkout_field');
functionsome_custom_checkout_field($checkout) {
echo '<div id="some_custom_checkout_field"><h2>' . __('Heading') . '</h2>';
woocommerce_form_field('some_field_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Additional Field'),
'placeholder' => __('Some hint'),
'required' => true,
), $checkout->get_value('some_field_name'));
echo '</div>';
}
/**
* Process the checkout
*/
add_action(‘woocommerce_checkout_process’, ‘some_custom_checkout_field_process’);
functionsome_custom_checkout_field_process() {
// if the field is set, if not then show an error message.
if (! $_POST[‘some_field_name’]){
wc_add_notice(__(‘Please enter value.’), ‘error’);
}
}
/**
* Update the order meta with field value
*/
add_action(‘woocommerce_checkout_update_order_meta’, ‘some_custom_checkout_field_update_order_meta’);
function some_custom_checkout_field_update_order_meta($order_id) {
if (! empty($_POST[‘some_field_name’])) {
update_post_meta($order_id, ‘Some Field’, sanitize_text_field($_POST[‘some_field_name’]));
}
}
我已经实现了这样的事情,它应该工作,我还没有测试过上面的代码。
希望它有帮助。
你浪费了你30秒的时间,你没有正确地阅读我的问题。我提到我想在购物车页面上实现该功能,而不是在Checkout页面上。我知道如何在结帐页面上做到这一点,但我要求购物车页面。 –
我也有这个问题。我已经用它解决了它。像购物车页面一样添加字段,但使用jQuery接收这些值并通过GET请求动态地转移它们(将它们添加到提交按钮),$ _GET以隐藏输入类型将它们填充到结帐页面中。
在购物车页面上添加它似乎是故意让它比应该更难。只需在各个产品页面上使用[Product Add ons](http://www.woothemes.com/products/product-add-ons)。 – helgatheviking