Wordpress 更新自定义字段时从购物车中删除项目

Wordpress 更新自定义字段时从购物车中删除项目,wordpress,woocommerce,woocommerce-subscriptions,Wordpress,Woocommerce,Woocommerce Subscriptions,我需要帮助,因为我在购物车页面上遇到了woocommerce问题,在附件中,你可以看到我的购物车中有1个产品,我有3个自定义字段收件人电子邮件、姓名和消息,我们可以更新每个正在工作的字段,当我在购物车中添加2个产品时会出现问题,这两个产品都正确添加到购物车中,当我更改了产品-1的自定义字段并更新购物车时,一切都很好,但当我更改了购物车中第二个产品的自定义字段时,购物车中的第一个产品被删除 [Cart page][1] [1]: https://i.stack.imgur.com/CO

我需要帮助,因为我在购物车页面上遇到了woocommerce问题,在附件中,你可以看到我的购物车中有1个产品,我有3个自定义字段收件人电子邮件、姓名和消息,我们可以更新每个正在工作的字段,当我在购物车中添加2个产品时会出现问题,这两个产品都正确添加到购物车中,当我更改了产品-1的自定义字段并更新购物车时,一切都很好,但当我更改了购物车中第二个产品的自定义字段时,购物车中的第一个产品被删除

[Cart page][1]  


  [1]: https://i.stack.imgur.com/COHlx.png


Code site: On update cart

add_filter( 'woocommerce_update_cart_action_cart_updated', __CLASS__ . '::cart_update', 1, 1 );

Updates the cart items for changes made to recipient infomation on the cart page.
@param bool $cart_updated whether the cart has been updated.

public static function cart_update( $cart_updated ) {
        
        if ( ! empty( $_POST['recipient_name'] ) ) {
            if ( ! empty( $_POST['_wcsgnonce'] ) && wp_verify_nonce( $_POST['_wcsgnonce'], 'wcsg_add_recipient' ) ) { 

                foreach ( WC()->cart->cart_contents as $key => $item ) {
                    
                    if ( isset( $_POST['recipient_name'][ $key ] ) ) {
                        WCS_Gifting::update_cart_item_key( $item, $key, $_POST['recipient_name'][ $key ], $field="recipient_name" ); 
                    }
                }
            } else {
                wc_add_notice( __( 'There was an error with your request. Please try again.', 'woocommerce-subscriptions-gifting' ), 'error' );
            }
        }
        
        
        
        if ( ! empty( $_POST['recipient_email'] ) ) {
            if ( ! empty( $_POST['_wcsgnonce'] ) && wp_verify_nonce( $_POST['_wcsgnonce'], 'wcsg_add_recipient' ) ) {               $recipients = $_POST['recipient_email']; 
                WCS_Gifting::validate_recipient_emails( $recipients );
                foreach ( WC()->cart->cart_contents as $key => $item ) {
                    if ( isset( $_POST['recipient_email'][ $key ] ) ) {
                        
                        WCS_Gifting::update_cart_item_key( $item, $key, $_POST['recipient_email'][ $key ], $field="recipient_email" ); 
                    }
                }
            } else {
                wc_add_notice( __( 'There was an error with your request. Please try again.', 'woocommerce-subscriptions-gifting' ), 'error' );
            }
        }
        
        
        
        if ( ! empty( $_POST['recipient_message'] ) ) {
            if ( ! empty( $_POST['_wcsgnonce'] ) && wp_verify_nonce( $_POST['_wcsgnonce'], 'wcsg_add_recipient' ) ) { 
                
                foreach ( WC()->cart->cart_contents as $key => $item ) {
                    
                    if ( isset( $_POST['recipient_message'][ $key ] ) ) {
                        WCS_Gifting::update_cart_item_key( $item, $key, $_POST['recipient_message'][ $key ], $field="recipient_message" ); 
                    }
                }
            } else {
                wc_add_notice( __( 'There was an error with your request. Please try again.', 'woocommerce-subscriptions-gifting' ), 'error' );
            }
        }

        return $cart_updated;
    }

Attaches recipient information to a subscription cart item key when the recipient information is updated. 
@param object $item The item in the cart to be updated.
@param string $key  Cart item key.
@param array  $new_recipient_data The new recipient information for the item.

