【问题标题】:Add a product note field in single product pages in Woocommerce在 Woocommerce 的单个产品页面中添加产品备注字段
【发布时间】:2018-11-07 16:35:15
【问题描述】:

我想在单一产品详细页面中为用户创建自定义订单备注。这个可以使用没有插件的php来完成。我附上了屏幕截图和网站 URL 以供参考。

已尝试在function.php 中使用此代码,它在结帐页面上工作,而不是在产品详细信息 页面中。任何人都可以帮助我实现这一目标。

add_action('woocommerce_after_order_notes', 'customise_checkout_field');

    function customise_checkout_field($checkout)
    {
    echo '<div id="customise_checkout_field"><h2>' . __('Heading') . '</h2>';
    woocommerce_form_field('customised_field_name', array(
    'type' => 'text',
    'class' => array(
    'my-field-class form-row-wide'
    ) ,
    'label' => __('Customise Additional Field') ,
    'placeholder' => __('Guidence') ,
    'required' => true,
    ) , $checkout->get_value('customised_field_name'));
    echo '</div>';
    }

website url

【问题讨论】:

    标签: php jquery wordpress woocommerce custom-fields


    【解决方案1】:

    要使其按您的意愿工作,请尝试以下代码,您的产品说明将显示在产品元下方的产品页面中。在我的代码中,我在添加到购物车表单中添加了一个隐藏字段,一些 jQuery 代码会即时将 textarea 字段的内容添加到隐藏字段中。这样,产品备注可以在购物车项目中保存为自定义数据。

    现在这个答案将无法处理按顺序保存数据并在结帐后显示它,因为这对于这个问题来说并不明确且过于宽泛。

    // Add a custom product note below product meta in single product pages
    add_action('woocommerce_single_product_summary', 'custom_product_note', 100 );
    function custom_product_note() {
    
        echo '<br><div>';
    
        woocommerce_form_field('customer_note', array(
            'type' => 'textarea',
            'class' => array( 'my-field-class form-row-wide') ,
            'label' => __('Product note') ,
            'placeholder' => __('Add your note here, please…') ,
            'required' => false,
        ) , '');
    
        echo '</div>';
    
        //
        ?>
        <script type="text/javascript">
        jQuery( function($){
            $('#customer_note').on( 'input blur', function() {
                $('#product_note').val($(this).val());
            });
         });
        </script>
    
        <?php
    }
    
    // Custom hidden field in add to cart form
    add_action( 'woocommerce_before_add_to_cart_button', 'hidden_field_before_add_to_cart_button', 5 );
    function hidden_field_before_add_to_cart_button(){
        echo '<input type="hidden" name="product_note" id="product_note" value="">';
    }
    
    // Add customer note to cart item data
    add_filter( 'woocommerce_add_cart_item_data', 'add_product_note_to_cart_item_data', 20, 2 );
    function add_product_note_to_cart_item_data( $cart_item_data, $product_id ){
        if( isset($_POST['product_note']) && ! empty($_POST['product_note']) ){
            $product_note = sanitize_textarea_field( $_POST['product_note'] );
            $cart_item_data['product_note'] = $product_note;
        }
        return $cart_item_data;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。


    如果您想在添加到购物车按钮后显示此字段,您将使用以下更短的代码:

    // Add a custom product note after add to cart button in single product pages
    add_action('woocommerce_after_add_to_cart_button', 'custom_product_note', 10 );
    function custom_product_note() {
    
        echo '<br><div>';
    
        woocommerce_form_field('product_note', array(
            'type' => 'textarea',
            'class' => array( 'my-field-class form-row-wide') ,
            'label' => __('Product note') ,
            'placeholder' => __('Add your note here, please…') ,
            'required' => false,
        ) , '');
    
        echo '</div>';
    }
    
    // Add customer note to cart item data
    add_filter( 'woocommerce_add_cart_item_data', 'add_product_note_to_cart_item_data', 20, 2 );
    function add_product_note_to_cart_item_data( $cart_item_data, $product_id ){
        if( isset($_POST['product_note']) && ! empty($_POST['product_note']) ){
            $product_note = sanitize_textarea_field( $_POST['product_note'] );
            $cart_item_data['product_note'] = $product_note;
        }
        return $cart_item_data;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。


    可以通过以下方式访问此自定义购物车商品数据:

    foreach( WC()->cart->get_cart() as $cart_item ){
        if( isset($cart_item['product_note']) )
           echo $cart_item['product_note'];
    }
    

    【讨论】:

    • 作为信息添加,textarea 的位置在form 之外,因此需要 jQuery 来解决这个问题。 :)
    • @LoicTheAztec 添加的文本未显示在订单和购物车页面中
    • @LoicTheAztec 我想在产品详细信息页面中显示,但问题是输入的数据要执行购物车和结帐页面,最后可以从仪表板订单部分看到数据。
    • @LoicTheAztec 好的
    【解决方案2】:

    以下是如何在产品页面添加自定义字段的完整流程,该值将被保存、显示在订单详细信息和电子邮件中。

      // Display custom field on single product page
        function d_extra_product_field(){
            $value = isset( $_POST['extra_product_field'] ) ? sanitize_text_field( $_POST['extra_product_field'] ) : '';
            printf( '<label>%s</label><input name="extra_product_field" value="%s" />', __( 'Enter your custom text' ), esc_attr( $value ) );
        }
        add_action( 'woocommerce_before_add_to_cart_button', 'd_extra_product_field', 9 );
    
        // validate when add to cart
        function d_extra_field_validation($passed, $product_id, $qty){
    
            if( isset( $_POST['extra_product_field'] ) && sanitize_text_field( $_POST['extra_product_field'] ) == '' ){
                $product = wc_get_product( $product_id );
                wc_add_notice( sprintf( __( '%s cannot be added to the cart until you enter some text.' ), $product->get_title() ), 'error' );
                return false;
            }
    
            return $passed;
    
        }
        add_filter( 'woocommerce_add_to_cart_validation', 'd_extra_field_validation', 10, 3 );
    
         // add custom field data in to cart
        function d_add_cart_item_data( $cart_item, $product_id ){
    
            if( isset( $_POST['extra_product_field'] ) ) {
                $cart_item['extra_product_field'] = sanitize_text_field( $_POST['extra_product_field'] );
            }
    
            return $cart_item;
    
        }
        add_filter( 'woocommerce_add_cart_item_data', 'd_add_cart_item_data', 10, 2 );
    
        // load data from session
        function d_get_cart_data_f_session( $cart_item, $values ) {
    
            if ( isset( $values['extra_product_field'] ) ){
                $cart_item['extra_product_field'] = $values['extra_product_field'];
            }
    
            return $cart_item;
    
        }
        add_filter( 'woocommerce_get_cart_item_from_session', 'd_get_cart_data_f_session', 20, 2 );
    
    
        //add meta to order
        function d_add_order_meta( $item_id, $values ) {
    
            if ( ! empty( $values['extra_product_field'] ) ) {
                woocommerce_add_order_item_meta( $item_id, 'extra_product_field', $values['extra_product_field'] );           
            }
        }
        add_action( 'woocommerce_add_order_item_meta', 'd_add_order_meta', 10, 2 );
    
        // display data in cart
        function d_get_itemdata( $other_data, $cart_item ) {
    
            if ( isset( $cart_item['extra_product_field'] ) ){
    
                $other_data[] = array(
                    'name' => __( 'Your extra field text' ),
                    'value' => sanitize_text_field( $cart_item['extra_product_field'] )
                );
    
            }
    
            return $other_data;
    
        }
        add_filter( 'woocommerce_get_item_data', 'd_get_itemdata', 10, 2 );
    
    
        // display custom field data in order view
        function d_dis_metadata_order( $cart_item, $order_item ){
    
            if( isset( $order_item['extra_product_field'] ) ){
                $cart_item_meta['extra_product_field'] = $order_item['extra_product_field'];
            }
    
            return $cart_item;
    
        }
        add_filter( 'woocommerce_order_item_product', 'd_dis_metadata_order', 10, 2 );
    
    
        // add field data in email
        function d_order_email_data( $fields ) { 
            $fields['extra_product_field'] = __( 'Your extra field text' ); 
            return $fields; 
        } 
        add_filter('woocommerce_email_order_meta_fields', 'd_order_email_data');
    
        // again order
        function d_order_again_meta_data( $cart_item, $order_item, $order ){
    
            if( isset( $order_item['extra_product_field'] ) ){
                $cart_item_meta['extra_product_field'] = $order_item['extra_product_field'];
            }
    
            return $cart_item;
    
        }
        add_filter( 'woocommerce_order_again_cart_item_data', 'd_order_again_meta_data', 10, 3 );
    

    【讨论】:

    • 可以在woocommerce_product_meta_end钩子上方添加自定义文字吗?
    • 我还发现部分使用了这个kathyisawesome.com/add-a-custom-field-to-woocommerce-product。我希望在产品元之后自定义文本
    • 是的,它基于上面的链接,您可以在字段后添加 d_extra_product_field 函数。
    • 是的。但我需要根据我的截图添加到 sku 和类别下方的注释部分
    • 你可以添加很多自定义字段,我给你一个字段的例子。
    【解决方案3】:

    您在结帐页面上使用了钩子。
    Here is a visual Guide of Hooks 用于产品页面。

    类别下有 2 个可用的钩子:

    • woocommerce_product_meta_end
    • woocommerce_share

    所以,只需替换

    woocommerce_after_order_notes

    woocommerce_product_meta_end

    在你的 add_action() 中

    add_action('woocommerce_product_meta_end', 'customise_checkout_field');
    
    function customise_checkout_field($checkout)
    {
    echo '<div id="customise_checkout_field"><h2>' . __('Heading') . '</h2>';
    woocommerce_form_field('customised_field_name', array(
    'type' => 'text',
    'class' => array(
    'my-field-class form-row-wide'
    ) ,
    'label' => __('Customise Additional Field') ,
    'placeholder' => __('Guidence') ,
    'required' => true,
    ) , $checkout->get_value('customised_field_name'));
    echo '</div>';
    }
    

    【讨论】:

    • 你检查过视觉指南吗?你的函数会产生错误吗?
    • 我检查了这个钩子......它工作正常,也许你的函数出错了
    • 不确定。只是我替换了你的代码。不管怎样,谢谢你的努力
    猜你喜欢
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    相关资源
    最近更新 更多