News:

Looking for documentation? Take a look on our wiki

Main Menu

Get Virtuemart products by category name or id

Started by Kuubs, November 20, 2017, 17:18:49 PM

Previous topic - Next topic

Kuubs

Hello,

Is there a function in Virtuemart that gets all the products from a certain category? Where you have to insert the category id or name?

I want to display all my products from a subcategory but I have to get all these products first before I can display them?

Greetings,


GJC Web Design

$productModel = VmModel::getModel('Product');
         
         $products = $productModel->getProductListing($group, $nbrReturnProducts, $withCalc, $onlyPublished, $single, $filterCategory, $category_id, $filterManufacturer, $manufacturer_id);
GJC Web Design
VirtueMart and Joomla Developers - php developers https://www.gjcwebdesign.com
VM4 AusPost Shipping Plugin - e-go Shipping Plugin - VM4 Postcode Shipping Plugin - Radius Shipping Plugin - VM4 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
https://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

Kuubs

#2
Thank you. Do I have to fill in all the variables? Or is the category id sufficient or not?

Got it, I got the list of the products. How do I display these products using the products sublayout?

Because this doesn't work:


$productModel = VmModel::getModel('Product');
         
$productsChildren = $productModel->getProductListing($group, $nbrReturnProducts, $withCalc, $onlyPublished, $single, $filterCategory, $category_id = $this->category->children->virtuemart_category_id, $filterManufacturer, $manufacturer_id);

echo shopFunctionsF::renderVmSubLayout('products',array('products'=>$productsChildren,'currency'=>$this->currency,'products_per_row'=>$this->perRow,'showRating'=>$this->showRating));

GJC Web Design

GJC Web Design
VirtueMart and Joomla Developers - php developers https://www.gjcwebdesign.com
VM4 AusPost Shipping Plugin - e-go Shipping Plugin - VM4 Postcode Shipping Plugin - Radius Shipping Plugin - VM4 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
https://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

Kuubs

#4
Quote from: GJC Web Design on November 21, 2017, 12:20:07 PM
what doesn't work about it?

if I use my own sublayout of the products, the products don't get displayed.

If I use the standard sublayout of Virtuemart I get a weird list of this:

Products
COM_VIRTUEMART_1_PRODUCT
COM_VIRTUEMART_2_PRODUCT
COM_VIRTUEMART_3_PRODUCT
COM_VIRTUEMART_4_PRODUCT
COM_VIRTUEMART_5_PRODUCT
COM_VIRTUEMART_6_PRODUCT
COM_VIRTUEMART_7_PRODUCT
COM_VIRTUEMART_8_PRODUCT
COM_VIRTUEMART_9_PRODUCT
COM_VIRTUEMART_10_PRODUCT
COM_VIRTUEMART_11_PRODUCT
COM_VIRTUEMART_12_PRODUCT
COM_VIRTUEMART_13_PRODUCT

Quote from: GJC Web Design on November 21, 2017, 12:20:07 PM


what doesn't work about it?
Any other idea? Do I only need to insert the products and not the currency and stuff?

Kuubs

Nobody? I mean it's shouldn't be this difficult to display the products right?

I have the same products array that the main category has, but the child products wont get displayed.

Ghost

Try this:
$productModel = VmModel::getModel('Product');
$productsChildren = array();
$productsChildren['products'] = $productModel->getProductListing(false, false, true, true, false, true, $this->category->children->virtuemart_category_id, false, 0);
$productModel->addImages($productsChildren['products']);
echo shopFunctionsF::renderVmSubLayout('products',array('products'=>$productsChildren,'currency'=>$this->currency,'products_per_row'=>$this->perRow,'showRating'=>$this->showRating));

Kuubs

#7
Quote from: Ghost on November 23, 2017, 10:24:44 AM
Try this:
$productModel = VmModel::getModel('Product');
$productsChildren = array();
$productsChildren['products'] = $productModel->getProductListing(false, false, true, true, false, true, $this->category->children->virtuemart_category_id, false, 0);
$productModel->addImages($productsChildren['products']);
echo shopFunctionsF::renderVmSubLayout('products',array('products'=>$productsChildren,'currency'=>$this->currency,'products_per_row'=>$this->perRow,'showRating'=>$this->showRating));

If I use this it displays the same products as the parent category. There aren't also any values if I print_r that. If I use my own code, which has the actual products from the childcategory, it does display the products, but the problem arises in the fact that these are all the products that are in Virtuemart, not only those from the child category.

EDIT: OK so I edited the query. if you put the variable $filterCategory on true, it only displays the products in the category you are viewing. If not you display all the products from the store. So there is some setting you can put it that fixes this. But I don't know where to find it.

Ghost

