News:

Support the VirtueMart project and become a member

Main Menu

Diffrent look of invoice based on selected payment method

Started by BKintera, February 22, 2018, 18:41:23 PM

Previous topic - Next topic

BKintera

Hi,


I need to add some text at the bottom of the invoice. Problem is that I want diffrent text based on selected payment method.

So, if user select "Paypal", than I need for example this text at the bottom of invoice: "Lorem ipsum dolor sit amet.", and if the user select "Standard" payment method I need diffrent text for example "Excepteur sint occaecat cupidatat non proident".


I saw this topic on the forum:
https://forum.virtuemart.net/index.php?topic=106459.msg355209#msg355209

And I added  this code to invoice_items.php file:


<?php if ($this->cart->cartData['paymentName'] != 'No payment selected') {
    echo 
'<p>Test</p>';
}
?>



After that I saw string 'Test' at the bottom of the invoice, so I tried to change that code a little bit to accomplish my goal, I tried to add this code to invoice_items.php file:


<?php if ($this->cart->cartData['paymentName'] == 'Standard') {
    echo 
'<p>Test</p>';
}
?>



But when I purchased product with payment method called "Standard" there was no string 'Test' at the bottom of the invoice.

Any idea how can I do this?

Thanks.

VM: 3.2.4
Joomla: 3.8.1

Studio 42

Invoice use $this->orderDetails and file components/com_virtuemart/views/invoice/tmpl/invoice_items.php (or override in your template)
So you have to use
$this->orderDetails['paymentName'];
in your sample code :
<?php if ($this->orderDetails['paymentName'] == 'Standard') {
    echo 
'<p>Test</p>';
}
?>

BKintera

Studio 42,

Thank you for your reply.

I tried to add in my override invoice-items.php file:

<?php if ($this->orderDetails['paymentName'] == 'Standard') {
    echo 
'<p>Test</p>';
}
?>



But it doesn't show the string, so I tried:


<?php if ($this->orderDetails['paymentName'] != 'Standard') {
    echo 
'<p>Test</p>';
}
?>




And than I see 'Test' in invoice.

I have double checked payment method name and even change it to small caps but no effect.

Is it posible to trigger payment method ID instead of name?

Studio 42

use
<?php var_dump($this->orderDetails['paymentName']) ; ?>
To dump the real variable
Or <?php echo htmlspecialchars($this->orderDetails['paymentName']); ?> so you see the full HTML

BKintera

I used

<?php echo htmlspecialchars($this->orderDetails['paymentName']); ?>

and result was:

<span class="vmpayment_name">standard</span><br/>

So I edited standard payment plugin file "standard.php" and deleted <br/> just to see if that is the problem.

After that result, as expected, was:
<span class="vmpayment_name">standard</span>

But still code:


<?php if ($this->orderDetails['paymentName'] == 'standard') {
    echo 
'Test';
}
?>



doesn't work. I'm really confused right now.

Studio 42

<?php if ($this->orderDetails['paymentName'] == 'standard') {
    echo 
'Test';
}
?>

Change to
<?php if ($this->orderDetails['paymentName'] == '<span class="vmpayment_name">standard</span>') {
    echo 
'Test';
}
?>

Or use php strip tags to remove the HTML before compare

BKintera

Studio 42 thank you very much for your help!

Now, I accomplished my goal :)

Maybe someone else will need this:


<?php
    $payment_name 
strip_tags($this->orderDetails['paymentName']);
    
$payment_name2 substr($payment_name0);

    if (
$payment_name2 == " standard") {
        echo 
'Test';
    }
?>



Best regards

Studio 42

$payment_name2 = substr($payment_name, 0); does nothing
If you want remove all space or tabs use
$payment_name2 = trim($payment_name, 0);
At end you should have "standard" only
<?php
    $payment_name 
trim(strip_tags($this->orderDetails['paymentName']));

    if (
$payment_name == "standard") {
        echo 
'Test';
    }
?>

But in all case, i had never have do it so but checked for $this->orderDetails['details']['BT']->virtuemart_paymentmethod_id, the ID is same as in payments back-end list

BKintera

I have change the code a little bit, this is what I'm using now:


<?php
    $payment_name 
strip_tags($this->orderDetails['paymentName']);
    
$payment_name2 substr($payment_name010);

    switch (
$payment_name2) {
        case 
" Direct Ba":
            echo 
'This is Direct Bank Transfer!';
            break;
        case 
" Paypal":
            echo 
'This is paypal!';
            break;
        case 
" Sofort ba":
            echo 
'This is sofort bank!';
            break;
        case 
" Per Nachn":
            echo 
'This is per nachname!';
            break;
        case 
" Credit Ca":
            echo 
'This is credit card!';
            break;    
        case 
" Debit Car":
            echo 
'This is Debit card!';
            break;
        case 
" iDeal":
            echo 
'This is iDeal!';
            break;
        case 
" Per Rechn":
            echo 
'This is Per Rechnung!';
            break;
        default:
            echo 
"Something is wrong";
    }
?>



I need substr($payment_name, 0, 10); because some of payment methods also have description and if I don't set limit to 10 characters, I would have to copy/paste Payment Name and Payment Description, because $this->orderDetails['paymentName'] gives Payment Name + Payment Desc.

I will try to do it by payment ID, it would be more efficient because this is multilingual store.


Edit:

This code work also:


<?php
    $payment_id 
$this->orderDetails['details']['BT']->virtuemart_paymentmethod_id;
    
    switch (
$payment_id) {
        case 
"8":
            echo 
'This is Direct Bank!';
            break;
        case 
"15":
            echo 
'This is some payment!';
            break;
        default:
            echo 
"Something is wrong";
    }
?>



And it's much simplier to do it like this, because I don't need to count characters and to add cases for payment name in English, German, French...

Again, Studio 42 thank you very much for your time, you helped me a lot.