public static function update_cart_item_key( $item, $key, $new_recipient_data, $field="" ) {
        
        if($field == "recipient_email"){
            
            if ( empty( $item['wcsg_gift_recipients_email'] ) || $item['wcsg_gift_recipients_email'] != $new_recipient_data ) { 

                $cart_item_data = self::add_cart_item_data( $item, $key, $new_recipient_data, $field );
                $new_key        = WC()->cart->generate_cart_id( $item['product_id'], $item['variation_id'], $item['variation'], $cart_item_data );
                $cart_item      = WC()->cart->get_cart_item( $new_key );                
                
                if ( $new_key != $key ) { 

                    if ( ! empty( $cart_item ) ) {
                        
                        $combined_quantity = $item['quantity'] + $cart_item['quantity'];

                        WC()->cart->cart_contents[ $new_key ]['quantity'] = $combined_quantity;
                        unset( WC()->cart->cart_contents[ $key ] );
                    } else { // there is no item in the cart with the same new key.
    
                        $item_cart_position = array_search( $key, array_keys( WC()->cart->cart_contents ), true );

                        WC()->cart->cart_contents = array_merge(
                            array_slice( WC()->cart->cart_contents, 0, $item_cart_position, true ),
                            array( $new_key => WC()->cart->cart_contents[ $key ] ),
                            array_slice( WC()->cart->cart_contents, $item_cart_position, count( WC()->cart->cart_contents ), true )
                        );

                        if ( empty( $new_recipient_data ) ) {
                            unset( WC()->cart->cart_contents[ $new_key ]['wcsg_gift_recipients_email'] );
                        } else {
                            WC()->cart->cart_contents[ $new_key ]['wcsg_gift_recipients_email'] = $new_recipient_data;
                        }

                        unset( WC()->cart->cart_contents[ $key ] );
                    }
                }
            }
        }
        
        
        if($field == "recipient_name"){
            if ( empty( $item['wcsg_gift_recipients_name'] ) || $item['wcsg_gift_recipients_name'] != $new_recipient_data ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison

                $cart_item_data = self::add_cart_item_data( $item, $key, $new_recipient_data, $field );
                $new_key        = WC()->cart->generate_cart_id( $item['product_id'], $item['variation_id'], $item['variation'], $cart_item_data );
                $cart_item      = WC()->cart->get_cart_item( $new_key );
                
                if ( $new_key != $key ) { // phpcs:ignore WordPress.PHP.StrictComparisons.

                    if ( ! empty( $cart_item ) ) {
                        $combined_quantity = $item['quantity'] + $cart_item['quantity'];

                        WC()->cart->cart_contents[ $new_key ]['quantity'] = $combined_quantity;
                        unset( WC()->cart->cart_contents[ $key ] );
                    } else { // there is no item in the cart with the same new key.
                    
                        $item_cart_position = array_search( $key, array_keys( WC()->cart->cart_contents ), true );

                        WC()->cart->cart_contents = array_merge(
                            array_slice( WC()->cart->cart_contents, 1, $item_cart_position, true ),
                            array( $new_key => WC()->cart->cart_contents[ $key ] ),
                            array_slice( WC()->cart->cart_contents, $item_cart_position, count( WC()->cart->cart_contents ), true )
                        );

                        if ( empty( $new_recipient_data ) ) {
                            unset( WC()->cart->cart_contents[ $new_key ]['wcsg_gift_recipients_name'] );
                        } else {
                            WC()->cart->cart_contents[ $new_key ]['wcsg_gift_recipients_name'] = $new_recipient_data;
                        }

                        unset( WC()->cart->cart_contents[ $key ] );
                    }
                }
            }
        }
        
        
        if($field == "recipient_message"){
            
            if ( empty( $item['wcsg_gift_recipients_message'] ) || $item['wcsg_gift_recipients_message'] != $new_recipient_data ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison

                $cart_item_data = self::add_cart_item_data( $item, $key, $new_recipient_data, $field );
                $new_key        = WC()->cart->generate_cart_id( $item['product_id'], $item['variation_id'], $item['variation'], $cart_item_data );
                $cart_item      = WC()->cart->get_cart_item( $new_key );

                
                if ( $new_key != $key ) {

                    if ( ! empty( $cart_item ) ) {
                        $combined_quantity = $item['quantity'] + $cart_item['quantity'];

                        WC()->cart->cart_contents[ $new_key ]['quantity'] = $combined_quantity;
                        unset( WC()->cart->cart_contents[ $key ] );
                    } else { // there is no item in the cart with the same new key.
                    
                    
                        $item_cart_position = array_search( $key, array_keys( WC()->cart->cart_contents ), true );

                        WC()->cart->cart_contents = array_merge(
                            array_slice( WC()->cart->cart_contents, 1, $item_cart_position, true ),
                            array( $new_key => WC()->cart->cart_contents[ $key ] ),
                            array_slice( WC()->cart->cart_contents, $item_cart_position, count( WC()->cart->cart_contents ), true )
                        );

                        if ( empty( $new_recipient_data ) ) {
                            unset( WC()->cart->cart_contents[ $new_key ]['wcsg_gift_recipients_message'] );
                        } else {
                            WC()->cart->cart_contents[ $new_key ]['wcsg_gift_recipients_message'] = $new_recipient_data;
                        }

                        unset( WC()->cart->cart_contents[ $key ] );
                    }
                }
            }
        }   
    }


Populates the cart item data that will be used by WooCommerce to generate a unique ID for the cart item. That is to
avoid merging different products when they aren't the same. Previously the resubscribe status was ignored.
@param array  $item               A cart item with all its data.
@param string $key                A cart item key.
@param array  $new_recipient_data Email address of the new recipient.
@return array New cart item data.

private static function add_cart_item_data( $item, $key, $new_recipient_data, $field ) {
        // start with a clean slate.
        $cart_item_data = array();
        
        // Add the recipient email.
        if($field == 'recipient_email'){
            if ( ! empty( $new_recipient_data ) ) {
                
                
                $cart_item_data = array( 'wcsg_gift_recipients_email' => $new_recipient_data );
            }   
        }
        
        // Add the recipient name.
        if($field == 'recipient_name'){
            if ( ! empty( $new_recipient_data ) ) {
                $cart_item_data = array( 'wcsg_gift_recipients_name' => $new_recipient_data );
            }   
        }
        
        // Add the recipient message.
        if($field == 'recipient_message'){
            if ( ! empty( $new_recipient_data ) ) {
                $cart_item_data = array( 'wcsg_gift_recipients_message' => $new_recipient_data );
            }   
        }

        // Add resubscribe data.
        if ( array_key_exists( 'subscription_resubscribe', $item ) ) {
            $cart_item_data = array_merge( $cart_item_data, array( 'subscription_resubscribe' => $item['subscription_resubscribe'] ) );
        }

        $cart_item_data = apply_filters( 'wcsg_cart_item_data', $cart_item_data, $item, $key, $new_recipient_data );

        return $cart_item_data;`enter code here`
    }