Duplicates of table are created for each item in invoice?

Started by mailblade, October 31, 2017, 08:04:34 AM

Previous topic - Next topic

mailblade

Good day,

Latest VirtueMart and Joomla 3.4.

I have added some code to my "details_items.php" override. The reason is simple. I have products with different storage temperatures; these are grouped: "Dry, Ambient, Chilled" & "Frozen".

I made a copy of the table in "details_items_php" and pasted it below the 1st table. The reason I want two tables is simply to group the products by storage temperature.

Group 1: "Dry, Ambient, Chilled"
Group 2: "Frozen"

I have added this above the 1st table:

<?php 
foreach($this->orderDetails['items'] as $item) {
$qtt $item->product_quantity ;
$product_link JURI::root().'index.php?option=com_virtuemart&view=productdetails&virtuemart_category_id=' $item->virtuemart_category_id .
'&virtuemart_product_id=' $item->virtuemart_product_id '&Itemid=' $menuItemID;

foreach(
$item->customfields as $customfields) { 
if(
$customfields->virtuemart_custom_id==21 && $customfields->customfield_value!=="Frozen") { ?>

//table data such as SKU, product name and all other functions such as the price calculation etc. comes here
<?php } } } ?> //closing tags


The 2nd table code:

<?php 
foreach($item->customfields as $customfields) { 
if(
$customfields->virtuemart_custom_id==21 && $customfields->customfield_value=="Frozen")  { ?>

//table data such as SKU, product name and all other functions such as the price calculation etc. comes here
<?php } } ?> //closing tags


NOTE: The 2nd table IS working correctly. If I have 3 "Frozen" "customfield_values", it will display the 3 products in one single table. However, if I remove this piece of code from the first table foreach($this->orderDetails['items'] as $item) {
$qtt = $item->product_quantity ;
$product_link = JURI::root().'index.php?option=com_virtuemart&view=productdetails&virtuemart_category_id=' . $item->virtuemart_category_id .
'&virtuemart_product_id=' . $item->virtuemart_product_id . '&Itemid=' . $menuItemID;
; none of the tables are displaying.


What it is currently doing, is duplicating the WHOLE table for each single entry where the "customfield_value!=="Frozen" and WHERE "virtuemart_customfield_id==21". So if I have 3 products which each have "Dry", "Ambient" and "Chilled" as a "customfield_value", it will duplicate the table 3 times displaying all three products in each table, instead of just one table.

The thing is, I know the reason it's doing it. It is because I inserted the "foreach($this->orderDetails['items'] as $item){ }" function, which clearly states that for each item it will do the following piece of code (which is displaying the whole table).

However, if I do not add this function above the first table, not one of the tables will work.

Any help would be appreciated. I have tried adding this (still above the first table), which would logically seem to work but unfortunately nothing happens if I add this:

1st Table

<?php 
$item 
$this->orderDetails['items'] ;
$customfields $item->customfields ;

if(
$customfields->virtuemart_custom_id==21 && $customfields->customfield_value!=="Frozen"){ ?>

//display the table data

<?php ?>


2nd Table

<?php 
$item 
$this->orderDetails['items'] ;
$customfields $item->customfields ;

if(
$customfields->virtuemart_custom_id==21 && $customfields->customfield_value=="Frozen"){ ?>

//display the table data

<?php ?>



Another important note
Just above the 2nd "<tr>" of the table (which starts the "<td> element"); I also included this code:

1st Table

<?php
foreach($item->customfields as $customfields) {
if(
$customfields->virtuemart_custom_id==21 && $customfields->customfield_value!=="Frozen"){ ?>

//<td> data
<?php ?>


2nd Table

<?php 
foreach($item->customfields as $customfields) {
if(
$customfields->virtuemart_custom_id==21 && $customfields->customfield_value=="Frozen"){ ?>

//<td> data
<?php ?>


This would eliminate it showing all the values for each row and only show the row data where "customfield_value" is either "Frozen" or "Dry" , "Ambient", "Chilled".

Summary

I know why it's creating the duplicate table if I insert the code above the first table; what I do not know is how to find a solution for it. As stated, the 2nd table will work, but only if the "foreach($this->orderDetails as $item){}" code is included above the 1st table.

I've tried using custom variables as seen in the middle piece of code in this post ($item = $this->orderDetails['items']; ) , but I literally can not find a solution.

Thanks for reading and I appreciate any help :)





Studio 42

I had do it so.
$frozens = array();
$items = $this->orderDetails['items'] ;
foreach($items as $item) {
foreach($item->customfields as $cf) {
  if($cf->virtuemart_custom_id==21 && $cf->customfield_value=="Frozen")  {
    $frozens[] = $item;
    continue;
  }
}
// your code for standard items  render
}

foreach($frozens as $item) {
// your code for frozens items,but should be same as standard items render.
}

mailblade

Thank you Studio 42 for your reply.

I will be giving this a test and see how it performs :)