PageSourceSearch

https://surecart-docs.netlify.app/assets/js/8e00edad.d4b63b23.js

js surecart-docs.netlify.app collected 2026-10-03 11:28:28 UTC 5,959 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunk_surecart_docs=self.webpackChunk_surecart_docs||[]).push([[353],{18022:function(e,t,n){n.r(t),n.d(t,{assets:function(){return a},contentTitle:function(){return o},default:function(){return p},frontMatter:function(){return i},metadata:function(){return d},toc:function(){return c}});var r=n(28427),s=(n(2784),n(30876));const i={},o="One Time Purchases",d={unversionedId:"guide/one-time-access",id:"guide/one-time-access",title:"One Time Purchases",description:"Many cases, you'll want to provide someone with access to something when they purchase a product. This can be done through a few steps.",source:"@site/docs/guide/one-time-access.mdx",sourceDirName:"guide",slug:"/guide/one-time-access",permalink:"/guide/one-time-access",draft:!1,editUrl:"https://github.com/facebook/docusaurus/edit/master/website/docs/guide/one-time-access.mdx",tags:[],version:"current",frontMatter:{}},a={},c=[{value:"Store a <code>price_id</code> in your own plugin to reference your purchasable item.",id:"store-a-price_id-in-your-own-plugin-to-reference-your-purchasable-item",level:2},{value:"Provide access using the <code>order_paid</code> event.",id:"provide-access-using-the-order_paid-event",level:2},{value:"Revoke access using the <code>refund_succeeded</code> event.",id:"revoke-access-using-the-refund_succeeded-event",level:2}],u={toc:c};function p(e){let{components:t,...n}=e;return(0,s.kt)("wrapper",(0,r.Z)({},u,n,{components:t,mdxType:"MDXLayout"}),(0,s.kt)("h1",{id:"one-time-purchases"},"One Time Purchases"),(0,s.kt)("p",null,"Many cases, you'll want to provide someone with access to something when they purchase a product. This can be done through a few steps."),(0,s.kt)("h2",{id:"store-a-price_id-in-your-own-plugin-to-reference-your-purchasable-item"},"Store a ",(0,s.kt)("inlineCode",{parentName:"h2"},"price_id")," in your own plugin to reference your purchasable item."),(0,s.kt)("p",null,"This way, when you listen to events, you can sort out if you want to provide or revoke access to something based on the price_id."),(0,s.kt)("h2",{id:"provide-access-using-the-order_paid-event"},"Provide access using the ",(0,s.kt)("inlineCode",{parentName:"h2"},"order_paid")," event."),(0,s.kt)("p",null,"Since this is a one-time purchase, we'll want to use the ",(0,s.kt)("a",{parentName:"p",href:"/events/Actions/order-actions"},"Order Actions")," to listen for order events and update access accordingly.\nThe ",(0,s.kt)("inlineCode",{parentName:"p"},"order_paid")," event is very handy for this, as it's triggered when an order is successfully paid. Here we will listen for the event and update the users meta\nto provide them with access to something specific. We will use the ",(0,s.kt)("a",{parentName:"p",href:"../models/order.mdx"},"Order Model")," to get the price_id and the customer_id."),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-php"},"use \\SureCart\\Models\\Order;\n\n$membership_price_id = '8ba8d60f-5277-4e6b-807c-dee8166446d5';\n\n// Use the order_paid event to update the users meta\nadd_action( 'surecart/order_paid', function( $order_id ) {\n    // fetch order with line items so we can get the purchased price_id.\n    $order = Order::with( ['line_items'] )->find( $order_id );\n\n    // we only want to proceed if the order contains the membership_price_id.\n    if ( ! $order->hasPriceId( $membership_price_id ) ) {\n        return;\n    }\n\n    // get the order's WordPress user\n    $wp_user = $order->getUser();\n\n    // this order has no associated user.\n    if ( $wp_user ) {\n        // update user meta to provide access to something.\n        update_user_meta( $wp_user->ID, 'one_time_membership', true );\n    }\n});\n")),(0,s.kt)("h2",{id:"revoke-access-using-the-refund_succeeded-event"},"Revoke access using the ",(0,s.kt)("inlineCode",{parentName:"h2"},"refund_succeeded")," event."),(0,s.kt)("p",null,"We'll want to revoke access for the user when the order is refunded. We will use the\n",(0,s.kt)("a",{parentName:"p",href:"/events/Actions/refund-actions"},"Refund Action")," ",(0,s.kt)("inlineCode",{parentName:"p"},"refund_succeeded")," and query the the ",(0,s.kt)("a",{parentName:"p",href:"../models/refund.mdx"},"Refund Model")," and find it's\nrelationships to check if the order with the price_id has been fully refunded."),(0,s.kt)("admonition",{title:"Note",type:"info"},(0,s.kt)("p",{parentName:"admonition"},"If you are processing a refund programatically, you will want to make sure you pass any price ids that you are refunding. To do that\nyou can use the ",(0,s.kt)("inlineCode",{parentName:"p"},"forPriceIds")," method on the Charge ",(0,s.kt)("inlineCode",{parentName:"p"}
1,"refund")," method."),(0,s.kt)("pre",{parentName:"admonition"},(0,s.kt)("code",{parentName:"pre",className:"language-php"},"<?php\n\\SureCart\\Models\\Charge::refund()\n    ->forPriceIds( [\n        // access will be revoked for these price ids.\n        '8ba8d60f-5277-4e6b-807c-dee8166446d5',\n        '8ba8d60f-5277-4e6b-dee8-807c166446d6',\n    ] )->create( [\n        // 9900 is equivalent to $99.\n        'amount' => 9900,\n    ] );\n"))),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-php"},"use \\SureCart\\Models\\Order;\n\n$membership_price_id = '8ba8d60f-5277-4e6b-807c-dee8166446d5';\n\n// Use the order_paid event to update the users meta\nadd_action( 'surecart/refund_succeeded', function( $order_id ) {\n    // fetch order with line items.\n    $refund = Refund::find( $order_id );\n\n    // is the refund for this price id?\n    if( ! $refund->hasPriceId( $membership_price_id ) ) {\n        return;\n    }\n\n    // get the order's WordPress user\n    $wp_user = $refund->getUser();\n\n    // this order has no associated user.\n    if ( $wp_user ) {\n        // update user meta to revoke access to something.\n        delete_user_meta( $wp_user->ID, 'one_time_membership');\n    }\n});\n")))}p.isMDXComponent=!0}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.