Skocz do zawartości
Zaloguj się, aby obserwować  
Invisionize.eu

[Dokumentacja IPS4] Commerce: Custom Actions

Polecane posty

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 )
	{
	
	}

 

Wyświetl pełny artykuł

Udostępnij ten post


Link to postu
Udostępnij na innych stronach

Bądź aktywny! Zaloguj się lub utwórz konto

Tylko zarejestrowani użytkownicy mogą komentować zawartość tej strony

Zaloguj się, aby obserwować  

  • Kto przegląda   0 użytkowników

    Brak zalogowanych użytkowników przeglądających tę stronę.

×

Ważne informacje

Kontynuując przeglądanie strony, wyrażasz zgodę na używanie przez nas plików cookies.