[英]How can I create a Custom Text Box on a WooCommerce Product Page?
I am working on a WordPress website, with WooCommerce functionality. 我正在使用WooCommerce功能的WordPress网站上工作。
I have created 2 Custom Fields, for the Product Data box within the backend of the Product Page, using the following Code: 我使用以下代码为产品页面后端内的产品数据框创建了2个自定义字段:
<?php
function theme_name_woocommerce_custom_fields() {
// Price Per Character
woocommerce_wp_text_input(
array(
'id' => '_character_price',
'label' => 'Price Per Character',
'description' => 'This is the amount a customer pays per letter entered.',
'desc_tip' => 'true',
'placeholder' => 'Enter Amount per Letter (Exclude the £)'
)
);
// Custom Text Box
woocommerce_wp_checkbox(
array(
'id' => '_custom_text_box',
'label' => 'Show Custom Text Box',
'description' => 'Select this box, if you would like a Custom Text Box to appear on the Product's page.',
'desc_tip' => 'true',
)
);
}
add_action( 'woocommerce_product_options_general_product_data', 'theme_name_woocommerce_custom_fields' );
function save_theme_name_woocommerce_custom_field( $post_id ) {
if ( ! empty( $_POST['_custom_text_field'] ) ) {
update_post_meta( $post_id, '_custom_text_field', esc_attr( $_POST['_custom_text_field'] ) );
}
}
add_action( 'woocommerce_process_product_meta', 'save_theme_name_woocommerce_custom_fields' );
?>
In reference to the woocommerce_wp_checkbox
, I would like to create a function whereby when this checkbox is selected, it creates a Custom Text Box on the associated Product's page. 关于
woocommerce_wp_checkbox
,我想创建一个函数,当选中此复选框时,它将在关联的产品页面上创建一个自定义文本框。 This Custom Text Box can then be used by a potential customer to enter a piece of text, which they would like to have printed to the page's Product. 然后,潜在客户可以使用此自定义文本框输入一段文本,他们希望将其打印到页面的产品上。
Is anyone aware of what additional piece of coding I would need to enter, in order to achieve said goal? 是否有人知道我需要输入哪些其他编码才能实现上述目标?
You can override simple/variable product's template file and add custom field there. 您可以覆盖简单/可变产品的模板文件,并在其中添加自定义字段。 Keep logic of show/hide textbox when checkbox is checked there only.
仅在其中选中复选框时,保持显示/隐藏文本框的逻辑。
When add to cart is done, you will get all posted data in POST. 完成添加到购物车后,您将在POST中获取所有过帐的数据。 Grab it from there and put into woocommerce' object.
从那里抓取它,放入woocommerce对象。
======================================================= Edited: ================================================== =====编辑:
You can follow this steps: 您可以按照以下步骤操作:
Copy woocommerce/templates/single-product/add-to-cart/variable.php this template to your child theme and customize it. 将此woocommerce / templates / single-product / add-to-cart / variable.php模板复制到您的子主题并对其进行自定义。 You will see a form tag there.
您将在此处看到一个表单标签。 In that you can put your custom created checkbox(which you added from admin).
您可以在其中放置自定义创建的复选框(从admin添加)。 Also, add a custom textbox(in which user will enter text) - and hide it.
另外,添加一个自定义文本框(用户将在其中输入文本)-并将其隐藏。
Add custom js from functions.php. 从functions.php添加自定义js。 - In this js you can write logic that if checkbox is checked then you will show the textbox, else not.
-在此js中,您可以编写逻辑,如果选中了复选框,则将显示文本框,否则不显示。
Now, when user does add to cart, add this custom textbox data to woocommerce object. 现在,当用户确实将其添加到购物车时,请将此自定义文本框数据添加到woocommerce对象。 How to enter custom data to woocommerce object - you can have step by step details here: https://wisdmlabs.com/blog/add-custom-data-woocommerce-order/
如何将自定义数据输入到woocommerce对象-您可以在此处逐步了解详细信息: https ://wisdmlabs.com/blog/add-custom-data-woocommerce-order/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.