<?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; order status</title>
	<atom:link href="http://nemops.com/tag/order-status/feed/" rel="self" type="application/rss+xml" />
	<link>http://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>Adding a &#8220;I received my order&#8221; button in Prestashop</title>
		<link>http://nemops.com/order-received-button-prestashop/</link>
		<comments>http://nemops.com/order-received-button-prestashop/#comments</comments>
		<pubDate>Wed, 05 Feb 2014 10:10:14 +0000</pubDate>
		<dc:creator><![CDATA[Nemo]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[order status]]></category>
		<category><![CDATA[orders]]></category>
		<category><![CDATA[prestashop]]></category>

		<guid isPermaLink="false">http://nemops.com/?p=1843</guid>
		<description><![CDATA[<p>For many types of business it is fundamental to know whether the order has been received by the client or not, especially when using non-traceable carriers. Today we will give our customers an option to mark an order as received, saving us time and trouble. Compatibility: Prestashop 1.5 Why asking customers if they received an [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://nemops.com/order-received-button-prestashop/">Adding a &#8220;I received my order&#8221; button in Prestashop</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>For many types of business it is fundamental to know whether the order has been received by the client or not, especially when using non-traceable carriers. Today we will give our customers an option to mark an order as received, saving us time and trouble.</p>
<p><span id="more-1843"></span></p>
<a class="download-files button style1" href="http://nemops.com/wp-content/uploads/2014/02/order_received.zip" title="Download Project Files">Download Project Files</a>
<ul>
<li>Compatibility: Prestashop 1.5</li>
</ul>
<div class="separator"></div>
<h2>Why asking customers if they received an order?</h2>
<p>Depending on the type of business you run, you might want to be sure some items have actually been delivered to your customers, whether you ship with tracked carriers or free postage (without tracking number).</p>
<p>Unless you integrated the carrier&#8217;s API into Prestashop, so that an order status can automatically be updated upon destination reach (impossible in the latter case, anyway), you&#8217;d find yourself having to manually check each and every order, to make sure it has actually been received. Giving customers the chance to manually notify themselves whether an order was delivered or not can save us a huge amount of time, and partially overcome the non-tracked carrier&#8217;s flaws.</p>
<p>In this tutorial, we will make use of one override, and modify 2 theme files. Specifically:</p>
<ul>
<li>OrderDetailController Override</li>
<li>order-detail.tpl</li>
<li>history.js	</li>
</ul>
<p>We will make use of the default order statuses <strong>Shipped</strong> and <strong>Delivered</strong> for the given example, but feel free to use your own, keeping in mind their ids as we add the necessary code snippets.</p>
<p>Next thing to notice is that, by default, the order detail page is grabbed using javascript, directly from the history page, and thus we have to account for an ajax call that points to the controller override.</p>
<p>Lastly, please note that the following applies to registered customers only, not to guests.</p>
<div class="separator"></div>
<h2>Step 1 &#8211; Adding the &#8220;I received this order&#8221; button</h2>
<p>The very first thing we need to do is add some kind of button the customer can click. The ideal page for this is, of course, the order detail one, which can be accessed via the user account panel:</p>
<p><a href="http://nemops.com/wp-content/uploads/2014/02/orderdetail.png"><img src="http://nemops.com/wp-content/uploads/2014/02/orderdetail-680x554.png" alt="Prestashop order detail" width="680" height="554" class="aligncenter size-large wp-image-1847" /></a></p>
<p>As you can see, this order has been shipped already, and thus the customer should be able to tell us if it was delivered or not. Since there is a step-by-step order follow section already, let&#8217;s add our button there. After making sure you got a theme&#8217;s backup in case of failure, open order-detail.tpl, located, in my case, in <em>themes/default/</em>.</p>
<p>Look for:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;h3&gt;{l s='Follow your order\'s status step-by-step'}&lt;/h3&gt;
</pre>
<p>And add the following right after, but <strong>before</strong> the table_block div:</p>
<pre class="brush: php; html-script: true; title: ; notranslate">
{if $order_history.0.id_order_state == 4}
	&lt;form action=&quot;{$link-&gt;getPageLink('order-detail', true)|escape:'html'}&quot; method=&quot;post&quot; class=&quot;std&quot; id=&quot;markAsReceived&quot;&gt;
		&lt;input type=&quot;hidden&quot; class=&quot;hidden&quot; value=&quot;{$order-&gt;id|intval}&quot; name=&quot;id_order&quot; /&gt;
		&lt;input type=&quot;submit&quot; class=&quot;exclusive&quot; name=&quot;markAsReceived&quot; id=&quot;markAsReceivedBtn&quot; value=&quot;{l s='I have received this order'}&quot;&gt;
		&lt;p class=&quot;clear&quot;&gt;&lt;/p&gt;
	&lt;/form&gt;

{/if}
</pre>
<p><strong>Explanation:</strong> Remember we mentioned the default <strong>order statuses</strong> at the beginning of the article? Well, in this conditional <strong>if</strong> statement, we are taking advantage of the <strong>Shipped</strong> status, which has 4 as status ID. Therefore, customers will only see the button if the current order has been set to Shipped. We access it using the variable <strong>$order_history</strong>, which is an array. Therefore, we get the most recent element&#8217;s status (which is the first one of the stack). <br />
Then, we open a form that points to the order-detail page (and relative controller), so that our <strong>markAsReceived</strong> parameter is sent to it. We also need to pass the order id to this form, as the system needs to know which order&#8217;s status needs to be updated later.</p>
<p><a href="http://nemops.com/wp-content/uploads/2014/02/orderdetailbutton.png"><img src="http://nemops.com/wp-content/uploads/2014/02/orderdetailbutton-680x293.png" alt="Prestashop order detail - adding the order received button" width="680" height="293" class="aligncenter size-large wp-image-1848" /></a></p>
<p>We are basically done with the template part. We&#8217;ll come back later to add a confirmation message upon successful order status change.</p>
<h3>How to check order status IDs?</h3>
<p>If you do not wish to use the default statuses, you can easily create your own, and check their IDs from the back office <em>Orders > Statuses</em>.</p>
<p><a href="http://nemops.com/wp-content/uploads/2014/02/orderstatuses.png"><img src="http://nemops.com/wp-content/uploads/2014/02/orderstatuses-680x540.png" alt="Prestashop Order Statuses" width="680" height="540" class="aligncenter size-large wp-image-1849" /></a></p>
<p>The IDs are, needless to say, the ones under the ID column.</p>
<div class="separator"></div>
<h2>Step 2 &#8211; OrderDetailController override</h2>
<p>Create a new php file in <em>override/controllers/front</em> and call it <strong>OrderDetailController.php</strong>. Inside it, add the following within php tags:</p>
<pre class="brush: php; title: ; notranslate">
class OrderDetailController extends OrderDetailControllerCore
{

	public function postProcess()
	{
		parent::postProcess();

		if (Tools::isSubmit('markAsReceived'))
		{
			$idOrder = (int)(Tools::getValue('id_order'));
			$order = new Order($idOrder);

			if(Validate::isLoadedObject($order))
			{
				if($order-&gt;getCurrentState() == 4) // if the order is shipped
				{
					$new_history = new OrderHistory();
					$new_history-&gt;id_order = (int)$order-&gt;id;
					$new_history-&gt;changeIdOrderState(5, $order); // 5: delivered
					$new_history-&gt;addWithemail(true);	
				}

				$this-&gt;context-&gt;smarty-&gt;assign('receipt_confirmation', true);

			} else $this-&gt;_errors[] = Tools::displayError('Error: Invalid order number');
			
		}		

	}


}


</pre>
<p><strong>Explanation:</strong> uh, quite a heavy jump! However, nothing special here, really. We are overriding the core&#8217;s postProcess method, which occurs right after having loaded the controller&#8217;s basic properties, so that other data can be collected and stored. In this case, we first make sure to refer back to the original postProcess(), then check if our <strong>markAsReceived</strong> button has been pressed. <br />
If it has, we grab the order ID we passed, then create a new order object using it. It&#8217;s fundamental to always make sure a Prestashop Object was correctly loaded every time, using <strong>Validate::isLoadedObject($object)</strong>. </p>
<p>If it is a valid object, and then if the current state is shipped (ID = 4) we can add a new order status. To do this, we use the <strong>OrderHistory</strong> class, setting the current order id ($new_history->id_order = (int)$order->id;) and then changing its status to <strong>5</strong> (delivered). Lastly, we submit the order change with $new_history->addWithemail(true). In case an email is to be sent out, it will. After this, we confirm the order has been received and assign a variable to the template, in order to show a confirmation message.</p>
<p>On the other end, we display an error in case the order object was not valid, for any reason.
</p>
<p>At this point, we can already test out the form. But remember to delete the <strong>class_index</strong> file, located inside the <em>/cache/</em> folder first, for the override to take place.</p>
<p>If you did everything correctly, the whole page will be refreshed, redirecting you to the order-detail controller, but with the &#8216;delivered&#8217; status added.</p>
<blockquote><p><span class="bqstart">“</span>It&#8217;s fundamental to always make sure a Prestashop Object was correctly loaded every time, using <strong>Validate::isLoadedObject($object)</strong><span class="bqend">”</span></p></blockquote>
<h3>The confirmation message</h3>
<p>As we want to be polite to our customers, right before the form we added to <strong>order-detail.tpl</strong>, insert the following snippets, to thank them for their feedback.</p>
<pre class="brush: php; html-script: true; title: ; notranslate">
{if isset($receipt_confirmation) &amp;&amp; $receipt_confirmation}
	&lt;p class=&quot;success&quot;&gt;
		{l s='Thank you for your feedback!'}
	&lt;/p&gt;
{/if}
</pre>
<p>So it looks like:</p>
<pre class="brush: php; html-script: true; title: ; notranslate">
&lt;h3&gt;{l s='Follow your order\'s status step-by-step'}&lt;/h3&gt;
{if isset($receipt_confirmation) &amp;&amp; $receipt_confirmation}
	&lt;p class=&quot;success&quot;&gt;
		{l s='Thank you for your feedback!'}
	&lt;/p&gt;
{/if}
{if $order_history.0.id_order_state == 4}
	&lt;form action=&quot;{$link-&gt;getPageLink('order-detail', true)|escape:'html'}&quot; method=&quot;post&quot; class=&quot;std&quot; id=&quot;markAsReceived&quot;&gt;


		&lt;input type=&quot;hidden&quot; class=&quot;hidden&quot; value=&quot;{$order-&gt;id|intval}&quot; name=&quot;id_order&quot; /&gt;
		&lt;input type=&quot;submit&quot; class=&quot;exclusive&quot; name=&quot;markAsReceived&quot; id=&quot;markAsReceivedBtn&quot; value=&quot;{l s='I have received this order'}&quot;&gt;
		&lt;p class=&quot;clear&quot;&gt;&lt;/p&gt;
	&lt;/form&gt;

{/if}
</pre>
<p>We could stop here; however, to give our customers a better user experience, we will integrate this button to the ajax style of the order history page.</p>
<div class="separator"></div>
<h2>Step 3 &#8211; Adding some Ajax to history.js</h2>
<p>Open up <strong>history.js</strong>, located in the theme folder, <em>/js/</em>. We have to catch the submit event of the new form first. a suitable place for this is right after the following:</p>
<pre class="brush: jscript; title: ; notranslate">
				$('form#sendOrderMessage').submit(function(){
					return sendOrderMessage();
				
				});
</pre>
<p>That catches the submission of the &#8220;Send a message&#8221; form. You can find it at line 73 in Prestashop 1.5.6.2. Right after, add:</p>
<pre class="brush: jscript; title: ; notranslate">
				$('form#markAsReceived').submit(function(){
					return markAsReceived();
				});
</pre>
<p>Okay, we are returning a function instead of submitting the page. We don&#8217;t have it yet, so let&#8217;s use the sendOrderMessage() one as a base for ours. Add this at the end of the file:</p>
<pre class="brush: jscript; title: ; notranslate">
				
function markAsReceived()
{
	paramString = &quot;ajax=true&quot;;
	$('#markAsReceived').find('input').each(function(){
		paramString += '&amp;' + $(this).attr('name') + '=' + encodeURIComponent($(this).val());
	});
	$.ajax({
		type: &quot;POST&quot;,
		headers: { &quot;cache-control&quot;: &quot;no-cache&quot; },
		url: $('#markAsReceived').attr(&quot;action&quot;) + '?rand=' + new Date().getTime(),
		data: paramString,
		success: function (msg){
			$('#block-order-detail').fadeOut('slow', function() {
				$(this).html(msg);
				//catch the submit event of sendOrderMessage form
				
				$(this).find('#markAsReceivedBtn').fadeOut;
				$(this).fadeIn('slow');
			});
		}
	});
	return false;
}

</pre>
<p><strong>Explanation:</strong> despite having a scary look, there is not much going on under the hood. We first tell the script we will be using ajax, then add each parameter in the form to the POST call we&#8217;ll do. In this case, they will be <strong>markAsReceived</strong> and <strong>id_order</strong>, as we need them in the controller. <br />
Then, we do an ajax call to our override (the url being called is the same of the action property of the form!), passing a random parameter to avoid duplicate calls. At this point, all the previously mentioned operations occur. Upon success, we fade out the entire order detail block, change its content with the one retrieved from the ajax call, fade out the mark as received button and show the order detail block again. Don&#8217;t forget to return false, or call e.preventDefault() at the end, to avoid the page refresh!</p>
<p>Save &#038; test, no page refresh will now occur anymore!</p>
<p><strong>TIP:</strong> instead of creating dozens of new orders for testing purposes, after the status is set to delivered, take it back to shipped so our button is visible again! (as i did in the following image)</p>
<p><a href="http://nemops.com/wp-content/uploads/2014/02/last-screen.png"><img src="http://nemops.com/wp-content/uploads/2014/02/last-screen-680x500.png" alt="Prestashop - I received my order confirmed" width="680" height="500" class="aligncenter size-large wp-image-1850" /></a></p>
<div class="separator"></div>
<h2>Conclusion</h2>
<p>Customers can now inform us when they receive an order. However, an ideal extension to this would be an automated system that sends out emails to all customers who have &#8220;shipped&#8221; but not &#8220;delivered&#8221; orders. Well, we might do it in another tutorial!</p>
<p>The post <a rel="nofollow" href="http://nemops.com/order-received-button-prestashop/">Adding a &#8220;I received my order&#8221; button in Prestashop</a> appeared first on <a rel="nofollow" href="http://nemops.com">NemoPS</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nemops.com/order-received-button-prestashop/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
	</channel>
</rss>
