首页 > 解决方案 > 在 WooCommerce 中将自定义产品文本字段作为购物车项目数据传递

问题描述

这里的目标是在产品页面上有一个文本字段。

客户填写并添加到购物车。文本被添加到产品中,并包含在购物车、结帐和订单中。

该字段工作正常,验证工作正常,但文本不包括/转移到购物车、结帐和订单。

这是代码:

add_action( 'woocommerce_before_add_to_cart_button', 'product_add_on', 9 );
function product_add_on() {
    $value = isset( $_POST['_custom_text_add_on'] ) ? sanitize_text_field( $_POST['_custom_text_add_on'] ) : '';
    echo '<div><label>Custom Text Add-On <abbr class="required" title="required">*</abbr></label><p><input name="_custom_text_add_on" value="' . $value . '"></p></div>';
}

add_filter( 'woocommerce_add_to_cart_validation', 'product_add_on_validation', 10, 3 ); 
function product_add_on_validation( $passed, $product_id, $qty ){
    if( isset( $_POST['_custom_text_add_on'] ) && sanitize_text_field( $_POST['_custom_text_add_on'] ) == '' ) {
        wc_add_notice( 'Custom Text Add-On is a required field', 'error' );
        $passed = false;
    }
    return $passed;
}

add_filter( 'woocommerce_add_cart_item_data', 'product_add_on_cart_item_data', 10, 2 ); 
function product_add_on_cart_item_data( $cart_item, $product_id ){
    if( isset( $_POST['_custom_text_add_on'] ) ) {
        $cart_item['custom_text_add_on'] = sanitize_text_field( $_POST['custom_text_add_on'] );
    }
    return $cart_item;
}

add_filter( 'woocommerce_get_item_data', 'product_add_on_display_cart', 10, 2 ); 
function product_add_on_display_cart( $_data, $cart_item ) {
    if ( isset( $cart_item['custom_text_add_on'] ) ){
        $data[] = array(
            'name' => 'Custom Text Add-On',
            'value' => sanitize_text_field( $cart_item['custom_text_add_on'] )
        );
    }
    return $data;
}

add_action( 'woocommerce_add_order_item_meta', 'product_add_on_order_item_meta', 10, 2 ); 
function product_add_on_order_item_meta( $item_id, $values ) {
    if ( ! empty( $values['custom_text_add_on'] ) ) {
        wc_add_order_item_meta( $item_id, 'Custom Text Add-On', $values['custom_text_add_on'], true );
    }
}

add_filter( 'woocommerce_order_item_product', 'product_add_on_display_order', 10, 2 );
function product_add_on_display_order( $cart_item, $order_item ){
    if( isset( $order_item['custom_text_add_on'] ) ){
        $cart_item_meta['custom_text_add_on'] = $order_item['custom_text_add_on'];
    }
    return $cart_item;
}

add_filter( 'woocommerce_email_order_meta_fields', 'product_add_on_display_emails' ); 
function product_add_on_display_emails( $fields ) { 
    $fields['custom_text_add_on'] = 'Custom Text Add-On';
    return $fields; 
}

标签: phpwordpresswoocommerceproductcustom-fields

解决方案


我复制了代码并进行了快速测试,发现您_在两个函数中缺少字段名称的下划线。您在函数中使用$_POST['custom_text_add_on']而不是。那只是一个错误。$_POST['_custom_text_add_on']product_add_on_cart_item_data

add_filter( 'woocommerce_add_cart_item_data', 'product_add_on_cart_item_data', 10, 2 ); 
function product_add_on_cart_item_data( $cart_item, $product_id ){
    if( isset( $_POST['_custom_text_add_on'] ) ) {
        $cart_item['custom_text_add_on'] = sanitize_text_field( $_POST['_custom_text_add_on'] );
    }
    return $cart_item;
}

希望你的问题得到解决


推荐阅读