X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Changing design (https://forum.x-cart.com/forumdisplay.php?f=51)
-   -   Customize invoice based on product purchased (https://forum.x-cart.com/showthread.php?t=64903)

chiactivate 09-19-2012 05:42 PM

Customize invoice based on product purchased
 
Anyone know how to customize this?

I want to show the different message in the invoice based on the product.

After customer has completed the order, an invoice be show at the thank you page and another email will be send by email.

How can I.

add message A invoice A (product A)
add message B in invoice B (product B)

is there a solution already?

totaltec 09-20-2012 04:53 AM

Re: Customize invoice based on product purchased
 
You would need to loop through all the products in the order with a foreach loop, and write an if statement inside the loop that tested for the productids.

chiactivate 09-20-2012 10:17 AM

Re: Customize invoice based on product purchased
 
thanks Total Tech!

How do I create that loop with if statement and how to find the productids?

I know how how to create a new invoice.

totaltec 09-20-2012 10:38 AM

Re: Customize invoice based on product purchased
 
I am a little crunched for time so I am going to try and guide you through without testing what I am telling you, if it doesn't work, don't despair, just let me know. All of this is pseudo code.

First look at: /common_files/customer/main/order_message.tpl

Look for: {foreach from=$orders item=order}

Right under this is the included invoice template {include file="mail/html/order_invoice.tpl"...

We want to switch between templates so setup an if statement:
PHP Code:

{if $some_var eq 1}
  {include 
file="mail/html/special_invoice.tpl"...
{else}
  {include 
file="mail/html/order_invoice.tpl"...
{/if} 


Above that we need to test for the product, right after the foreach statement that loops through the orders, start another one and check for the product id. if it is found assign the $some_var we referenced in our if statement:
PHP Code:

{assign var="some_var" value=0}
{foreach 
from=$order.products item=product}
  {if 
$product.productid eq 23}
   {
assign var="some_var" value=1}
  {/if}
{/foreach} 


Make sense?

chiactivate 09-20-2012 11:11 AM

Re: Customize invoice based on product purchased
 
You are awesome! Many thanks!!!!

Will try it and let you know whether it works

Pyro 09-26-2012 09:29 AM

Re: Customize invoice based on product purchased
 
Quote:

Originally Posted by chiactivate
You are awesome! Many thanks!!!!

Will try it and let you know whether it works




Did Mike's example end up working for you? I am looking to do something similar.


I will give it a try when I get home in a few hours, but I just wanted to see how things ended up for you.

Thanks!

Pyro 09-26-2012 03:36 PM

Re: Customize invoice based on product purchased
 
Mike,

Thank you for the base that you provided. Could you please see where I am going wrong?

My scenario is that I want to display a certain message if a certain product is ordered.

As such, I have gone right to mail/html/order_invoice.tpl .


Here is the code that I am currently trying. The values in red are values that I have tried in with quotes and without quotes, but I get the same result either way.


Code:

{assign var="ordered8" value=0}
{foreach from=$order.products item=product}
  {if $product.productid eq "8"}
  {assign var="ordered8" value=1}
  {/if}
{/foreach} 



{if $ordered8 eq 1}
  extra thank you
{else}
no thank you for you
 {/if} 






The output is always "no thank you for you", even if product 8 was ordered and is on the invoice.


Is there anything that you can see that I am missing?


Thank you very much for any guidance!

totaltec 09-26-2012 06:20 PM

Re: Customize invoice based on product purchased
 
James, in each template that you are working in you need to make sure that the variable you are trying to access is actually assigned, and that it contains the info you are looking for. Watch my tuts on webmaster mode to see how I would figure this out. Another way to check this is to use print_r Try just putting {$order|@print_r} to output the entire arrays contents.

The reason that your code is not working is very simple, $order does not actually contain the code you are looking for! In this template the products have been assigned to the $products array. To understand why, take a look at /common_files/customer/main/order_message.tpl look at the code that calls /common_files/mail/html/order_invoice.tpl
Line 42:
Quote:

{foreach from=$orders item=order}
{include file="mail/html/order_invoice.tpl" is_nomail='Y' products=$order.products giftcerts=$order.giftcerts userinfo=$order.userinfo order=$order.order}
<br />

See how the template include, also assigns the variable "products" to $order.products and reassigns "order" to $order.order? That is how the $products array gets populated, and the reason that $order mysteriously doesn't contain the products array.

Smarty is a very easy, forgiving language and it tolerates simple mistakes, but since you are bent on learning I want to point out the ones in this code. (Commented)
PHP Code:

{assign var="ordered8" value=0
{*
/* You can drop the "" quotes around ordered8,
after all you are assigning a variable not
passing a string. */
*}

{foreach 
from=$order.products item=product}
{*
/* Here is the important change: 
     $products instead of $order.products 
*/
*}

  {if 
$product.productid eq "8"}
  {*
/* again drop the quotes around 8, it is an integer. */*}

   {
assign var="ordered8" value=1
   {*
/* ditto */*}

  {/if}
{/foreach}  

{if 
$ordered8 eq 1}
  
extra thank you
{else}
no thank you for you
 
{/if}
{*
/* ^Straighten up that spacing soldier!
     Form up those lines! :-)
*/
*} 


Hope that explains it in a way that you can apply elsewhere. Have a good one!

-Mike

Pyro 09-26-2012 06:44 PM

Re: Customize invoice based on product purchased
 
Mike,


Thank you for the detailed reply. I know you are busy and the help is greatly appreciated. I have watched a few of your videos and I will definitely give the smarty one a try.


As for this code, this is what I have ended up with.


Code:

{assign var=ordered817 value=0}
{foreach from=$products item=product}
  {if $product.productid eq 8 or $product.productid eq 1 or $product.productid eq 7}
    {assign var=ordered8 value=1}
  {/if}
{/foreach} 

{if $ordered817 eq 1}
  extra thank you
{else}
 {/if}


{assign var=ordered234 value=0}
{foreach from=$products item=product}
  {if $product.productid eq 2 or $product.productid eq 3 or $product.productid eq 4}
    {assign var=ordered8 value=1}
  {/if}
{/foreach} 

{if $ordered234 eq 1}
  some other thank you
{else}
 {/if}




This code appears to have the desired effect; however, is there a better way to condense the two? The only way I can see doing it is by using a few ifelse statements, but that causes its own issue. Doing it that way would mean that I would have to define all of the possible combinations and all of the outputs.


Thanks again!

totaltec 09-26-2012 06:54 PM

Re: Customize invoice based on product purchased
 
James, you are welcome. If you have a large volume of special messages then you should try to find a php solution, like assigning the message to each product in the db. Then you can just loop through the products and foreach output the products special message.

Let's just do it like this (untested psuedo code):
HTML Code:


{foreach from=$products item=product}
  {if $product.productid eq 8 or $product.productid eq 1 or $product.productid eq 7}
    {assign var=ordered8 value="You bought a boat!"}   
  {/if}
  {if $product.productid eq 2 or $product.productid eq 3 or $product.productid eq 4}
    {assign var=ordered7 value="You bought some life jackets!"}   
  {/if} 
{/foreach}

{if $ordered8 ne '' and $ordered7 ne ''}
  You bought some life jackets and a boat!
{elseif $ordered8 ne ''}
  {$ordered8}
{elseif $ordered7 ne ''}
  {$ordered7}
{else}
  Go get a boat, and some $*%# life jackets, loser!
{/if}

Make sense?

Pyro 09-26-2012 07:22 PM

Re: Customize invoice based on product purchased
 
Quote:

Originally Posted by totaltec
Make sense?



Hmm, I think I follow. Ne is for negative, so you are basically saying not to include that, but what does the ne '' do? If negative nothing?


Thanks!

totaltec 09-26-2012 07:25 PM

Re: Customize invoice based on product purchased
 
ne is for "not equals" (!=). I am saying if this not equals nothing, display it, else display that. There are a thousand ways to word these things, you can test for positive or negative and go from there. Finding the best way, now that is an art. :-)

Pyro 09-26-2012 08:20 PM

Re: Customize invoice based on product purchased
 
Hmm, not equals makes more sense. :)


This thread threw me off. http://forum.x-cart.com/showthread.php?t=60942


The guy asked about negative and you responded ne. :oops:



Thanks again for all of the help. I think I understand this now (or at least 1000x more than I did before).

Pyro 09-26-2012 09:27 PM

Re: Customize invoice based on product purchased
 
Sorry Mike, one more question for you about the variables.


Does everything have to be defined?

I used {$order|@print_r} to print the variables and I see nothing related to count.


I guess what I am asking is can I count the number of each SKU and use that number in a basic math problem?


Here is an example of the logic that I am thinking:

HTML Code:

{foreach from=$products item=product}
  {if $product.productid eq 8}
    {assign var=count8 value=(count($product.productid eq 8))}   
  {/if}
  {if $product.productid eq 2}
    {assign var=count7 value=(count($product.productid eq 7))}   
  {/if} 
{/foreach}


{if $count8 ne '' and $count7 ne ''}
  Text[{$count7}*17]+[{$count8}*27]
{elseif $count7 ne ''}
  Text{$count7}*17
{elseif $count8 ne ''}
  Text{$count8}*27
{else}
  Go get a boat, and some $*%# life jackets, loser!
{/if}



(Just incase I randomly became a genius at this in 4 posts, I checked to see if my attempt at pseudo code worked. Its safe to say it didn't for those of you that may try it in the future. ;) )

totaltec 09-27-2012 04:54 AM

Re: Customize invoice based on product purchased
 
To get the total number of rows in the products array, try {$products|@count}

To know what the current index of the array is use {$smarty.foreach.myproducts.index} -to use .index or .iteration you need to name your foreach loop.
Example:
Quote:

{foreach from=$products item=product name=myproducts}
{$smarty.foreach.myproducts.index}
{/foreach}

Be warned that some of this is on the fringe of needing to be done in PHP. Smarty is a template engine used for presentation of data. Using it to perform logical operations is against the principles behind smarty. (purist nonsense to some)

Are you perhaps trying to determine how many of a certain product they bought? Then you would use {$product.amount}

Pyro 09-27-2012 08:02 AM

Re: Customize invoice based on product purchased
 
Quote:

Originally Posted by totaltec
Are you perhaps trying to determine how many of a certain product they bought? Then you would use {$product.amount}





Mike,


That is exactly what I am trying to do. I have 2 products that I am interested in counting on the invoice, multiplying each by a set number, and then adding the mathematical products together, so that my end result is one number.

I think you may even tear up on this one, but I managed to come up with what I think is the correct smarty math function (or at least it is pretty close). :-) {math equation="(( y * 9 ) + ( z * 18 ))" y=$county z=$countz}




As such, I have edited my code to be this:


HTML Code:

{foreach from=$products item=product}
  {if $product.productid eq 1}
    {assign var=orderedy value={$product.amount}}   
  {/if}
  {if $product.productid eq 7 or $product.productid eq 5 or $product.productid eq 6}
    {assign var=orderedz value={$product.amount}}   
  {/if} 
{/foreach}

{if $orderedy ne '' and $orderedz ne ''}
  text{math equation="(( y * 9 ) + ( z * 18 ))" y=$county z=$countz}
{elseif $orderedy ne ''}
  text{math equation="( y * 9 )" y=$county}
{elseif $orderedz ne ''}
  text{math equation="( z * 18 )" z=$countz}
{else}
  Go get a boat, and some $*%# life jackets, loser!
{/if}




I tested the code with an order that had 1 prodid=6 and the only output was
Code:

}
. Any idea where I went wrong? I think the issue is with how I am using $product.amount.







-------------------------------Edit-----------------------

I noticed that I was not defining the countz and county variables anywhere, so I revised my code to this, but I still get the same output.

Code:

{foreach from=$products item=product}
  {if $product.productid eq 1}
    {assign var=orderedy value={$product.amount}}   
  {/if}
  {if $product.productid eq 7 or $product.productid eq 5 or $product.productid eq 6}
    {assign var=orderedz value={$product.amount}}   
  {/if} 
{/foreach}

{if $orderedy ne '' and $orderedz ne ''}
  text{math equation="(( y * 9 ) + ( z * 18 ))" y=$orderedy z=$orderedz}
{elseif $orderedy ne ''}
  text{math equation="( y * 9 )" y=$orderedy}
{elseif $orderedz ne ''}
  text{math equation="( z * 18 )" z=$orderedz}
{else}
  Go get a boat, and some $*%# life jackets, loser!
{/if}


Thanks!

totaltec 09-27-2012 08:09 AM

Re: Customize invoice based on product purchased
 
James I can't test the code right now. I am not at my normal IP address. I can tell you what looks suspicious to me:
{assign var=orderedy value={$product.amount}} --There is no need for those internal brackets. Since you are already inside the brackets with your assign, you don't use them again when accessing variables. Try fixing that and let me know.

Pyro 09-27-2012 08:29 AM

Re: Customize invoice based on product purchased
 
Mike,


Thanks! That definitely helped as it is now displaying a number at least!

On an invoice that has 5 of product#7 and 1 of product#6, the output is 18. (which is the output of the 1 product#6.)


It seems the issue is the or statement. It is not combining the count of each of the products in the or, but rather 1 of them.


It seems that I should switch which side of the equation the or is on.

This is my attempt, which only outputs "text" for an order with 5 product#7 and 1 product#6.




HTML Code:


{foreach from=$products item=product}
  {if $product.productid eq 1}
    {assign var=ordered1 value=$product.amount}   
  {/if}
  {if $product.productid eq 5}
    {assign var=ordered5 value=$product.amount}   
  {/if} 
  {if $product.productid eq 6}
    {assign var=ordered6 value=$product.amount}   
  {/if}
  {if $product.productid eq 7}
    {assign var=ordered7 value=$product.amount}   
  {/if}
{/foreach}

{if $ordered1 ne '' or $ordered5 ne '' or $ordered6 ne '' or $ordered7 ne ''}
  text{math equation="(( w * 9 ) + ( z * 18 ) + ( y * 18 ) + ( z * 18 ))" w=$ordered1 x=$orderedz5 y=$ordered6 z=$ordered7}
{else}
  Go get a boat, and some $*%# life jackets, loser!
{/if}


totaltec 09-27-2012 09:46 AM

Re: Customize invoice based on product purchased
 
Aha! I think I see it. :-)

Smarty cannot use non integer variables in a math equation. Since some of your variables are not assigned like w=$ordered1 then it returns null. "null" doesn't add up and breaks the math equation.

You can handle this several ways by assigning a value of 0 to ordered1 etc, or by using |default:0 which is my preference.

Consider this code:
PHP Code:

{if $ordered1 ne '' or $ordered5 ne '' or $ordered6 ne '' or $ordered7 ne ''}
  
You bought {$ordered1|default:0boats, {$ordered5|default:0life jackets, {$ordered6|default:0anchors, and {$ordered7|default:0paddles!
   
You get {math equation="(( w * 9 ) + ( z * 18 ) + ( y * 18 ) + ( z * 18 ))" w=$ordered1|default:0 x=$ordered5|default:0 y=$ordered6|default:0 z=$ordered7|default:0points!
{else}
  
Go get a boat, and some $*%# life jackets, loser!
{/if} 


BTW, you also have a small typo: x=$orderedz5

Pyro 09-27-2012 11:54 AM

Re: Customize invoice based on product purchased
 
Quote:

Originally Posted by totaltec
Aha! I think I see it. :-)

Smarty cannot use non integer variables in a math equation. Since some of your variables are not assigned like w=$ordered1 then it returns null. "null" doesn't add up and breaks the math equation.

You can handle this several ways by assigning a value of 0 to ordered1 etc, or by using |default:0 which is my preference.




Brilliant! I was hoping it would just assume non intergers as 0, for purposes of math equations, but that obviously wasn't the case. Everything is working now. Thanks again.





Quote:

BTW, you also have a small typo: x=$orderedz5


THESE DAMN Z's!! I also noticed that it was outputting an unexpected number. Upon closer inspection:
Quote:

text{math equation="(( w * 9 ) + ( z * 18 ) + ( y * 18 ) + ( z * 18 ))" w=$ordered1 z=$orderedz5 y=$ordered6 z=$ordered7}
{else}


should have been:
Quote:

text{math equation="(( w * 9 ) + ( z * 18 ) + ( y * 18 ) + ( z * 18 ))" w=$ordered1 x=$orderedz5 y=$ordered6 z=$ordered7}
{else}


Hopefully that helps someone that comes along later.

P.S. for anyone that needs it, I added format="%.2f" to my code in order to display 2 decimal places (like you would need in the case of currency).

Code:

{math equation="(( w * 1.96 ) + ( x * 1.50 ) + ( y * 1.50 ) + ( z * 1.50 ))" w=$ordered1|default:0 x=$ordered5|default:0 y=$ordered6|default:0 z=$ordered7|default:0 format="%.2f"}

totaltec 09-27-2012 12:01 PM

Re: Customize invoice based on product purchased
 
Glad you got it sorted out. Thanks for posting your final solution, helps the forum be a better place.

Pyro 09-27-2012 12:04 PM

Re: Customize invoice based on product purchased
 
Quote:

Originally Posted by totaltec
Glad you got it sorted out. Thanks for posting your final solution, helps the forum be a better place.




:) No worries. I know what a life saver the forum is. My goal is always to follow up, so the next guy can figure it out even quicker.

chiactivate 10-10-2012 10:57 AM

Re: Customize invoice based on product purchased
 
Hi James,

sorry for the late reply. Got stucked with something else. Will try this soon and help you out

Pyro 10-10-2012 03:22 PM

Re: Customize invoice based on product purchased
 
Quote:

Originally Posted by chiactivate
Hi James,

sorry for the late reply. Got stucked with something else. Will try this soon and help you out




Don't worry about it, everything is already resolved. :)

chiactivate 10-11-2012 04:44 PM

Re: Customize invoice based on product purchased
 
Thank You So Much!!!

You guys are awesome. This is great custom mod for everybody!

This should be a featured thread.


All times are GMT -8. The time now is 10:53 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.