<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NemoPS &#187; downloads</title>
	<atom:link href="https://nemops.com/tag/downloads/feed/" rel="self" type="application/rss+xml" />
	<link>https://nemops.com</link>
	<description>Prestashop Tutorials, Modules and More!</description>
	<lastBuildDate>Wed, 05 Dec 2018 13:25:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.35</generator>
	<item>
		<title>Specific payment methods for virtual products in Prestashop</title>
		<link>https://nemops.com/prestashop-virtual-products-specific-payment/</link>
		<comments>https://nemops.com/prestashop-virtual-products-specific-payment/#comments</comments>
		<pubDate>Tue, 07 Apr 2015 09:39:48 +0000</pubDate>
		<dc:creator><![CDATA[Nemo]]></dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[downloads]]></category>
		<category><![CDATA[payment]]></category>
		<category><![CDATA[virtual products]]></category>

		<guid isPermaLink="false">http://nemops.com/?p=2364</guid>
		<description><![CDATA[<p>In this quick tip we will see how to enable or disable payment methods for virtual products in Prestashop If you are selling both virtual and normal products, it might be worth displaying your customers specific payment methods depending on what they&#8217;re buying. It&#8217;s useless, for example, to allow them to pay by cash on [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://nemops.com/prestashop-virtual-products-specific-payment/">Specific payment methods for virtual products in Prestashop</a> appeared first on <a rel="nofollow" href="https://nemops.com">NemoPS</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>In this quick tip we will see how to enable or disable payment methods for virtual products in Prestashop</p>
<p><span id="more-2364"></span></p>
<p>If you are selling both virtual and normal products, it might be worth displaying your customers specific payment methods depending on what they&#8217;re buying. It&#8217;s useless, for example, to allow them to pay by cash on delivery something they can download soon after purchase! Similarly, you might want to offer a specific digital payment for virtual products only.</p>
<p>It is important to know that the modification we are about to apply <strong>needs to be made on every single payment module you want to restrict</strong>. This means having to hard-modify core files on older prestashop versions, or using overrides on newer than 1.6.0.11. For simplicity, I will modify core files, but have a look at my tutorial on <a href="http://nemops.com/override-prestashop-modules-core/#.VRlOVfnQqr0" target="_blank" title="how to override Prestashop Modules' core files">how to override Prestashop Modules&#8217; core files</a>.</p>
<div class="separator"></div>
<h2>The hookPayment method</h2>
<p>The simplest way to restrict our payment methods is to modify the method named <strong>hookPayment</strong>, used by every single Prestashop Payment module. It&#8217;s the function responsible of displaying payment methods during the checkout, so hiding them here will prevent people from continuing the purchase with them.</p>
<p>We will use bankwire as an example, but any other will equally do. Since non-standard modules might have their own rules, it&#8217;s better to add these lines of code at the very beginning of the method.</p>
<p>Therefore, open up <strong>modules/bankwire/bankwire.php</strong> (or use overrides), and scroll to the <strong>hookPayment</strong> method, which looks like this:</p>
<pre class="brush: php; title: ; notranslate">

	public function hookPayment($params)
	{

		if (!$this-&gt;active)
			return;
		if (!$this-&gt;checkCurrency($params['cart']))
			return;

		$this-&gt;smarty-&gt;assign(array(
			'this_path' =&gt; $this-&gt;_path,
			'this_path_bw' =&gt; $this-&gt;_path,
			'this_path_ssl' =&gt; Tools::getShopDomainSsl(true, true).__PS_BASE_URI__.'modules/'.$this-&gt;name.'/'
		));
		return $this-&gt;display(__FILE__, 'payment.tpl');
	}

</pre>
<p>First of all, we need to check which products are in the cart</p>
<pre class="brush: php; title: ; notranslate">
$products = $this-&gt;context-&gt;cart-&gt;getProducts();
</pre>
<p>Then, we loop through them, and see if any is virtual:</p>
<pre class="brush: php; title: ; notranslate">
		$products = $this-&gt;context-&gt;cart-&gt;getProducts();

		if($products)
		{
			foreach ($products as $product) {
				if ($product['is_virtual'])
				{
					// what to do?
				}		
			}
		}
</pre>
<p>It&#8217;s dead easy, isn&#8217;t it? Now we can choose what to do, let&#8217;s try removing bankwire for virtual products!</p>
<pre class="brush: php; title: ; notranslate">
		$products = $this-&gt;context-&gt;cart-&gt;getProducts();

		if($products)
		{
			foreach ($products as $product) {
				if ($product['is_virtual'])
				{
					return false;
				}		
			}
		}
</pre>
<p>What happens now? This will kill the bankwire payment method whenever someone has a virtual product in the cart. It means, of course that it won&#8217;t be accessible even if there are other products there. This might lead to unwanted situations, when no method is available at all.</p>
<p>For example, you have two methods: bankwire, and paypal. You remove bankwire with this method on virtual products, and paypal on others. What happens when a user has both a virtual and normal product in the cart? <strong>No method will be available!</strong> We therefore need to make a choice, whether to leave a warning in the cart page (shopping-cart.tpl) or explore another way.</p>
<p>We might want to only hide the method if <strong>all</strong> products are virtual, for example. How to?</p>
<p>The easiest way is to use $this->context->cart->isVirtualCart(), which will return true if all products in it are virtual. Alternatively, we can do it manually ourselves: </p>
<pre class="brush: php; title: ; notranslate">
	if($products)
	{
		$virtuals = 0;
		foreach ($products as $product) {
			if ($product['is_virtual'])
			{
				$virtuals++;
			}		
		}
		if($virtuals == count($products))
			return false;
	}
</pre>
<p>In this case, we use a counter to check how many virtual products we have. If all of them are virtual, we disable the module! How can we revere it then?</p>
<pre class="brush: php; title: ; notranslate">
		$products = $this-&gt;context-&gt;cart-&gt;getProducts();

		if($products)
		{
			foreach ($products as $product) {
				if (!$product['is_virtual'])
				{
					return false;
				}		
			}
		}
</pre>
<p>This will disable bankwire whenever a non-virtual product is in the cart.</p>
<pre class="brush: php; title: ; notranslate">
	if($products)
	{
		$nonvirtuals = 0;
		foreach ($products as $product) {
			if (!$product['is_virtual'])
			{
				$nonvirtuals++;
			}		
		}
		if($nonvirtuals == count($products))
			return false;
	}
</pre>
<p>While this other will hide it when all products are physical.</p>
<div class="separator"></div>
<h2>Conclusion</h2>
<p>There are many ways to utilize the restriction, depending on your needs (such as even buying a certain number of products!). Just keep in mind that you might end up not having any payment available if you mix up the wrong conditions!</p>
<p>The post <a rel="nofollow" href="https://nemops.com/prestashop-virtual-products-specific-payment/">Specific payment methods for virtual products in Prestashop</a> appeared first on <a rel="nofollow" href="https://nemops.com">NemoPS</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://nemops.com/prestashop-virtual-products-specific-payment/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
