Magento: How to sort product collection? - magento

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

Related

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.

Sort topmenu in Magento?

This is really driving me crazy. I have been trying for hours to get it right, but no luck!
All i want is to be able to sort the parent & subcategories in the topmenu of Magento. I tried a lot of custom coding, but all what Ive found seems to be out of date. Then i also tried to just move the categories order in the admin panel of Magento. This seems to work, but only for parent categories. It won't sort the subcategories. In addition, for some strange reason it will put the old order back again. So for instance, when i sorted the parent categories, it seems to work but when i refresh the admin page the old sorted categories are back and when i refresh the frontpage i see (indeed) the old (wrong) sorted categories back. Might have something to do with the cache ? It should be so simple (i hope:)). Is there anyone who can tell me how i can sort the categories and subcategories (custom, or by id, or anything else...)?
Thanks.
I feel these urls will help you in this case
http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/catalog/getting_and_using_categories_and_subcategories
How to sort a category list array alphabetically in Magento

Adding multiple items to special page

I have a quick how to question with Magento.
On my main site I need to have 7 links to special "categories" of items, but these don;t reflect categories of items. It is for a clothes shop so link may be to a summer wear page to only display items tagged as summer wear but from all types of item and categories.
What would the best way to go about creating this functionality?
I am assuming that when you say 'taged' you mean the product has a attribute set to 'summer'. In that case:
If you have products that you want to group together by attribute and list similar to a category page without being in a category, you could try using the Fishpig Splash Page extension:
http://fishpig.co.uk/attribute-splash-pages.html
This extensions allows product attributes to group products instead of categories.
I hope I understood your question correctly.
You can create categories for each of the 7 pages you mention, but hide them from the navigation in the category settings page.
you can then add your products to these categories as you see fit.
They will still be in their original categories too, but you can now link to the new pages and see them there too.
It seems like to me, you would just create the categories like normal. Because products can belong to many categories, you would just assign on a per-product basis which ones need to be in your categories.

Changing the new product widget sort order in Magento

I'm using Magento for my webshop and my homepage uses a new products widget. I want to change the sort order for the products within the widget.
I searched for hours but I can't figure it out.
The block for displaying new product is:
app/code/core/Mage/Catalog/Block/Product/New.php
see the function _beforeToHtml()
default sorting is done by start date descending:
->addAttributeToSort('news_from_date', 'desc')
You can copy this block to code/local and replace it with position or the sort order you want... or extend it with a Magento extension.
You might want to search Magento Connect to see if there is any module that would fit your needs.

Magento category order on frontend

I want to change the default order of categories in Magento frontend navigation to order by name. Running Magento 1.5.
Does anyone have a clue?
You can manually order the categories in admin by dragging them up and down in the left pane of "Manage Categories" page.
We have been searching on a way to do this also, and ended up doing this:
<?php
...
$categories = array(1,3,5); // this holds the category ids you want to show, in the correct order
foreach($categories as $cat){
Mage::getModel("catazlog/category")->load($cat);
// Do whatever you want here
}
?>
It definitely isn't the cleanest way to achieve what you are trying to do, but just offering you a quick (dirty?) solution.
-Kenny
You would need to build the collection first, then sort the attribute
Mage::getModel('catalog/category')->getCollection()->addFieldToFilter('parent_id', '319')->addAttributeToSort('name', 'ASC');
In Admin Panel go to Catalog—>Manage Categories.
From there you can change categories order by just drag-and-dropping categories right in category tree at the left-hand side of the page.
Good luck!!!

Resources