在 WooCommerce 中将自定义字段添加为购物车项元数据和订单项元数据
Add custom fields as cart item meta and order item meta in WooCommerce
这是一个关于如何为我的 WooCommerce 订单添加购物车项目元数据和订单项目元数据的插件。最初我的下面的代码适用于输入类型=文本。它 return 是值的标签和输入的值。
转换为 type=checkbox
时,选中的代码 returns label
和 value="on"
。
我想return唯一检查值的值名称(忽略未检查的值)。
帮助包含更多复选框选项的重构将有助于减少编写的代码。
我的代码:
<?php
global $woocommerce, $product, $post;
add_action( 'woocommerce_before_add_to_cart_button', 'add_fields_before_add_to_cart' );
function add_fields_before_add_to_cart( ) {
?>
<div class="simple-selects">
<div class="col-md-6">
<h3>Main meals</h3>
<p><input type="checkbox" name="mm_chicken_cutlet_bento" id="mm_chicken_cutlet_bento"><?php _e( "Chicken Cutlet Bento", "aoim"); ?></p>
<p><input type="checkbox" name="mm_roasted_pork_rib_bento" id="mm_roasted_pork_rib_bento"><?php _e( "Roasted Pork Rib Bento", "aoim"); ?></p>
</div>
</div>
<?php
}
/**
* Add data to cart item
*/
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 25, 2 );
function add_cart_item_data( $cart_item_meta, $product_id ) {
if ( isset( $_POST ['mm_chicken_cutlet_bento'] ) && isset( $_POST ['mm_roasted_pork_rib_bento'] ) ) {
$custom_data = array() ;
$custom_data [ 'mm_chicken_cutlet_bento' ] = isset( $_POST ['mm_chicken_cutlet_bento'] ) ? sanitize_text_field ( $_POST ['mm_chicken_cutlet_bento'] ) : "" ;
$custom_data [ 'mm_roasted_pork_rib_bento' ] = isset( $_POST ['mm_roasted_pork_rib_bento'] ) ? sanitize_text_field ( $_POST ['mm_roasted_pork_rib_bento'] ): "" ;
$cart_item_meta ['custom_data'] = $custom_data ;
}
return $cart_item_meta;
}
/**
* Display custom data on cart and checkout page.
*/
add_filter( 'woocommerce_get_item_data', 'get_item_data' , 25, 2 );
function get_item_data ( $other_data, $cart_item ) {
if ( isset( $cart_item [ 'custom_data' ] ) ) {
$custom_data = $cart_item [ 'custom_data' ];
$other_data[] = array( 'name' => 'Chicken Cutlet Bento', 'display' => $custom_data['mm_chicken_cutlet_bento'] );
$other_data[] = array( 'name' => 'Roasted Pork Rib Bento', 'display' => $custom_data['mm_roasted_pork_rib_bento'] );
}
return $other_data;
}
/**
* Add order item meta.
*/
add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta' , 10, 2);
function add_order_item_meta ( $item_id, $values ) {
if ( isset( $values [ 'custom_data' ] ) ) {
$custom_data = $values [ 'custom_data' ];
wc_add_order_item_meta( $item_id, 'Chicken Cutlet Bento', $custom_data['mm_chicken_cutlet_bento'] );
wc_add_order_item_meta( $item_id, 'Roasted Pork Rib Bento', $custom_data['mm_roasted_pork_rib_bento'] );
}
}
?>
Update (related to comments):
- Limit the functionality to only one product ID
- Add all checkboxes values as a coma separated string
为了轻松获取复选框的标签名称作为值并“重构以帮助包含更多复选框选项将有助于减少编写的代码”,我添加了一个简单的函数,您将在其中设置 key/value对要显示和处理的每个复选框…
所以我重新访问了你的所有代码:
// HERE set the array of pairs keys/values for your checkboxes
function custom_checkboxes(){
return array(
'mm_chicken_cutlet_bento' => __( "Chicken Cutlet Bento", "aoim"),
'mm_roasted_pork_rib_bento' => __( "Roasted Pork Rib Bento", "aoim"),
);
}
// Displaying the checkboxes
add_action( 'woocommerce_before_add_to_cart_button', 'add_fields_before_add_to_cart' );
function add_fields_before_add_to_cart( ) {
global $product;
if( $product->get_id() != 2 ) return; // Only for product ID "2"
?>
<div class="simple-selects">
<div class="col-md-6">
<h3><?php _e("Main meals", "aoim"); ?></h3>
<?php foreach( custom_checkboxes() as $key => $value ): ?>
<p><input type="checkbox" name="<?php echo $key; ?>" id="<?php echo $key; ?>"><?php echo ' ' . $value; ?></p>
<?php endforeach; ?>
</div>
</div>
<?php
}
// Add data to cart item
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 25, 2 );
function add_cart_item_data( $cart_item_data, $product_id ) {
if( $product_id != 2 ) return $cart_item_data; // Only for product ID "2"
// Set the data for the cart item in cart object
$data = array() ;
foreach( custom_checkboxes() as $key => $value ){
if( isset( $_POST[$key] ) )
$cart_item_data['custom_data'][$key] = $data[$key] = $value;
}
// Add the data to session and generate a unique ID
if( count($data > 0 ) ){
$cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );
WC()->session->set( 'custom_data', $data );
}
return $cart_item_data;
}
// Display custom data on cart and checkout page.
add_filter( 'woocommerce_get_item_data', 'get_item_data' , 25, 2 );
function get_item_data ( $cart_data, $cart_item ) {
if( $cart_item['product_id'] != 2 ) return $cart_data; // Only for product ID "2"
if( ! empty( $cart_item['custom_data'] ) ){
$values = array();
foreach( $cart_item['custom_data'] as $key => $value )
if( $key != 'unique_key' ){
$values[] = $value;
}
$values = implode( ', ', $values );
$cart_data[] = array(
'name' => __( "Option", "aoim"),
'display' => $values
);
}
return $cart_data;
}
// Add order item meta.
add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta' , 10, 3 );
function add_order_item_meta ( $item_id, $cart_item, $cart_item_key ) {
if ( isset( $cart_item[ 'custom_data' ] ) ) {
$values = array();
foreach( $cart_item[ 'custom_data' ] as $key => $value )
if( $key != 'unique_key' ){
$values[] = $value;
}
$values = implode( ', ', $values );
wc_add_order_item_meta( $item_id, __( "Option", "aoim"), $values );
}
}
此代码位于您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。
已测试并有效。
你会得到这样的东西:
I have added "Option", as label to avoid the value repetition…
大多数情况下仍然有效,但现在已弃用将元数据从购物车添加到订单的操作。
您可以像这样使用 woocommerce_checkout_create_order_line_item
而不是 woocommerce_add_order_item_meta
(已弃用):
add_action(
'woocommerce_checkout_create_order_line_item',
function(WC_Order_Item_Product $cartItem, string $cartItemKey, array $values): void {
if (!empty($values['custom_data']) && is_array($values['custom_data'])) {
$values = [];
foreach ($values['custom_data'] as $key => $value) {
if ($key !== 'unique_key'){
$values[] = $value;
}
}
$cartItem->add_meta_data(__('Option', 'aoim'), implode(', ', $values), true);
}
},
10,
3
);
这是一个关于如何为我的 WooCommerce 订单添加购物车项目元数据和订单项目元数据的插件。最初我的下面的代码适用于输入类型=文本。它 return 是值的标签和输入的值。
转换为 type=checkbox
时,选中的代码 returns label
和 value="on"
。
我想return唯一检查值的值名称(忽略未检查的值)。
帮助包含更多复选框选项的重构将有助于减少编写的代码。
我的代码:
<?php
global $woocommerce, $product, $post;
add_action( 'woocommerce_before_add_to_cart_button', 'add_fields_before_add_to_cart' );
function add_fields_before_add_to_cart( ) {
?>
<div class="simple-selects">
<div class="col-md-6">
<h3>Main meals</h3>
<p><input type="checkbox" name="mm_chicken_cutlet_bento" id="mm_chicken_cutlet_bento"><?php _e( "Chicken Cutlet Bento", "aoim"); ?></p>
<p><input type="checkbox" name="mm_roasted_pork_rib_bento" id="mm_roasted_pork_rib_bento"><?php _e( "Roasted Pork Rib Bento", "aoim"); ?></p>
</div>
</div>
<?php
}
/**
* Add data to cart item
*/
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 25, 2 );
function add_cart_item_data( $cart_item_meta, $product_id ) {
if ( isset( $_POST ['mm_chicken_cutlet_bento'] ) && isset( $_POST ['mm_roasted_pork_rib_bento'] ) ) {
$custom_data = array() ;
$custom_data [ 'mm_chicken_cutlet_bento' ] = isset( $_POST ['mm_chicken_cutlet_bento'] ) ? sanitize_text_field ( $_POST ['mm_chicken_cutlet_bento'] ) : "" ;
$custom_data [ 'mm_roasted_pork_rib_bento' ] = isset( $_POST ['mm_roasted_pork_rib_bento'] ) ? sanitize_text_field ( $_POST ['mm_roasted_pork_rib_bento'] ): "" ;
$cart_item_meta ['custom_data'] = $custom_data ;
}
return $cart_item_meta;
}
/**
* Display custom data on cart and checkout page.
*/
add_filter( 'woocommerce_get_item_data', 'get_item_data' , 25, 2 );
function get_item_data ( $other_data, $cart_item ) {
if ( isset( $cart_item [ 'custom_data' ] ) ) {
$custom_data = $cart_item [ 'custom_data' ];
$other_data[] = array( 'name' => 'Chicken Cutlet Bento', 'display' => $custom_data['mm_chicken_cutlet_bento'] );
$other_data[] = array( 'name' => 'Roasted Pork Rib Bento', 'display' => $custom_data['mm_roasted_pork_rib_bento'] );
}
return $other_data;
}
/**
* Add order item meta.
*/
add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta' , 10, 2);
function add_order_item_meta ( $item_id, $values ) {
if ( isset( $values [ 'custom_data' ] ) ) {
$custom_data = $values [ 'custom_data' ];
wc_add_order_item_meta( $item_id, 'Chicken Cutlet Bento', $custom_data['mm_chicken_cutlet_bento'] );
wc_add_order_item_meta( $item_id, 'Roasted Pork Rib Bento', $custom_data['mm_roasted_pork_rib_bento'] );
}
}
?>
Update (related to comments):
- Limit the functionality to only one product ID
- Add all checkboxes values as a coma separated string
为了轻松获取复选框的标签名称作为值并“重构以帮助包含更多复选框选项将有助于减少编写的代码”,我添加了一个简单的函数,您将在其中设置 key/value对要显示和处理的每个复选框…
所以我重新访问了你的所有代码:
// HERE set the array of pairs keys/values for your checkboxes
function custom_checkboxes(){
return array(
'mm_chicken_cutlet_bento' => __( "Chicken Cutlet Bento", "aoim"),
'mm_roasted_pork_rib_bento' => __( "Roasted Pork Rib Bento", "aoim"),
);
}
// Displaying the checkboxes
add_action( 'woocommerce_before_add_to_cart_button', 'add_fields_before_add_to_cart' );
function add_fields_before_add_to_cart( ) {
global $product;
if( $product->get_id() != 2 ) return; // Only for product ID "2"
?>
<div class="simple-selects">
<div class="col-md-6">
<h3><?php _e("Main meals", "aoim"); ?></h3>
<?php foreach( custom_checkboxes() as $key => $value ): ?>
<p><input type="checkbox" name="<?php echo $key; ?>" id="<?php echo $key; ?>"><?php echo ' ' . $value; ?></p>
<?php endforeach; ?>
</div>
</div>
<?php
}
// Add data to cart item
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 25, 2 );
function add_cart_item_data( $cart_item_data, $product_id ) {
if( $product_id != 2 ) return $cart_item_data; // Only for product ID "2"
// Set the data for the cart item in cart object
$data = array() ;
foreach( custom_checkboxes() as $key => $value ){
if( isset( $_POST[$key] ) )
$cart_item_data['custom_data'][$key] = $data[$key] = $value;
}
// Add the data to session and generate a unique ID
if( count($data > 0 ) ){
$cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );
WC()->session->set( 'custom_data', $data );
}
return $cart_item_data;
}
// Display custom data on cart and checkout page.
add_filter( 'woocommerce_get_item_data', 'get_item_data' , 25, 2 );
function get_item_data ( $cart_data, $cart_item ) {
if( $cart_item['product_id'] != 2 ) return $cart_data; // Only for product ID "2"
if( ! empty( $cart_item['custom_data'] ) ){
$values = array();
foreach( $cart_item['custom_data'] as $key => $value )
if( $key != 'unique_key' ){
$values[] = $value;
}
$values = implode( ', ', $values );
$cart_data[] = array(
'name' => __( "Option", "aoim"),
'display' => $values
);
}
return $cart_data;
}
// Add order item meta.
add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta' , 10, 3 );
function add_order_item_meta ( $item_id, $cart_item, $cart_item_key ) {
if ( isset( $cart_item[ 'custom_data' ] ) ) {
$values = array();
foreach( $cart_item[ 'custom_data' ] as $key => $value )
if( $key != 'unique_key' ){
$values[] = $value;
}
$values = implode( ', ', $values );
wc_add_order_item_meta( $item_id, __( "Option", "aoim"), $values );
}
}
此代码位于您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。
已测试并有效。
你会得到这样的东西:
I have added "Option", as label to avoid the value repetition…
您可以像这样使用 woocommerce_checkout_create_order_line_item
而不是 woocommerce_add_order_item_meta
(已弃用):
add_action(
'woocommerce_checkout_create_order_line_item',
function(WC_Order_Item_Product $cartItem, string $cartItemKey, array $values): void {
if (!empty($values['custom_data']) && is_array($values['custom_data'])) {
$values = [];
foreach ($values['custom_data'] as $key => $value) {
if ($key !== 'unique_key'){
$values[] = $value;
}
}
$cartItem->add_meta_data(__('Option', 'aoim'), implode(', ', $values), true);
}
},
10,
3
);