Jump to content

Community

  • We are moving our Documentation to our new Guides area.

    As articles are moved they will be deleted. Please start following the new guides. We look forward to publishing all our new guides soon!

  • Sign in to follow this  

    Commerce: Custom Actions (*)


    Mark

    IP.Nexus for IP.Board allowed each Package to specify a "Custom module" - a PHP script which could specify custom code to run when purchases of that package were made, expired, cancelled, etc.

    In IPS Community Suite 4, it is possible to overload any class in the suite, so this is no longer a specific option for packages. But it is easy to recreate.

     

    Step 1: Create a plugin

    You will need to create a plugin, which requires you to have developer mode enabled. We strongly advise against installing developer mode on a live installation so do this, and your development on a test installation.

    Once developer mode is enabled, a "Create Plugin" button will appear in the Admin CP under System --> Site Features --> Plugin. Use this tool to create your plugin, after which you'll be taken into the Plugin Developer Center.

    Once in the Plugin Developer Center you will create a Code Hook on the \IPS\nexus\Package class (for more information about Code Hooks and other Plugin features see http://community.invisionpower.com/4docs/advanced-usage/development/plugins-r71/).

    Step 2: Write your code

    The content of the plugin will be very similar to the custom module from before, however there are some key differences:

    • The parameters for the methods are different. Rather than arrays, you will be passed objects for purchase and other data. Since your code is running on an instance of a package, you will not be passed $package and should use $this instead. A full list of the available methods and their signatures are below.
    • Because your code will run for every package, you must first check the ID number ($this->id) to see if it is a package you want to run the code for.
    • You are overloading a method which may be doing things itself, so you need to call the parent:: method within your code.
    • If you were using the IP.Board 3 API, you will of course need to update your calls to use the IPS Community Suite 4 API.

    For example, if this was your custom module on IP.Nexus for IP.Board 3:

    <?php
    
    class custom_actions_FOO
    {
        /**
         * Purchase record generated (run after onPaid)
         *
         * @param	array	The member purchasing
         * @param	array   Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param	invoice	Invoice Model
         * @param	array	Row from nexus_purchases [since Nexus 1.5]
         * @return	void
         */
        public function onPurchaseGenerated( $member, $package, $invoice, $purchase )
        {    
    		ipsRegistry::DB()->insert( 'purchase_log', array(
    			'member'	=> $member['member_id'],
    			'package'	=> $package['p_id'],
    			'purchase'	=> $purchase['ps_id'],
    			'time'		=> time()
    		) );
        }
    
    }

    Then your code hook for IPS4 will look something like:

    <?php
    
    class hook
    {
    	/**
    	 * On Purchase Generated
    	 *
    	 * @param	\IPS\nexus\Purchase	$purchase	The purchase
    	 * @param	\IPS\nexus\Invoice	$invoice	The invoice
    	 * @return	void
    	 */
    	public function onPurchaseGenerated( \IPS\nexus\Purchase $purchase, \IPS\nexus\Invoice $invoice )
        {
    	    if ( in_array( $this->id, array( 1, 2, 3 ) ) )
    	    {
    			\IPS\Db::i()->insert( 'purchase_log', array(
    				'member'	=> $purchase->member->member_id,
    				'package'	=> $this->id,
    				'purchase'	=> $purchase->id,
    				'time'		=> time()
    			) );
    		}
    		
    		return parent::onPurchaseGenerated( $purchase, $invoice );
        }
    
    }

     

    Step 3: Download the plugin and install

    Once you're ready, download your plugin from the Plugin Developer Center which will generate an xml file. Go to your live installation and install this plugin there.

     

     

     

    Appendix: Available Methods

    You are overloading \IPS\nexus\Package, so you can specify any methods which are part of that. The most common use-cases however are:

    	/**
    	 * On Purchase Generated
    	 *
    	 * @param	\IPS\nexus\Purchase	$purchase	The purchase
    	 * @param	\IPS\nexus\Invoice	$invoice	The invoice
    	 * @return	void
    	 */
    	public function onPurchaseGenerated( \IPS\nexus\Purchase $purchase, \IPS\nexus\Invoice $invoice )
    	{		
    
    	}
    
        /**
         * On Renew (Renewal invoice paid. Is not called if expiry data is manually changed)
         *
         * @param    \IPS\nexus\Purchase    $purchase    The purchase
         * @param    int                    $cycles        Cycles
         * @return    void
         */
        public function onRenew( \IPS\nexus\Purchase $purchase, $cycles )
        {
            // This method is only available since 4.0.8
        }
    	
    	/**
    	 * On Expiration Date Change
    	 *
    	 * @param	\IPS\nexus\Purchase	$purchase	The purchase
    	 * @return	void
    	 */
    	public function onExpirationDateChange( \IPS\nexus\Purchase $purchase )
    	{
    	
    	}
    	
    	/**
    	 * On Purchase Expired
    	 *
    	 * @param	\IPS\nexus\Purchase	$purchase	The purchase
    	 * @return	void
    	 */
    	public function onExpire( \IPS\nexus\Purchase $purchase )
    	{
    
    	}
    	
    	/**
    	 * On Purchase Canceled
    	 *
    	 * @param	\IPS\nexus\Purchase	$purchase	The purchase
    	 * @return	void
    	 */
    	public function onCancel( \IPS\nexus\Purchase $purchase )
    	{
    
    	}
    	
    	/**
    	 * On Purchase Deleted
    	 *
    	 * @param	\IPS\nexus\Purchase	$purchase	The purchase
    	 * @return	void
    	 */
    	public function onDelete( \IPS\nexus\Purchase $purchase )
    	{
    
    	}
    	
    	/**
    	 * On Purchase Reactivated (renewed after being expired or reactivated after being canceled)
    	 *
    	 * @param	\IPS\nexus\Purchase	$purchase	The purchase
    	 * @return	void
    	 */
    	public function onReactivate( \IPS\nexus\Purchase $purchase )
    	{
    
    	}
    	
    	/**
    	 * On Transfer (is ran before transferring)
    	 *
    	 * @param	\IPS\nexus\Purchase	$purchase		The purchase
    	 * @param	\IPS\Member			$newCustomer	New Customer
    	 * @return	void
    	 */
    	public function onTransfer( \IPS\nexus\Purchase $purchase, \IPS\Member $newCustomer )
    	{
    
    	}
    	
    	/**
    	 * On Upgrade/Downgrade
    	 *
    	 * @param	\IPS\nexus\Purchase							$purchase				The purchase
    	 * @param	\IPS\nexus\Package							$newPackage				The package to upgrade to
    	 * @param	int|NULL|\IPS\nexus\Purchase\RenewalTerm	$chosenRenewalOption	The chosen renewal option
    	 * @return	void
    	 */
    	public function onChange( \IPS\nexus\Purchase $purchase, \IPS\nexus\Package $newPackage, $chosenRenewalOption = NULL )
    	{
    	
    	}
    

     

    Sign in to follow this  


    User Feedback

    Recommended Comments

    On 12/6/2015 at 0:09 PM, Edward Shephard said:

    So how do you apply this to a particular package in Commerce? 

    in an array, target your specific package by package number.

    Share this comment


    Link to comment
    Share on other sites

×
×
  • Create New...