Use qty in filtered navigation [magento EE] - magento

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.

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')

View all products within category lead to blank page

I'm on Magento 1.7.0.2. When I view all products within category, I got a blank page after loading for while and sometimes I got Internal Server Error.
Please advise
If it works for only a few products, most probably you are running out of memory. Increase it. The recommended value is 512M, but the more you have the better.
if you used product collection & addAttributeToSelect('*') on it.
or
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
then dont use any one
select the specific attribute which you need.
for example
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect(array('name', 'url_key','price','lowest_price','service_id','description','short_description'));
because loading all attributes of product affects on big database.
and its also affect on site performance
hope this help you

Magento: How to sort product collection?

i have created a list_home.phtml that i call from {{block type="catalog/product_list" category_id="6" template="catalog/product/list_home.phtml"}}The category is very important because i will have 3 tabs in this homepage slider.This is a version of the product list without toolbar to include in a homepage slider, where the user should never be able to change the sorting.
However in the main catalog there is the toolbar. Currently, if the user changes the sorting there and then goes back to the homepage the sort order on the home page will also be affected!
How can i force the $collection to always sort by newest and also filter the category from the frontend's block call?
I am really almost stuck after opening around 50 tabs in the broswer and trying to research, really need some help here please.
Thanks in advance.
On the product collection you can set the order by calling ->setOrder on it.
I'm not sure of the field you would need to order by, but if you wanted to set it by price ascending you would do
->setOrder('price', 'ASC');
To filter the category you need to do this
$productCollection = Mage::getModel('catalog/category')->load($categoryId);
You can then set the sort order

Magento - Not able to display tier pricing in product view

I have a different price set for products based on customer group. I want to show both price's to the customer in case both of them apply in product list, view, related and upsell products.
I turned on template path hints to verify that prices for all views are being rendered from the same template file, which is as follows:
/app/design/frontend/default/my_theme/template/catalog/product
I can see tier pricing correctly in product list, related and upsell products, but NOT for product view.
After debugging for a while I have narrowed down the problematic part of catalog/product/price.phtml file as follows:
<?php
$_coreHelper = $this->helper('core');
$_weeeHelper = $this->helper('weee');
$_taxHelper = $this->helper('tax');
$_product = $this->getProduct();
$_id = $_product->getId();
echo 'Product Id: ' . $_id;
$_weeeSeparator = '';
$_simplePricesTax = ($_taxHelper->displayPriceIncludingTax() || $_taxHelper->displayBothPrices());
echo 'Simple Price Tax: ' . $_simplePricesTax;
$_minimalPriceValue = $_product->getMinimalPrice();
echo 'Minimal Price Value: ' . $_minimalPriceValue;
//$_minimalPriceValue = 41;
$_minimalPrice = $_taxHelper->getPrice($_product, $_minimalPriceValue, $_simplePricesTax);
echo 'Minimal Price: ' . $_minimalPrice;
//$_minimalPrice = 41;
?>
I have echoed all prices fetched from models above, and only in case of product view page the $_product->getMinimalPrice() above does not return anything, while it appears correctly on list, related and upsell products.
I cannot think of any reason for this. There are a few lines different in catalog.xml but I don't think they have anything to do with this. Besides, there are a couple of commented lines in the above code, where I have hard-coded the minimalPrice and minimalPriceValue variables. After doing that the price starts appearing in product view also. The Product id for all views including product view is also appearing correctly, so the product IS loaded at that time.
We are using a custom template, and I see that in default we are not having this problem. I am using Magento 1.4.1.1
Did you say that what you are trying to do works 100% with a stock theme? If so then you really should look at the differences between your custom theme and the default. Also, you might want to look at any changes the developer made in app/code/community and app/code/local that are customizations for the theme. There could be some conflict.
But if you can't find a difference maybe I can give a few hints as to why you might be seeing this behavior. Sometimes the same model (and block) objects have different data in them when you are viewing on the category list page vs the product view page. The reason is that backend queries to the database are different. I did some work with the tiering system before and I remember that when you are looking at the catalog page, the pricing data actually comes from some catalogindex_* tables rather than the catalog_product_entity_* tables. If I remember correctly, there are two tables that it queries, something like catalogindex_price and catalogindex_minimal_price. But then when you are at the product view page the pricing data comes from the standard catalog_product_entity_* and catalog_product_entity_tier_price tables. Anyway, that probably doesn't solve your problem, but it might get you pointed in the right direction. Good luck.

Magento: Difference between loading product through collection than product model

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.

Resources