代码之家  ›  专栏  ›  技术社区  ›  Saravana

在WooCommerce的单个产品页面中添加产品注释字段

  •  3
  • Saravana  · 技术社区  · 7 年前

    我要创建 自定义订单注释 在里面 单一产品 用户的详细信息页。这一个可以使用没有插件的PHP。我已附上截图和网站网址供参考。

    已尝试使用此代码 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

    enter image description here

    3 回复  |  直到 7 年前
        1
  •  3
  •   LoicTheAztec    7 年前

    要使其按您想要的方式工作,请尝试以下代码,您的产品说明将显示在产品页面下面的产品元。在我的代码中,我在“添加到购物车”窗体中添加了一个隐藏字段,一些jquery代码会将文本区域字段的内容即时添加到隐藏字段中。这样,产品说明就可以作为自定义数据保存在购物车项目中。

    现在,这个答案将不会处理按顺序保存数据并在签出后显示数据的问题,因为这对于这个问题来说既不明确,也不太宽泛。

    // 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文件中。测试和工作。

    enter image description here


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

    // 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;
    }
    

    enter image description here

    代码位于活动子主题(或活动主题)的function.php文件中。测试和工作。


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

    foreach( WC()->cart->get_cart() as $cart_item ){
        if( isset($cart_item['product_note']) )
           echo $cart_item['product_note'];
    }
    
        2
  •  0
  •   Tobias    7 年前

    您使用一个钩子正好用于签出页面。
    Here is a visual Guide 产品页的挂钩。

    在类别下面有两个挂钩:

    • WooCommerce_产品_meta_端
    • 宇商股份

    所以,只需更换

    具有

    WooCommerce_产品_meta_端

    在添加操作()中

    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>';
    }
    
        3
  •  0
  •   dipmala    7 年前

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

      // 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_add_extra_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 );
    
        // lodat 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 );
    
    
        // displayed 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 );
    
    
        // ad 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');
    
        // agin 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 );
    
    推荐文章