php - 在 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;
}
解决方案
我复制了代码并进行了快速测试,发现您_
在两个函数中缺少字段名称的下划线。您在函数中使用$_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;
}
希望你的问题得到解决
推荐阅读
- docker - Stackdriver Log Agent - 与 Google Cloud Logging Driver for Docker 无关的日志级别
- json - 使用 Talend 将 MongoDB 集合数据写入 csv
- javascript - 简单的标签式 div
- node.js - 在 node.js 关闭信号上使用循环过程退出自定义函数
- hyperledger-fabric - 错误链码实例化 Hyperledger(带节点)
- mysql - 如何使用 slqalchemy 和 mysql 加速将具有 400 万行的数据帧插入表中
- google-cloud-platform - 如何通过 - Java 以编程方式使用 gcloud 命令
- typescript - 在baseUrl之外导入文件时汇总+ Typescript错误
- ruby-on-rails - Rails 将秘密转换为凭证
- reselect - 'reselect' 选择器的 console.log(..) 显示 f()