How to list products of a category in prestashop - categories

I would like to list product of a category once i've selected a category in my left quicklaunch.
Does someone know how to do that ?
Thanks by advance :)

You can use the following function to get all the products of the category.
$id_category = Tools::getValue('id_category');
$products = Product::getProducts(Context::getContext()->language->id, 0, 0, 'date_upd', 'ASC', $id_category, true);
$products is an array of products belong to the category.
Note: id_category should be the default category of the product.

Related

Product count for anchored category in Magento2

I am using the Megamenu third-party module in Magento 2 and I have customized this module due to some custom requirements.
I need to count product same as display in the admin category section for the anchored category and using the below code
$category->getProductCollection()->count()
this code is returning 0 products while this category is anchored and its subcategory has some product so it should count it subcategories product same as display in the admin section.
Please advise what will the code to get the products.
Thanks,
Please make sure you have successfully reindexed after setting the anchor via php bin/magento indexer/reindex.
Then try following code:
Include product collection factory to your construct
public function __construct(
// ...
\Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection,
// ...
) {
// ...
$this->productCollection = $productCollection;
// ...
}
And count the product collection filtered by your category.
$products = $this->productCollection->create();
$products->addCategoryFilter($category);
$products->count();

Eloquent select items with several where clause on same relationship column

I have a User model with a hasMany relations to model Cart.
Cart model has (among others) user_id and campaign_id.
I want to make a request which will grab all the users who have a cart with a specific campaign_id and also at least one of a list of campaign ids.
I came up with this request
$alistUsers = User::whereHas('cart', function($query) use($campaignId, $campaignIds){
$query->whereIn('campaign_id', $campaignIds)
->where('campaign_id', '=', $campaignId)
;
})
->get()
;
which obviously returns 0 results since a cart item can't have several campaign_id.
I probably need to do something with sub selects but I can't find the correct answer.
If someone has an idea I'm all ears.
Thanks.
I finally ended up with 2 different queries
$userIds = User::whereHas('cart', function($query) use($campaignId, $campaignIds){
$query->where('campaign_id', '=', $campaignId);
})
->pluck('uuid')
->toArray()
;
$iKnowUsers = User::whereHas('cart', function($query) use($userIds, $campaignIds){
$query->whereIn('campaign_id', $campaignIds)
->whereIn('user_id', $userIds );
})
->get()
->count();
But I'm not really happy with that so if someone has a cleaner answer I'll be interested to see it :)

how to show list of CustomerId in dropdown list

i want to create Customer diary on the base of customerID that present in customer table.
In customerID field i want dropdown list having list of CustomerId's that are present in database
How can i do that ? can someone help me please
var _objAllCustomerIds = null;
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
_objAllCustomerIds = context.MyCustomers.Select(customer => customer.Id).ToList();
}
//where AdventureWorksEntities is your DBcontent containing all your entities
//MyCustomers is the entities representation of your customer table
if this is in your .aspx file
then in your .aspx.cs file (assuming you're using code behind) you'd get the list of customer IDs and then bind that list to this dropdown.
Does this answer from another question help?
How to bind the selected value of a DropDownList

magento rest join stockitems with products

I use Mgento rest to get Stock items and Products. I want to join results (i want to get product that contains sku & qty). Which key i must to use to join?
<item_id>576</item_id> = <entity_id>576</entity_id>
or
<product_id>576</product_id> = <entity_id>576</entity_id>
?
stockitem:
<data_item>
<item_id>576</item_id>
<product_id>576</product_id>
<stock_id>1</stock_id>
<qty>100500.0000</qty>
...
</data_item>
product:
<data_item>
<entity_id>576</entity_id>
<attribute_set_id>4</attribute_set_id>
<type_id>simple</type_id>
<sku>501cap00001</sku>
...
</data_item>
Answer is too late
stockItem:product_id = product:entity_id

Display Latest Magento products without setting date

I use Magento 1.7.0.2.
I'd like to show the 8 latest added products in my homepage, but without setting a date "from" and "to". I need it to be automatically.
Does anyone know of a solution?
Product IDs are incremental. By ordering them descending and limiting the collection to 8 you will have 8 last products.
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->getSelect()->order('entity_id desc')->limit(8);
/* just for testing
Mage::log($collection->getSelect()->assemble());
foreach ($collection as $product) {
Mage::log($product->getSku());
} */
With the collection you can do whatever you need, add visibility and status filter etc.
in order to do that you will need to use the date that the order was created. The key to display all products information as price, name, and etc. is to us ->addAttributeToSelect('*') Here is the script:
$store_id = Mage::app()->getStore()->getId();
$_products = Mage::getResourceModel('reports/product_collection')
->addStoreFilter($store_id)
->addAttributeToFilter('visibility', 4)
->addAttributeToFilter('status', 1)
->addAttributeToSelect('*')
->setVisibility(array(2,3,4))
->setOrder('created_at', 'desc')
->setPage(1, 9);