我已经包含了meta_data,这是我添加到购物车中的每个产品的注释。我需要的是当我在购物车页面中时,可以随数量更改此注释,并且当我更新购物车时meta_data也将更新。
当我点击更新购物车时,我尝试使用此更新:
add_action( 'init', 'update_cart_action', 9);
function update_cart_action($args) {
global $woocommerce;
echo '<script>console.log("pass1");</script>';
if ( ( ! empty( $_POST['update_cart'] )`enter code here` || ! empty( $_POST['proceed'] ) ) && wp_verify_nonce('cart')) {
$cart_totals = isset( $_POST['cart'] ) ? wp_unslash( $_POST['cart'] ) : '';
if ( ! WC()->cart->is_empty() && is_array( $cart_totals ) ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
if ( isset( $cart_totals[ $cart_item_key ]['product_special_notes_field'] ) ) {
echo '<script>console.log("pass2");</script>';
WC()->cart->cart_contents[ $cart_item_key ]['product_special_notes_field'] = $cart_totals[ $cart_item_key ]['product_special_notes_field'];
}
}
}
}
}
// this adds Url as meta in Order for item
add_action ('woocommerce_add_order_item_meta', 'add_item_meta', 10, 2);
function add_item_meta( $item_id, $values ) {
woocommerce_add_order_item_meta( $item_id, 'Special Notes', $values['product_special_notes_field'] );
}
要添加到产品页面
/**
* Add a custom text input field to the product page
*/
function sbm_add_text_field_cart() {
?>
<?php
if(get_field("option_per_item", get_the_ID())){
?>
<div class="div-button-kg-item">
<button class="button-kg-item" type="button" onclick="changePerItem();">PER ITEM</button>
</div>
<?php
}
?>
<div class="product-special-notes-field-wrap">
<label id="product_special_notes_field_label" for="product_special_notes_field"><?php _e( 'Special Notes', 'product-special-notes' ); ?></label>
<textarea name='product_special_notes_field' id='product_special_notes_field' rows="4"
placeholder="PERSONALISE the order e.g. “3 pieces instead of Kgs”"></textarea>
</div>
<?php }
add_action( 'woocommerce_before_add_to_cart_button', 'sbm_add_text_field_cart' );
添加到购物车
/**
* Add custom cart item data
*/
function sbm_add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
if( isset( $_POST['product_special_notes_field'] ) ) {
if(empty($_POST['product_special_notes_field']) || $_POST['product_special_notes_field'] == ""){
return;
}
$cart_item_data['product_special_notes_field'] = sanitize_text_field( $_POST['product_special_notes_field'] );
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'sbm_add_cart_item_data', 10, 3 );
在购物车中显示
/**
* Display custom item data in the cart
*/
function sbm_get_item_data( $item_data, $cart_item_data ) {
if(is_cart()){
return;
}
if( isset( $cart_item_data['product_special_notes_field'] ) ) {
$item_data[] = array(
'key' => __( 'Special Notes', 'product-special-notes' ),
'value' => wc_clean( $cart_item_data['product_special_notes_field'] )
);
}
return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'sbm_get_item_data', 10, 2 );
/**
* Add custom meta to order
*/
function sbm_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
if( isset( $values['product_special_notes_field'] ) ) {
$item->add_meta_data(
__( 'Special Notes', 'product-special-notes' ),
$values['product_special_notes_field'],
true
);
}
}
add_action( 'woocommerce_checkout_create_order_line_item', 'sbm_checkout_create_order_line_item', 10, 4 );
/**
* Add custom cart item data to emails
*/
function sbm_order_item_name( $product_name, $item ) {
if( isset( $item['product_special_notes_field'] ) ) {
$product_name .= sprintf(
'<ul><li>%s: %s</li></ul>',
__( 'Special Notes', 'product-special-notes' ),
esc_html( $item['product_special_notes_field'] )
);
}
return $product_name;
}
add_filter( 'woocommerce_order_item_name', 'sbm_order_item_name', 10, 2 );
/**
* Add Continue Shopping Button on Cart Page & Checkout page
* Add to theme functions.php file or Code Snippets plugin
*/
add_action( 'woocommerce_before_cart_table', 'woo_add_continue_shopping_button_to_cart' );
add_action( 'woocommerce_before_checkout_form', 'woo_add_continue_shopping_button_to_cart' );
// get from session your special notes variable and add it to item
add_filter('woocommerce_get_cart_item_from_session', 'cart_item_from_session', 99, 3);
function cart_item_from_session( $data, $values, $key ) {
$data['product_special_notes_field'] = isset( $values['product_special_notes_field'] ) ? $values['product_special_notes_field'] : '';
return $data;
}
谢谢