VirtueMart Forum

VirtueMart 2 + 3 + 4 => General Questions => Topic started by: Kuubs on November 20, 2017, 17:18:49 PM

Title: Get Virtuemart products by category name or id
Post by: Kuubs on November 20, 2017, 17:18:49 PM
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,

Title: Re: Get Virtuemart products by category name or id
Post by: GJC Web Design on November 21, 2017, 01:15:16 AM
$productModel = VmModel::getModel('Product');
         
         $products = $productModel->getProductListing($group, $nbrReturnProducts, $withCalc, $onlyPublished, $single, $filterCategory, $category_id, $filterManufacturer, $manufacturer_id);
Title: Re: Get Virtuemart products by category name or id
Post by: Kuubs on November 21, 2017, 09:29:28 AM
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));
Title: Re: Get Virtuemart products by category name or id
Post by: GJC Web Design on November 21, 2017, 12:20:07 PM
what doesn't work about it?

Title: Re: Get Virtuemart products by category name or id
Post by: Kuubs on November 21, 2017, 12:40:15 PM
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?
Title: Re: Get Virtuemart products by category name or id
Post by: Kuubs on November 23, 2017, 09:34:46 AM
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.
Title: Re: Get Virtuemart products by category name or id
Post by: 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));
Title: Re: Get Virtuemart products by category name or id
Post by: Kuubs on November 23, 2017, 14:16:47 PM
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.
Title: Re: Get Virtuemart products by category name or id
Post by: 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));
}
Title: Re: Get Virtuemart products by category name or id
Post by: Kuubs on November 23, 2017, 18:07:23 PM
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.
Title: Re: Get Virtuemart products by category name or id
Post by: Kuubs on November 24, 2017, 10:16:11 AM
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.
Title: Re: Get Virtuemart products by category name or id
Post by: Dimi on March 09, 2021, 13:13:20 PM
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)?
Title: Re: Get Virtuemart products by category name or id
Post by: pinochico on March 09, 2021, 14:27:59 PM
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.
Title: Re: Get Virtuemart products by category name or id
Post by: Dimi on March 09, 2021, 15:59:12 PM
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?
Title: Re: Get Virtuemart products by category name or id
Post by: pinochico on March 09, 2021, 16:02:12 PM
Thanks for your info and I hope someone will advise you better than I do