0

When a user signs up to the site, they make a Stripe payment, on success the package they purchased is stored in the database as stripe_package_id. This ID correlates with a relationship field in the ACF's invoice_product.

invoice_product is a relationship field shown on the single user Dashboard page, the relationship is to pull the products in Stripe into the Dashboard -> User -> Single -> invoice_product so an admin can manually select or change the package / product associated with the user.

During the payment, a function runs asp_after_txn_callback() which is used to update user meta update_user_meta(get_current_user_id(), 'stripe_package_id', $post_data['product_id']);.

In this function I need to also set the relationship field invoice_product to the value product pulled in with the same ID as stripe_package_id.

This value will not exist for this user before they have successfully enrolled so the appropriate steps will be required to create the field for this particular user (if this is even a valid step to update user meta of a relationship field).

The stripe_package_id does not need to change when a new package is manually selected from the relationship field invoice_product within Dashboard -> User -> Single -> invoice_product. Only when the product is processed through the stripe function.

If the user was to purchase a different product, we would need to run through the updating of the Dashboard -> User -> Single -> invoice_product, this time the field will already exist and will just need updating.

This is my current attempt:

function asp_after_txn_callback ($post_data, $charge) {
    if(!empty($post_data['product_id'])) {
        update_user_meta(get_current_user_id(), 'stripe_package_id', $post_data['product_id']);
        
        $user_id = get_current_user_id();
        $invoice_product = get_field('invoice_product', $user_id);
        $invoice_product_id = $post_data['product_id'];
                        
        if (!is_array($invoice_product)):
            $invoice_product = array();
        endif;

        array_push($invoice_product, $invoice_product_id);
        update_field('invoice_product', $invoice_product, $user_id);
        update_field('_invoice_product', 'field_6040cfd16a6c1', $user_id);
    }  
}

I appreciate this might be no where near right, but I feel I have attempted so many solutions I am actually now getting further away from the correct solution, please help me allocate a product to be shown in the relationship ACF field in the backend of a user's profile.

I have also attached an image of the database entries for existing invoice_product related fields.

Database entries for the invoice_product & _invoice_product fields

Jason Is My Name
  • 929
  • 2
  • 14
  • 38

0 Answers0