News:

Support the VirtueMart project and become a member

Main Menu

How to detect if a product is a parent in the category default view ?

Started by Jörgen, June 12, 2015, 11:16:37 AM

Previous topic - Next topic

Jörgen

Hello

I am using
VM 3.0.8
Joomla 3.4.1

I want to display the stock value in the category view and this works quite well, but there is a small issue.
If the product diplayed is a parent with Child products I don´t want to show the stock of the parent. Since this
parent is normally not orderable that stock value shown is of no interest. In this case I want to check if the
product is a parent an hide the stock value for products that have child Products.

How can this be done ?

best regards

Jörgen @ Kreativ Fotografi


Joomla 3.9.18
Virtuemart 3.4.x
Olympiantheme Hera (customized)
This reflects current status when viewing old post.

PRO

what have you tried?

ON the backend,  I know this is used.
$this->product_childs

I dont use childs, so its hard to test.

but try

if (!empty($product->product_childs)){echo 'child true';}


1 long/resourceful way to go about it, is to get the product_parent_id     from the product table, and then check if in_array()   each product. But thats not ideal.






Jörgen

Hello Pro

I have tried to catch the child selector in a similar fashion as You proposed check the $this->product_childs. I have looked through the $this array in search for anything including child. But I could not find any. I will have a new go at it. The other way around is easier, the child has always a reference to it´s parent.

I will try this tonight, thanks for Your reply.

Jörgen @ Kreativ Fotografi

Joomla 3.9.18
Virtuemart 3.4.x
Olympiantheme Hera (customized)
This reflects current status when viewing old post.

PRO

Quote from: Jörgen on June 16, 2015, 08:06:23 AM
Hello Pro

I have tried to catch the child selector in a similar fashion as You proposed check the $this->product_childs. I have looked through the $this array in search for anything including child. But I could not find any. I will have a new go at it. The other way around is easier, the child has always a reference to it´s parent.

I will try this tonight, thanks for Your reply.

Jörgen @ Kreativ Fotografi



category uses   $product->

NOT $this->

check the variables

print_r($product);

Jörgen

Hello Pro

Thanks for Your efforts. But I can not find anything in the structure $product that has child in the name. I need a better idea.

Probably has to load a model and get info about the children that way.

regards

Jörgen @ Kreativ Fotografi


Joomla 3.9.18
Virtuemart 3.4.x
Olympiantheme Hera (customized)
This reflects current status when viewing old post.

PRO

Quote from: Jörgen on June 16, 2015, 21:44:38 PM
Hello Pro

Thanks for Your efforts. But I can not find anything in the structure $product that has child in the name. I need a better idea.

Probably has to load a model and get info about the children that way.

regards

Jörgen @ Kreativ Fotografi




The product table has

product_parent_id

this will load all the parent ids


<?php function getParentIds(){
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('product_parent_id');
$query->from($db->quoteName('#__virtuemart_products'));
$query->where('product_parent_id!=0');
$db->setQuery($query);
$rows = $db->loadAssocList();
return $rows;
}?>

Then, you check if its a parent, and display it differently.

Studio 42

Hi Pro,
The query is not good and not the fastest:

//for SELECT `virtuemart_product_id` FROM `j3vm3_virtuemart_products` where `product_parent_id` = 15 limit 0,1
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('virtuemart_product_id');
$query->from($db->quoteName('#__virtuemart_products'));
$query->where('product_parent_id='.(int)$product->virtuemart_product_id);
$db->setQuery($query);
$hasChild = $db->loadResult(); // it's not needed to get the number of result

PRO

Quote from: Studio 42 on June 16, 2015, 23:31:27 PM
Hi Pro,
The query is not good and not the fastest:

//for SELECT `virtuemart_product_id` FROM `j3vm3_virtuemart_products` where `product_parent_id` = 15 limit 0,1
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('virtuemart_product_id');
$query->from($db->quoteName('#__virtuemart_products'));
$query->where('product_parent_id='.(int)$product->virtuemart_product_id);
$db->setQuery($query);
$hasChild = $db->loadResult(); // it's not needed to get the number of result



Then, you have to run the query on each product.
If you select all the parents 1 time, then check if in_array on each product, wont it be faster?

Studio 42

This is the answer to
.
Quote from: Jörgen on June 12, 2015, 11:16:37 AM
I want to check if the
product is a parent an hide the stock value for products that have child Products.
on a single product

Jörgen

Hello

Thanks for Your replys. suspected that this would boil down to a new query to get the info I need.

I want to use this in order to hide the stock display in the category view for products with children. I have no idea if it would be faster with a query loading the result from each and every product in the database or if a query for each displayed product would be faster.

