Magento: Difference between loading product through collection than product model - magento

So we were trying to load a product through a collection with certain criteria, we didn't have the sku or the id so when we did the following
$prodModel->getCollection()
->addAttributeToFilter('visibility', $visibility)
->addAttributeToSelect('*')
->addCategoryFilter($cat)
->addAttributeToFilter('attribute_1', $sattribute_1)
->addAttributeToFilter('attribute_2', $attribute_2)
->addAttributeToFilter('type_id', 'configurable')
->load()
->getFirstItem()
When doing this we got the product we wanted but for some reason it didn't have all of the attributes, even though we specified "*" for all attributes. Specifically the one we were not getting was the media gallery attribute. What we ended up doing is saying getFirstItem()->getId() then loaded the product and it worked find.
I just don't understand whey loading the product with the catalog product model would have more attributes.

I understand you actually asked how to get all of the attributes but I notice you also mention the media gallery attribute specifically.
There happens to be a shortcut for getting the product image's final URL.
(string)Mage::helper('catalog/image')->init($product, 'media_gallery');
Casting to a string calls __toString which performs the cleverness. You can also apply resizing, rotation, watermarks, etc. immediately after initialising.

Related

Get list of custom brands extension attribute for current category Magento

I'm using a Magento extension called BELVG brands, which creates a custom brand attribute and gives each of my brands a link to it's own brand page as well as an overall brand page ( where I got this code from )
On my parent category pages I'm trying to show a list of brands just for that particular category, but because the Brand attributes data is stored in the extensions own brands table I'm having trouble with the usual getCollection methods.
I can pull out a list of all brands no problem and my code below does this, you'll see where I'm also getting the id of the category which is not really doing anything as yet because I am stumped as how to integrate this into my current code so I get what I want.
I've tried lot's of variations but I can't get anything to work, and every other way of listing attributes uses the catalog/category collection and filtering which doesn't help me here.
Can someone point me in the right direction?
I hope I'm being clear here, I don't use stack overflow very often.
Thanks
( Magento Community 1.8 )
<?php
$catId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); // Get current category id
$collection = Mage::getModel('brands/brands')->getCollection(); //get collection from brands
$collection->addAttributeToSelect(array('title', 'description', 'url_key', 'image'))
->addAttributeToFilter('active', 1)
->setStore(Mage::app()->getStore())
->setOrder('title', 'ASC')

Use qty in filtered navigation [magento EE]