Category filter is already set to true but the problem is in the use of $this->category->children->virtuemart_category_id. It doesn't give any value. $this->category->children is an array of categories so it doesn't fit to be used with getProductListing. You could get an ID of one category by using array key, e.g. $this->category->children[0]->virtuemart_category_id.

May I ask what you're trying to accomplish here? If you want to show products grouped by child categories, you could try this:


$productModel = VmModel::getModel('Product');
foreach($this->category->children as $child){
$productsChildren = array();
$productsChildren['products'] = $productModel->getProductListing(false, false, true, true, false, true, $child->virtuemart_category_id, false, 0);
$productModel->addImages($productsChildren['products']);
echo shopFunctionsF::renderVmSubLayout('products',array('products'=>$productsChildren,'currency'=>$this->currency,'products_per_row'=>$this->perRow,'showRating'=>$this->showRating));
}

Kuubs

Quote from: Ghost on November 23, 2017, 14:41:16 PM
Category filter is already set to true but the problem is in the use of $this->category->children->virtuemart_category_id. It doesn't give any value. $this->category->children is an array of categories so it doesn't fit to be used with getProductListing. You could get an ID of one category by using array key, e.g. $this->category->children[0]->virtuemart_category_id.

May I ask what you're trying to accomplish here? If you want to show products grouped by child categories, you could try this:


$productModel = VmModel::getModel('Product');
foreach($this->category->children as $child){
$productsChildren = array();
$productsChildren['products'] = $productModel->getProductListing(false, false, true, true, false, true, $child->virtuemart_category_id, false, 0);
$productModel->addImages($productsChildren['products']);
echo shopFunctionsF::renderVmSubLayout('products',array('products'=>$productsChildren,'currency'=>$this->currency,'products_per_row'=>$this->perRow,'showRating'=>$this->showRating));
}


I'm essentially trying to display the child category items below the main category items. With the children->category id I can get the products that are from the subcategory. I want to display these products below the main category items.

Kuubs

Quote from: Ghost on November 23, 2017, 14:41:16 PM
Category filter is already set to true but the problem is in the use of $this->category->children->virtuemart_category_id. It doesn't give any value. $this->category->children is an array of categories so it doesn't fit to be used with getProductListing. You could get an ID of one category by using array key, e.g. $this->category->children[0]->virtuemart_category_id.

May I ask what you're trying to accomplish here? If you want to show products grouped by child categories, you could try this:


$productModel = VmModel::getModel('Product');
foreach($this->category->children as $child){
$productsChildren = array();
$productsChildren['products'] = $productModel->getProductListing(false, false, true, true, false, true, $child->virtuemart_category_id, false, 0);
$productModel->addImages($productsChildren['products']);
echo shopFunctionsF::renderVmSubLayout('products',array('products'=>$productsChildren,'currency'=>$this->currency,'products_per_row'=>$this->perRow,'showRating'=>$this->showRating));
}


Thank you. This is the solution.

Dimi

Hello,
I used this code for the same thing and it works great. My problem is that I have some products with two categories. Is it possible to display these products twice (once in each category)?

pinochico

In my example I see products in two categories, but we don't use code from this forum.

We hires developer and pay 6*50 EUR for finally solutions.

I can ask this developer, if he want make this solutions for you, if you want.
www.minijoomla.org  - new portal for Joomla!, Virtuemart and other extensions
XML Easy Feeder - feeds for FB, GMC,.. from products, categories, orders, users, articles, acymailing subscribers and database table
Virtuemart Email Manager - customs email templates
Import products for Virtuemart - from CSV and XML
Rich Snippets - Google Structured Data
VirtueMart Products Extended - Slider with products, show Others bought, Products by CF ID and others filtering products

Dimi

QuoteI can ask this developer, if he want make this solutions for you, if you want.
No, thank you. I found a workaround for this.

However, I noticed that this code doen't get the product custom fields. In the default category view template the custom fields display correctly, but in my custom template they don't.

I tried using this
$customfieldsModel = VmModel::getModel ('Customfields');
$productsChildren->customfields = $customfieldsModel->getCustomEmbeddedProductCustomFields($productsChildren->allIds,0,-1, true);

but nothing happend.

How can I get the custom fields to display?

pinochico

Thanks for your info and I hope someone will advise you better than I do
www.minijoomla.org  - new portal for Joomla!, Virtuemart and other extensions
XML Easy Feeder - feeds for FB, GMC,.. from products, categories, orders, users, articles, acymailing subscribers and database table
Virtuemart Email Manager - customs email templates
Import products for Virtuemart - from CSV and XML
Rich Snippets - Google Structured Data
VirtueMart Products Extended - Slider with products, show Others bought, Products by CF ID and others filtering products