Commerce: Custom Actions (*) > Invisionize.eu - IP.Board (IPB) News
Kanał Rss Kanał Rss
Kanał Atom Kanał Atom

Commerce: Custom Actions (*)

wersja drukowalna wersja Microsoft Word wersja HTML

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 IPSnexusPackage 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:

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	IPSnexusPurchase	$purchase	The purchase
	 * @param	IPSnexusInvoice	$invoice	The invoice
	 * @return	void
	 */
	public function onPurchaseGenerated( IPSnexusPurchase $purchase, IPSnexusInvoice $invoice )
    {
	    if ( in_array( $this->id, array( 1, 2, 3 ) ) )
	    {
			IPSDb::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 IPSnexusPackage, so you can specify any methods which are part of that. The most common use-cases however are:

	/**
	 * On Purchase Generated
	 *
	 * @param	IPSnexusPurchase	$purchase	The purchase
	 * @param	IPSnexusInvoice	$invoice	The invoice
	 * @return	void
	 */
	public function onPurchaseGenerated( IPSnexusPurchase $purchase, IPSnexusInvoice $invoice )
	{		

	}

    /**
     * On Renew (Renewal invoice paid. Is not called if expiry data is manually changed)
     *
     * @param    IPSnexusPurchase    $purchase    The purchase
     * @param    int                    $cycles        Cycles
     * @return    void
     */
    public function onRenew( IPSnexusPurchase $purchase, $cycles )
    {
        // This method is only available since 4.0.8
    }
	
	/**
	 * On Expiration Date Change
	 *
	 * @param	IPSnexusPurchase	$purchase	The purchase
	 * @return	void
	 */
	public function onExpirationDateChange( IPSnexusPurchase $purchase )
	{
	
	}
	
	/**
	 * On Purchase Expired
	 *
	 * @param	IPSnexusPurchase	$purchase	The purchase
	 * @return	void
	 */
	public function onExpire( IPSnexusPurchase $purchase )
	{

	}
	
	/**
	 * On Purchase Canceled
	 *
	 * @param	IPSnexusPurchase	$purchase	The purchase
	 * @return	void
	 */
	public function onCancel( IPSnexusPurchase $purchase )
	{

	}
	
	/**
	 * On Purchase Deleted
	 *
	 * @param	IPSnexusPurchase	$purchase	The purchase
	 * @return	void
	 */
	public function onDelete( IPSnexusPurchase $purchase )
	{

	}
	
	/**
	 * On Purchase Reactivated (renewed after being expired or reactivated after being canceled)
	 *
	 * @param	IPSnexusPurchase	$purchase	The purchase
	 * @return	void
	 */
	public function onReactivate( IPSnexusPurchase $purchase )
	{

	}
	
	/**
	 * On Transfer (is ran before transferring)
	 *
	 * @param	IPSnexusPurchase	$purchase		The purchase
	 * @param	IPSMember			$newCustomer	New Customer
	 * @return	void
	 */
	public function onTransfer( IPSnexusPurchase $purchase, IPSMember $newCustomer )
	{

	}
	
	/**
	 * On Upgrade/Downgrade
	 *
	 * @param	IPSnexusPurchase							$purchase				The purchase
	 * @param	IPSnexusPackage							$newPackage				The package to upgrade to
	 * @param	int|NULL|IPSnexusPurchaseRenewalTerm	$chosenRenewalOption	The chosen renewal option
	 * @return	void
	 */
	public function onChange( IPSnexusPurchase $purchase, IPSnexusPackage $newPackage, $chosenRenewalOption = NULL )
	{
	
	}

 

czw, 01 wrzesień 2016