The use case I am working on is such that all products are marked is_in_stock=1 because the items can be purchased regardless of whether they are physically on hand or not. However I need to display only what has a qty value greater than zero. If the user decides they only want to see whats on hand.
I figured out how to select the correct collection by joining the cataloginventory/stock_item table with current collection and filtering for quantity. However I am little in the dark on how to apply this on the frontend and what kinds of side effects this might have since 'qty' is never passed through to solr.
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->joinField('qty',
'cataloginventory/stock_item',
'qty',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left')
->addAttributeToFilter('qty', array("gt" => 0));
If anyone with experience working with EE layered nav has any input on a better approach to take, advice would be appreciated, thanks.
Unable to post a comment at this time.
Trying to understand your problem statement. Here is what I understand from your statement.
Products can be purchased when they are In Stock and when they are Out Of Stock
Currently, you DO NOT want to display Out Of Stock products in Category and Search Pages. You want to display ONLY In Stock products.
Let me know if my understanding is correct.
If the above is the case, this can be easily achieved from the ADMIN panel.
Navigate to System > Configuration > Catalog > Inventory > Stock Options > Display out of stock products > Yes / No
Set this to Yes, to make the products visible across Category and Search (MySQL/SOLR) pages.
Set this to No, to make the products invisible across Category and Search Pages. However, in this case, Product URL will still work fine.
When using Filtered Navigation, the condition for STOCK is not checked. It checks STATUS and VISIBILITY
The above code block written is fine from performance perspective. Does this mean you are going to use QTY for filtering purpose?
If that's the case and if you are using MySQL/SOLR for Search, the product collection needs to be modified using the code block mentioned above.
This does not impact in a negative way if used as below:
After the final collection is formed using all the necessary attribute filters - default magento, then add your code block.
**Do not instantiate the collection using the code mentioned above.
$collection->joinField('qty',
'cataloginventory/stock_item',
'qty',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left')
->addAttributeToFilter('qty', array("gt" => 0));
I am not really sure if I was able to answer your question. However, these are few basic points about Filtered Navigation, SOLR/MySQL Search, Category and Search Pages w.r.t In Stock/Out of Stock products.

Is it possible to filter related products based on some of it's attributes on the product view page?

I am new to magento and looking for a solution. Is it possible to filter related products based on some of it's attributes on the product view page? I have a product and there are 100s of related products associated with it. I want customers to filter these related products based on their attributes on the product view page itself. Is it possible? Please help me.
Regards, Pallab
The template (Magento 1.9.1.0 version) for this is located in: frontend/your_package/your_theme/template/catalog/product/list/related.phtml template and the Block class associated with this template is: Mage/Catalog/Block/Product/List/Related.php and the method is: protected function _prepareData() where the related products collection is being fetched. You will have to setup your filters as you need, most likely by using JavaScript and finding the products that match the selected (filtered) attributes by the customer.
For more thorough answer, I will need more information about how these filters should look like and work.

magento - product model and product_collection items have different properties in flat catalog mode

i just know that if i have a product_collection (for example the collection generated in catalog pages)
//event catalog_product_collecion_after_load
$productCollection = $observer->getEvent()->getCollection();
if i take an item from this collection and i compare it with the relative model
$_product = Mage::getModel('catalog/product')->load($item->getEntityId());
this 2 instace of the same entity have different properties!
I'm working in flat catalog mode.
Why the collection's items are not the same of product models?
I would to know if this is the right behaviour and if is it how to have same properties in both object!
sorry, but magento is very dark :(
Because Mage::getModel('catalog/product')->load($item->getEntityId()); loads all attributes for the product and the collection loads only specified attributes different from situation. You may find (CTRL+F) at app/code/core/Mage/Catalog/etc/config.xml something like attributes then you will see the list of all default loaded attributes for product collection. Also you able to change them in your module or directly in Catalog config.xml. But it's not the best idea to change something at app/code/core/Mage/Catalog/etc/config.xml except for debug
For the catalog_product_collection with flat_mode is more complicated add an attribute to the item's collection.
The attributes that are in product items are the join between catalog_product_flat table and the EAV attribute for product entity.
So, in product model from:
collection we have the attributes joined between catalog_product_flat table and the EAV attribute
getModel('catalog/product') we have all EAV attributes
Over these attributes we will certainly have other attributes, i think added in other point.
Now, which attributes are in catalog_product_flat?
Simple are the attributes you checked as "Used in Product Listing" in magento manage attributes! :)
But in some attributes you can't change this option, depends by kind of type you have select. Atribute type Image hasn't "Used in Product Listing" flag, so you have to modify catalog_eav_attribute if you want the new image in product listing.
Well, i spent 2 days to know this, i hope it will useful for other unlucky magento developer. :)
Magento version 1.5

Filter one specific attribute values from products in magento

I'm new in magento, I'm working on http://www.theartworkgallery24.com/stage/ website. Please check beneath flash banner, there are 4 big images that are links to attributes value from which user can directly go to a product.
I need a page that display on one specific attribute values like in case of artist, it should be Gustav Klimt, Leonardo Da Vinci etc. These should be links to actual product related to the attribute values.
Please tell me how to make your own PHTML or PHP file, which code to use and how, and in last how to call your code in your magento website.
Any ideas will be appreciated, thanks.
You please create an attribute (davinchi) in admin side for these functionality. You can create products attributes from catalog->attributes->menu.After that you can see these attribute value in product creation page.You just assign it to different products
After that you can either create a module for this. Otherwise just create a phtml file in catalog/product/ folder.
then enter the below custom collection code in it.
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('*');
//filter codition
$collection->addFieldToFilter(array(
array('attribute'=>'davinchi','eq'=>1),
));
This will load products having attribute value davinchi=1.

Resources