这是一个关于如何添加购物车项目Meta&的插件为我的WooCommerce订单订购商品元.最初我的代码适用于输入类型=文本.它返回值的标签和输入的值.
在转换为type =复选框时,代码会返回标签,并为已检查的值返回值“on”.
我想返回已检查值的唯一值名称(忽略未选中的值).
帮助包含更多复选框选项的重构将有助于减少编写的代码.
我的代码:
<?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):
要轻松地将复选框的标签名称作为值,并“重构以帮助包含更多复选框选项将有助于减少编写的代码”我添加了一个简单的函数,您将为每个要设置的复选框设置键/值对展示和处理……
所以我重新访问了你的所有代码:
// 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…
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。