简体   繁体   English

如何在WooCommerce产品页面上创建自定义文本框?

[英]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: 您可以按照以下步骤操作:

  1. 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. 另外,添加一个自定义文本框(用户将在其中输入文本)-并将其隐藏。

  2. 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中,您可以编写逻辑,如果选中了复选框,则将显示文本框,否则不显示。

  3. 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.

相关问题 添加产品时,如何在woocommerce产品页面中制作自定义字段值(必填) - How can i make custom field value required ( compulsory ) in woocommerce product page when adding product WooCommerce - 如何在 single_product_page 下添加自定义页脚(页面元素) - WooCommerce - how can I add a custom made footer (page element) under single_product_page 如何在 Woocommerce 缺货产品文本下方添加单个产品的 Woocommerce 产品价格 - How can I add below of Woocommerce out of stock product text the Woocommerce product price on a single product Woocommerce 如何在我的油墨单品页面中显示每页价格文本? - Woocommerce How can i have a price per page text appear in my ink single product pages? 如何在WooCommerce中创建“动态”按钮以将人们带到“产品”页面? - How can I create a Dynamic button, within WooCommerce, to take people to the Product page? 如何使用 woocommerce 中的产品类别创建页面 - How do I create a page with the product categories from woocommerce WooCommerce-在单个产品页面上创建自定义产品循环 - WooCommerce - create custom product loop on single product page WooCommerce-定制产品页面 - WooCommerce - custom product page 如何在单个页面上返回 Woocommerce 产品库? - How can I return the Woocommerce product gallery on a single page? 如何在WooCommerce产品页面中添加用户输入字段 - How can I add User Input field in WooCommerce Product Page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM