Add Cloneable Group using update_post_meta() whilst preserving existing data

Support Meta Box AIO Add Cloneable Group using update_post_meta() whilst preserving existing data

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #39296
    Macky McCormackMacky McCormack
    Participant

    Hey,

    When I try to add data to a cloneable group which already has a group the data overwrites the existing group. How do I add a new set of data whilst preserving the existing data?

    Group code

    
    
                    'name'            => __( 'Payments', 'your-text-domain' ),
                    'id'              => $prefix . 'fr_payments',
                    'type'            => 'group',
                    'clone'           => true,
                    'hide_from_front' => true,
                    'fields'          => [
                        [
                            'name'     => __( 'Amount', 'your-text-domain' ),
                            'id'       => $prefix . 'fr_payment_amount',
                            'type'     => 'number',
                            'readonly' => true,
                            'prepend'  => '£',
                        ],
                        [
                            'name'     => __( 'Stripe Payment Intent ID', 'your-text-domain' ),
                            'id'       => $prefix . 'fr_stripe_payment_intent_id',
                            'type'     => 'text',
                            'readonly' => true,
                        ],
                        [
                            'name'     => __( 'Date Paid', 'your-text-domain' ),
                            'id'       => $prefix . 'fr_date_paid',
                            'type'     => 'text',
                            'readonly' => true,
                        ],
                    ],
    

    Update code:

    
        $payment_meta[] =  
        [
          'fr_payment_amount' => $this->stripe_pi_amt,
          'fr_stripe_payment_intent_id' => $this->stripe_pi_id,
          'fr_date_paid' => date('j F Y')
        ];
    
        update_post_meta($this->order_id, 'fr_payments', $payment_meta);
    

    Thanks

    #39306
    PeterPeter
    Moderator

    Hello Macky,

    Please try to use this code

    $payment_meta[0]['fr_payment_amount'] = $this->stripe_pi_amt;

    The cloneable field is the group field so you need to indicate the key of the array where to update the value.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.