• Resolved smams

    (@smams)


    I have a digital product which has a monthly subscription price of £20.

    What I would like to do is increment the value of a custom field (ACF) on the users profile by 1 every time this product is purchased or renewed.

    Are there any hooks or other methods that I can use to execute code once the product is purchased or renewed?

    Many thanks,

    Sam

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Andre Gagnon

    (@2winfactor)

    Yup! You will want to look at the surecart/purchase_created action:

    https://developer.surecart.com/docs/purchase-actions-reference

    add_action(
    'surecart/purchase_created',
    function ( $purchase ) {
    $wp_user = $purchase->getUser();
    $existing = get_user_meta( $wp_user->ID, 'custom_increment', true );
    update_user_meta( $wp_user->ID, 'custom_increment', $existing + 1 );
    }
    );

    This will increment when it is initially purchased. https://developer.surecart.com/docs/purchase-actions-reference

    To do a renewal, you will want to use surecart/subscription_renewed

    https://developer.surecart.com/docs/subscription-actions-reference

    Any other questions, let me know.

    Thread Starter smams

    (@smams)

    Thank you for the example and links to the documentation. This is now working however, the field value seems to increase by 2 instead of 1?

    add_action( 'surecart/purchase_created', 'purchaseCreated' );
    function purchaseCreated( $purchase, $webhook_data = [] ) {
    	$wp_user = $purchase->getUser();
    	$boosts = (int) get_field( 'field_65baaaee56f72', 'user_' . $wp_user->ID );
    	$boosts++;
    	update_field('field_65baaaee56f72', $boosts, 'user_' . $wp_user->ID);
    	error_log('Purchase Created #' . $boosts);
    };

    When I purchase a test order, the value in the field increases by one (as expected) but if I leave it for about 10 seconds, the value in the field increases again by 1.

    Is there a way to resolve this issue?

    Many thanks.

    Plugin Author Andre Gagnon

    (@2winfactor)

    It may be that the purchase_created hook is firing twice. Sometimes this can happen so that we can run integrations on sites locally, or otherwise unaccessible to webhooks. Can you add this condition at the front? This will ensure it only runs for the webhook context:

    add_action( 'surecart/purchase_created', 'purchaseCreated', 10, 2 );
    function purchaseCreated( $purchase, $webhook_data = [] ) {
    	if ( empty( $webhook_data['id'] ) ) {
    		return;
    	}
    	$wp_user = $purchase->getUser();
    	$boosts  = (int) get_field( 'field_65baaaee56f72', 'user_' . $wp_user->ID );
    	$boosts++;
    	update_field( 'field_65baaaee56f72', $boosts, 'user_' . $wp_user->ID );
    	error_log( 'Purchase Created #' . $boosts );
    };
    Thread Starter smams

    (@smams)

    I have added this condition however, the $webhook_data array always seems to be empty and causes the rest of the code to fail to run?

    Plugin Support Ivan C.

    (@ivanbsf)

    Hey smams!

    Can you kindly open a support ticket? Our dedicated team will promptly investigate the issue and provide a timely response!

    Best,

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Is it possible to assign a value to a custom field once a product is purchased?’ is closed to new replies.