Change Magento collection to show only grouped products - magento

I'm trying to modify Magento (1.7.0.2) collection to show only grouped products in the product list. I've tried to do this with addAttributeToSelect or addAttributeToFilter like this
$_productCollection=$this ->getLoadedProductCollection()
->addAttributeToSelect('*')->addAttributeToFilter('type_id','grouped');
or
$_productCollection=$this ->getLoadedProductCollection()
->addAttributeToFilter('type_id', array('eq' => 'grouped'));
but it's not working now with this version of Magento.
Anybody knows how can I get it working?

Filters not being applied often happens when the production is already loaded.
Try doing a $_productCollection->clear() before you iterate across the collection. Preferably though, find a place to update the collection before it is loaded.

Related

Magento: Deleting archived products - Will the products inside orders be affected?

I have a lot of archived products that I would like to delete.
But I am using a function in the frontend where users can see their old orders and what products were bought:
$orders = Mage::getResourceModel('sales/order_grid_collection')
->addFieldToFilter('store_id', $storeId)
->load();
Some of the products in the orders are already archived.
When I delete all archived products, can the products inside the orders still be accessed? Or will I lose those products?
As answered by Marius:
"Normally, the orders don't have only references to the order products, but they also keep product values (that might seam redundant) because you want to see a snapshot of the product you ordered at the time you ordered it.
This way you avoid seeing new prices or descriptions.
The order history section that magento offers by default works even if you delete ordered products.
but if you have a custom code that loads a product collection or a product to get additional info, it will be affected if you delete the products.
If the only code you use is the one you shown in the question you should be save.
I suggest trying to delete the products first on a staging server then do it on live.
and backup before doing anything."
(If you want to upvote, please consider upvoting his answer too)

How to determine if a simple product is part of a configurable product?

