1条回答 默认 最新
- doubo6658 2018-08-17 14:21关注
Update 2 - Handle the field validation, allowing only a unique non existant text input
Here is the complete way to add a custom text field in single product pages, add it as cart item data, display it in cart and checkout pages, save it as order item data and display it on orders and email notifications:
// HERE define your field label name function get_field_label_name(){ return __( "The label name" ); } // Add a custom product note below product meta in single product pages add_action('woocommerce_before_add_to_cart_button', 'my_product_custom_field', 100 ); function my_product_custom_field() { echo '<div class="my-custom-field">'; woocommerce_form_field('custom_field1', array( 'type' => 'text', 'class' => array( 'my-field-class form-row-wide') , 'label' => get_field_label_name(), 'placeholder' => __("The field placeholder…" ), 'required' => true, // Or false ) , ''); echo '</div>'; } // Check if the custom field value is unique add_filter( 'woocommerce_add_to_cart_validation', 'wc_add_on_feature', 20, 3 ); function wc_add_on_feature( $passed, $product_id, $quantity ) { if( isset($_POST['custom_field1']) && ! empty($_POST['custom_field1']) ){ global $wpdb; $label = get_field_label_name(); $value = sanitize_text_field( $_POST['custom_field1'] ); // Check if value exits already $result = $wpdb->get_var( " SELECT COUNT(meta_value) FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key LIKE '$label' AND meta_value LIKE '$value' " ); // If it exist we don't allow add to cart if( $result > 0 ){ // Display an error notice wc_add_notice( sprintf( __( 'This "%s" input already exist. Please choose another one…' ), $value ), 'error' ); $passed = false; } } return $passed; } // Add custom field value to cart item data add_filter( 'woocommerce_add_cart_item_data', 'custom_field_value_to_cart_item_data', 20, 2 ); function custom_field_value_to_cart_item_data( $cart_item_data, $product_id ){ if( isset($_POST['custom_field1']) && ! empty($_POST['custom_field1']) ){ $cart_item_data['custom_data'] = sanitize_textarea_field( $_POST['custom_field1'] ); } return $cart_item_data; } // Display custom cart item data in cart add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 ); function display_custom_item_data( $cart_item_data, $cart_item ) { if ( isset( $cart_item['custom_data'] ) ){ $cart_item_data[] = array( 'name' => get_field_label_name(), 'value' => $cart_item['custom_data'] ); } return $cart_item_data; } // Save and display custom field in orders and email notifications (everywhere) add_action( 'woocommerce_checkout_create_order_line_item', 'custom_field_update_order_item_meta', 20, 4 ); function custom_field_update_order_item_meta( $item, $cart_item_key, $values, $order ) { if ( isset( $values['custom_data'] ) ){ $item->update_meta_data( get_field_label_name(), $values['custom_data'] ); } }
Code goes in function.php file of your active child theme (or active theme). Tested and works.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 聚类分析 设计C-均值算法分类器,对一组二维模式向量进行分类。
- ¥15 stm32c8t6工程,使用hal库
- ¥100 有偿求易语言word文档取doc和docx页数方法或模块
- ¥15 找能接spark如图片的,可议价
- ¥15 关于#单片机#的问题,请各位专家解答!
- ¥15 博通raid 的写入速度很高也很低
- ¥15 目标计数模型训练过程中的问题
- ¥100 Acess连接SQL 数据库后 不能用中文筛选
- ¥15 用友U9Cloud的webapi
- ¥20 电脑拓展屏桌面被莫名遮挡