VirtueMart Forum

VirtueMart 2 + 3 + 4 => General Questions => Topic started by: dirkb on October 21, 2016, 20:56:25 PM

Title: product weight in email confirmation
Post by: dirkb on October 21, 2016, 20:56:25 PM
VM 3.018 Joomla 3.63

hmmm ... I saw 2 posts regarding that, but none work for me.

I want to add a column in the items table. I would like to have the product single weight top after the product name, and a summary with the total weight of the order.

anybody ideas or has done that before ?

thanks d.
Title: Re: product weight in email confirmation
Post by: GJC Web Design on October 22, 2016, 11:10:59 AM
the product weights are in the order products object so just extract them, x by quantity and display

in the cart list template
Title: Re: product weight in email confirmation
Post by: dirkb on October 25, 2016, 10:35:57 AM
well ... tried ...

<?php echo $this->product->product_weight ?>
<?php echo $this->product->product_weight_uom ?>
<?php echo $this->product->product_weight.$this->product->product_weight_uom ?>

not showing anything in the invoice_items.php

Why is it so complicated to show the individual weight and the total weight in the confirmation email ? shaking my head here ...
Title: Re: product weight in email confirmation
Post by: Ghost on October 25, 2016, 11:16:26 AM
It's $item->product_weight. You must add it inside this loop:

foreach($this->orderDetails['items'] as $item) {
...
}
Title: Re: product weight in email confirmation
Post by: dirkb on October 25, 2016, 11:31:07 AM
you mean like that ?

<?php foreach($this->orderDetails['items'] as $item) {
$item->product_weight;
} ?>

I also tried another snippet for total weight which I found on another thread ... not working either

<?php echo $totalWeight = 0;
                        foreach ( $this->orderDetails['items'] as $items ) {
            $q = 'SELECT `product_weight` FROM `#__virtuemart_products` WHERE `virtuemart_product_id`=' . $items->virtuemart_product_id;
            $db = JFactory::getDbo();
            $db->setQuery($q);
            $productWeight = $db->loadResult();
            $totalWeight += $productWeight * $items->product_quantity;
         }
         echo $totalWeight; ?>
Title: Re: product weight in email confirmation
Post by: Ghost on October 25, 2016, 11:36:18 AM
Like that but you forgot echo.
Title: Re: product weight in email confirmation
Post by: dirkb on October 25, 2016, 11:50:06 AM
thanks ... you are my hero of the day, now I have to figure out total weight =)
Title: Re: product weight in email confirmation
Post by: Milbo on October 25, 2016, 13:07:50 PM
Quote from: dirkb on October 25, 2016, 10:35:57 AM
Why is it so complicated to show the individual weight and the total weight in the confirmation email ? shaking my head here ...

You must admit, it was not complicated. There are also tools to help with issues like, is it product_weight  or productWeight. When you edit your layouts. Use the overrides and learn how to use the vmdebug. vmdebug('my this here',$this); would have shown you, that there is no $this->product available.
Title: Re: product weight in email confirmation
Post by: dirkb on October 25, 2016, 18:01:33 PM
thanks milbo, I will take a look into it. The "easy" part depends on from what side you look at it. From mine, its not. My php knowledge is very limited. I can copy paste and sometimes, I´m lucky replacing parts of the code and I have a "Yeah" moment, and it works. Tiny things like that might take me days to figure out on my own, if ... lots of times, it does not work.

For example ... that easy thing with single product weigt and total product weight - I tried for 1 day without success, went to the forum, tried, did not work or not correctly. Now, I´m still lost ... for the single product weight, using the code ...

<?php foreach($this->orderDetails['items'] as $item) {
echo $item->product_weight;
} ?>

I get with a product with 22 kg a  display of 22.0000. How can I reduce that to 2 decimals after the comma.
In addition ... if I have another product with 0.5 kg ... its displaying the weight like 0.500022.0000 ... on each single product line.
The total is displayed fine, 22.5 ... but what do I miss with the single product weight ?

See screen ... thanks if anybody has some spare time, it should be easy I guess.