Im currently writing a export tool for Mage1 in order to export products from Mage1 to Mage2.
I thereby want to determine if a product in the collection is part of a configurable product.
My current collection is set up this way:
$_productCollection = Mage::getModel('catalog/product')
->getCollection()
->setPageSize(500)
->setCurPage(1)
->addAttributeToSort('sku', 'ASC')
->addAttributeToFilter('type_id', array('eq' => 'simple'))
->addAttributeToSelect('*')
->load();
First, I request only simple products right now. If I delete this line, I get every products in the shop in the collection, but In the code every product is handled like a simple product currently.
Now, following is happening:
I request the script and get my information for all simple products. In the store I use to test there are configurable products AND bundle products. I know want to know how I could determine if a product is part of a configurable or bundle article and how I could assign those "simple" products to the respective configurable products?
I generate a CSV which I can upload in Mage2 successfully, but as I already said, it only imports simple products (as desired so far).
I googled a little (https://www.google.de/search?q=get+assigned+simple+products+for+a+configurable+product&ie=utf-8&oe=utf-8&gws_rd=cr&ei=b3i8VuD6BIevswGWtbGACw) but I'm still clueless. :/ Hopefully someone here can help me with this matter.
Also, I checked this thread: Checking if a Magento product is a child of a configurable product - but I don't get what to do with the respective name. Maybe this is already the right guess?
Thanks, Max
Yes there is a way of doing so. Have a look at the model class Mage_Catalog_Model_Product_Type_Configurable, there is a function called getParentIdsByChild.
Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($productId);
It will return an empty array if no parents id's have been found.

How to get product collection of given main category id?

In my website I have several stores available. One particular store contains both the simple and configurable products. This is a huge collection of products. What is the fastest way to get all enabled products (including child products of configurable products) collection of this store by store id?
Note: I tried this lot of different ways. But it takes loo much time. Sometimes cause to crash the server even.
Note : I'm using Magento CE 1.3
Any suggestions will be appreciated.
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect(array('name', 'url_key','price','lowest_price','service_id','description','short_description'));
$collection->addStoreFilter();
instead of use addAttributeToSelect('*') use only specific attributes which you needed because it effect performance of site
hope this will help you

Displaying single product through list.phtml edit

So what i'm trying to achieve is that the product with the attribute 'promotion' set to 'yes' is displayed on the frontpage of the website. This is working, but the .phtml file i'am using with this is the regular list.phtml. This is currently showing all the items I have set to promotion but I only want to show 1.
So in short: How do I edit the list.phtml to only show 1 product instead of everything?
Change how you are pulling your collection. Clone/Rename your list.phtml, for example promotion.phtml. Then change this line, from this:
$_productCollection=$this->getLoadedProductCollection();
To this:
$_productCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('promotion', 1)
->addAttributeToSort('updated_at', 'DESC')
->clear()->setPageSize(1)->load();
And it should load only one item with promotion set to yes. Make sure you set the new template in either your CMS Page Content or XML, depending on which method this is added.
Explanation
Mage::getModel('catalog/product')->getCollection(): Gets the Product Collection. You can get other collections by changing the model, such as "catalog/category" and "cms/page".
->addAttributeToSelect('*'): Adds All Product Columns. This can be exchanged for things like ('name', 'url'). I'm assuming it's faster than loading all but I haven't benchmarked it. Since you're using the full template it's probably best to leave this set to all.
->addFieldToFilter('promotion', 1): Filters out products by attributes. Here we have the products filtered for all those with the 'promotion' attribute set to 1(yes/true). Products use this one, while categories use ->addAttributeToFilter() oddly enough. Definitely give Alan Storm's Collection Explanation(link below) a read through to know what all you can do with this one. You can add multiple filters to your collection, either by adding another ->addFieldToFilter(), or storing your filters in nested arrays.
->addAttributeToSort('updated_at', 'DESC'): Sorts the product collection by specific attribute and direction. Here I have the "updated_at" date set to descending order, "ASC" is ascending. You can add multiple sorting attributes, pay attention to the order you add them in, of course.
->clear()->setPageSize(1)->load(): These three are needed to make adjustments to how much the collection pulls. ->clear() must be called before it will allow changing how many products are pulled. The ->setPageSize() bit is where you specify how many products you would like to return, and ->load() of course loads the collection. Note that if you do not limit the size of the collection returned, you do not need this entire line, the products will iterate without having to call ->load().
Resources
Alan Storm said it best, give this a read and you should be a pro at manipulating collections: http://alanstorm.com/magento_collections

Magento 1.4.0.1 create and populate new attribute value for almost all products

I am trying to figure out how I might be able to do this - possibly in the mysql database, but I'm not sure of all the tables involved, so daren't go much further?
I have added an attribute called "condition" to the defaul attribute set.
I need to populate that attribute value for every product, but the admin system requires me to fill in many other attributes if I try to bulk update them using the admin form. This can't happen as "description" is different for every product, obviously.
So, can someone tell me how I might populate this attribute value with the value "new" for every product in my database?
Realistically, I can change this attribute value for the number of products that need a different value "used", but if the update could be filtered on SKU, I can make it work right first time. I think!
The programming approach is to save the following script in your Magento folder and execute it by visiting the address directly. It might be slow but that doesn't matter if it's only going to be run once and then deleted afterwards.
<?php
require 'app/Mage.php';
umask(0);
Mage::app();
$products = Mage::getResourceModel('catalog/product_collection');
foreach ($products as $product) {
// replace IS_NEW with a test perhaps using $product->getSku()
$product->setCondition(IS_NEW ? 'new' : 'used')
->save();
}
Not a programming answer so perhaps not suitable for Stack Overflow but there are a number of workarounds.
You could export all products to Excel, bulk update many rows at once then re-inport. Use Magmi if you have many products.
There are some extensions which let you update many attributes at once. This Mass Actions is expensive but gives you an idea what might be available if you search.
As much as I loathe desktop applications for web services there is Store Manager which specialises in bulk actions.

Resources