【问题标题】:Woocommerce product custom field: Check if the input already existWoocommerce 产品自定义字段:检查输入是否已存在
【发布时间】:2018-08-17 10:47:42
【问题描述】:

我安装了这个“WC Fields Factory”插件来向产品添加新字段,但如果它的值已经存在于数据库中,我需要检查这个新文本框。 举个例子,比如在注册中你不能使用已经在使用的电子邮件。

我必须像在屏幕中一样添加文本字段

【问题讨论】:

  • 你应该避免使用这个插件,因为 StackOverFlow 中有很多相关的问题和问题......请记住,当你使用插件时,你会得到那个插件的限制。
  • 如何在没有插件且已使用检查的情况下向我的产品添加新字段
  • 您想在前端还是后端添加这些字段?什么样的领域? …也许你可以更新你的问题,提供尽可能多的细节…
  • 我更新了帖子

标签: php wordpress validation woocommerce custom-fields


【解决方案1】:

更新 2 - 处理字段验证,只允许唯一的不存在的文本输入

这是在单个产品页面中添加自定义文本字段、将其添加为购物车商品数据、在购物车和结帐页面中显示、将其保存为订单商品数据并在订单和电子邮件通知中显示的完整方法:

// 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'] );
    }
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 2018-01-10
    • 2015-07-04
    • 2021-06-24
    • 1970-01-01
    相关资源
    最近更新 更多