I hope that I can test this tonight and make some comparison between both methods. My database is not so big, only 1800 products, so loading everything once might be a faster alternative. I will let You know.

thanks

Jörgen @ Kreativ Fotografi
Joomla 3.9.18
Virtuemart 3.4.x
Olympiantheme Hera (customized)
This reflects current status when viewing old post.

Studio 42

This is another possible solution to get all together

$q = 'SELECT p2.product_parent_id FROM `#__virtuemart_product_categories` as pc
LEFT JOIN #__virtuemart_products as p on p.virtuemart_product_id = pc.virtuemart_product_id
LEFT JOIN #__virtuemart_products as p2 on p2.product_parent_id = p.virtuemart_product_id
WHERE virtuemart_category_id = '.$this->category->virtuemart_category_id.' GROUP BY p2.product_parent_id';
$db->setQuery($q);
$productsWithChildrensInCategory = $db->loadAssocList('product_parent_id');

This does not count how much you have child(it's faster and only need one value)
YOu can then test
if(!empty($productsWithChildrensInCategory[$productID])) { .....

Jörgen

@ Patrick

Thanks for query
$q = 'SELECT p2.product_parent_id FROM `#__virtuemart_product_categories` as pc
LEFT JOIN #__virtuemart_products as p on p.virtuemart_product_id = pc.virtuemart_product_id
LEFT JOIN #__virtuemart_products as p2 on p2.product_parent_id = p.virtuemart_product_id
WHERE virtuemart_category_id = '.$this->category->virtuemart_category_id.' GROUP BY p2.product_parent_id';
$db->setQuery($q);
$productsWithChildrensInCategory = $db->loadAssocList('product_parent_id');

placing it in the category default view worked great.

Now I just have to figure out how to access $productsWithChildrensInCategory in the sublayout Products.
Did not have much time last night, will get back to You as soon as I get it right.

regards

Jörgen @ Kreativ Fotografi
Joomla 3.9.18
Virtuemart 3.4.x
Olympiantheme Hera (customized)
This reflects current status when viewing old post.

Studio 42

something so:
foreach ($this->products as $p) {
  if(!empty($productsWithChildrensInCategory[$p->virtuemart_product_id])) $p->hasChildren = true;
  else $p->hasChildren = false;
}
echo shopFunctionsF::renderVmSubLayout('productsincat',array('products'=>$products,'currency'=>$this->currency,'products_per_row'=>$this->perRow,'showRating'=>$this->showRating));


and set a new sublayout in virtuemart(or in your template) : productsincat.php

in this sublayout  you can check then for $product->hasChildren;

Jörgen

@ Patrick

Thank You very much. I included the following lines in html/com_virtuemart/category/default.php
// Get array with products that has children !
// Thanks to Studio 42 Patrick

$db = JFactory::getDbo();
$q = 'SELECT p2.product_parent_id FROM `#__virtuemart_product_categories` as pc
LEFT JOIN #__virtuemart_products as p on p.virtuemart_product_id = pc.virtuemart_product_id
LEFT JOIN #__virtuemart_products as p2 on p2.product_parent_id = p.virtuemart_product_id
WHERE virtuemart_category_id = '.$this->category->virtuemart_category_id.' GROUP BY p2.product_parent_id';
$db->setQuery($q);
$productsWithChildrensInCategory = $db->loadAssocList('product_parent_id');

// Mark every product that has children with hasChildren true
foreach ($this->products as $p) {
  if(!empty($productsWithChildrensInCategory[$p->virtuemart_product_id])) {
$p->hasChildren = true;
}
  else {
$p->hasChildren = false;
}
}

Just after the following line, approx line 101, VM 3.0.8:
if (!empty($this->products)) {

I then make my test in html/com_virtuemart/sublayouts/products.php
if (!$product->hasChildren) {
I replaced the contents in the block <div class="vm-product-rating-container"> with the following:
echo '<strong>' . vmText::_('COM_VIRTUEMART_PRODUCT_IN_STOCK') . ': </strong>' ;
$nrAvail = ($product->product_in_stock - $product->product_ordered);
if ($nrAvail > 5)
echo vmText::_('COM_VIRTUEMART_MORE_THAN_FIVE_IN_STOCK');
elseif ($nrAvail > 1)
echo $nrAvail;
elseif ($nrAvail > 0)
  echo vmText::_('COM_VIRTUEMART_ONLY_ONE_IN_STOCK');
else echo '0';


Studio 42 and Pro, thank You so much for Your help :)

regards

Jörgen @ Kreativ Fotografi



Joomla 3.9.18
Virtuemart 3.4.x
Olympiantheme Hera (customized)
This reflects current status when viewing old post.