Wordpress WooCommerce-购物车页面中的可编辑元数据

Wordpress WooCommerce-购物车页面中的可编辑元数据,wordpress,woocommerce,cart,Wordpress,Woocommerce,Cart,我已经包括了一个元数据,它是我添加到购物车中的每个产品的注释。我需要的是,当我在购物车页面时,可以将此注释更改为数量,当我更新购物车时,元数据也会更新 我尝试在单击更新购物车时更新此内容: add_action( 'init', 'update_cart_action', 9); function update_cart_action($args) { global $woocommerce; echo '<script>console.log("pass1

我已经包括了一个元数据,它是我添加到购物车中的每个产品的注释。我需要的是,当我在购物车页面时,可以将此注释更改为数量,当我更新购物车时,元数据也会更新

我尝试在单击更新购物车时更新此内容:

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'] );
}
在购物车中展示

 /**
 * 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;
}
/**
*在购物车中显示自定义项目数据
*/
函数sbm_get_item_data($item_data,$cart_item_data){
if(is_cart()){
返回;
}
if(isset($cart\u item\u data['product\u special\u notes\u field'])){
$item_data[]=数组(
'键'=>\('特殊注释','产品特殊注释'),
'value'=>wc\u clean($cart\u item\u data['product\u special\u notes\u field']))
);
}
返回$item_数据;
}
添加_过滤器('woocommerce_get_item_data','sbm_get_item_data',10,2);
/**
*将自定义元添加到订单
*/
函数sbm\u checkout\u create\u order\u line\u item($item,$cart\u item\u key,$values,$order){
if(isset($values['product\u special\u notes\u field'])){
$item->添加元数据(
__(“特别说明”、“产品特别说明”),
$values[“产品\特殊\注释\字段”],
真的
);
}
}
添加操作('woocommerce\u checkout\u create\u order\u line\u item'、'sbm\u checkout\u create\u order\u line\u item',10,4);
/**
*将自定义购物车项目数据添加到电子邮件
*/
函数sbm_订单_项目_名称($product_名称,$item){
if(isset($item['product\u special\u notes\u field'])){
$product\U name.=sprintf(
“
  • %s:%s
    • ”, __(“特别说明”、“产品特别说明”), esc_html($item['product_special_notes_field'])) ); } 返回$product\u名称; } 添加_过滤器('woocmerce_order_item_name','sbm_order_item_name',10,2); /** *在购物车页面和结帐页面上添加“继续购物”按钮 *添加到theme functions.php文件或代码段插件 */ 添加操作(“购物车表前的woocommerce”、“继续购物”按钮“添加到购物车”); 添加操作('woocommerce\u before\u checkout\u form'、'woo\u add\u continue\u shopping\u button\u to\u cart'); //从会话中获取特殊notes变量并将其添加到项目中 添加\u过滤器(“从\u会话中获取\u购物车\u项目”,“从\u会话中获取购物车\u项目”,99,3); 功能购物车\u项目\u来自\u会话($data、$values、$key){ $data['product_special_notes_field']=isset($values['product_special_notes_field'])?$values['product_special_notes_field']:''; 返回$data; }

谢谢

我们希望您在问题中提供相关代码(包括元数据)以及您自己的实际代码尝试。没有这些,你得到有用答案的机会很低。
/**
 * 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;
}