d.
Title: Re: product weight in email confirmation
Post by: Milbo on October 25, 2016, 21:29:37 PM
Quote from: dirkb on October 25, 2016, 18:01:33 PM
thanks milbo, I will take a look into it. The "easy" part depends on from what side you look at it. From mine, its not. My php knowledge is very limited. I can copy paste and sometimes, I´m lucky replacing parts of the code and I have a "Yeah" moment, and it works. Tiny things like that might take me days to figure out on my own, if ... lots of times, it does not work.

It is easier to invest 30 minutes and to learn the basics of php. We need only basic php here. Just read this pages https://www.tutorialspoint.com/php/php_variable_types.htm
https://www.tutorialspoint.com/php/php_loop_types.htm
https://www.tutorialspoint.com/php/php_arrays.htm

You can find this also in german!
The rest is to handle an object, instead of an array, which uses for properties and functions just the -> . The rest ist to learn the available variables by reading the code and/or with vmdebug. So for example you wanted the weight of a product within the loop. If you understand the foreach() thing, which is reeeaally simple!, then it is quite easy. It is clear, that it cant be any "$this->...", because you are inside the loop and want to access the items of the looped array. But people sometimes think it is too hard to learn that, but no, that is not the hard part in programming.

Quote from: dirkb on October 25, 2016, 18:01:33 PM
For example ... that easy thing with single product weigt and total product weight - I tried for 1 day without success, went to the forum, tried, did not work or not correctly. Now, I´m still lost ... for the single product weight, using the code ...
Because you think yourself it is easier to stay away from it then just to learn it. But of course, yes. You start to change your mind :-)

Quote from: dirkb on October 25, 2016, 18:01:33 PM
<?php foreach($this->orderDetails['items'] as $item) {
echo $item->product_weight;
} ?>

I get with a product with 22 kg a  display of 22.0000. How can I reduce that to 2 decimals after the comma.
Enter "php round" in your searchengine, take the first link  http://php.net/manual/de/function.round.php
There you see

float round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] )

It is not hard to read it it. The normal command is just round($myNumber); any parameter set in [] is optional. So in your case you want to round to 2 digits (Nachkommastellen) and your variable is not $myNumber, it is $item->product_weight and the return value is a "float".

So you can just write

round($item->product_weight, 2)


Quote from: dirkb on October 25, 2016, 18:01:33 PM
In addition ... if I have another product with 0.5 kg ... its displaying the weight like 0.500022.0000 ... on each single product line.
The total is displayed fine, 22.5 ... but what do I miss with the single product weight ?

Because you missed to read the simple chapter about variable types. In fact it is quite hard to understand all consequences of the variable types. But the simple understanding is enough for templating.

When you want to concatenate Strings (Wörter zusammenfügen), then you use the dot "." .

For example

echo 'my length '. $item->product_length .' and my height '.$item->product_height;


But you want to sum up numbers, values, not strings. So you must use the classic + sign.
Title: Re: product weight in email confirmation
Post by: Jörgen on October 25, 2016, 21:53:10 PM
@Milbo

Very nice to see such great generosity and mentorship when trying to explain the basics in programming and php.

But remember, "No good deed ever goes unpunished"  ;)

+5 from me

regards

Jörgen @ Kreativ Fotografi
Title: Re: product weight in email confirmation
Post by: Milbo on October 25, 2016, 23:16:20 PM
Thank you very much, in fact the idea is to create a small tutorial out of it, with the most important basics for layouting. 80% of the layout code is quite simple and easy to read, if you know the basics how to read program code.
Title: Re: product weight in email confirmation
Post by: dirkb on October 26, 2016, 17:03:20 PM
well, I guess I have to say thank you too - got quiet lectured and a lesson too
Title: Re: product weight in email confirmation
Post by: Milbo on October 27, 2016, 00:01:04 AM
a draft https://docs.virtuemart.net/tutorials/development/224-php-basics-for-templater.html
Title: Re: product weight in email confirmation
Post by: Jörgen on October 27, 2016, 00:06:10 AM
Thank You Milbo

This looks promising. Nice to get a better understanding about the scope for data passed through views in VM.

regards

Jörgen @ Kreativ Fotografi