Magento 1.5 sort categories by name in head-navigation - magento

I try to order the categories and child-categories in the head-navigation by name.
I found at least 3 differnet ways to do so, but none of them seems to work.
add "->setOrder('name', Varien_Db_Select::SQL_ASC)" to Category.php
add "->setOrder('name', 'asc')" to Category.php
add "->addAttributeToSort('name', Varien_Db_Select::SQL_ASC)" to Category.php
add "->addAttributeToSort('name', 'asc')" to Category.php
sort with an SQL Query, may work, but its not a real option in my case
and some others not worth to notice...
For the layered-navigation i figured out to sort the array, but can't do so in the head-navigation.
I'm grateful for all kind of hints...
THX!

That's because collection is loaded using ->load() - after that you can't change it. Best way to sort it is by sorting it in admin with drag and drop, or if that's not an option, you have to hook on catalog collection event _load_before in
app/code/core/Mage/Catalog/Model/Resource/Category/Flat/Collection.php
(if you're using flat catalog),
but that's not a proper way since it will impact display of any category listing and render your admin sorting unusable. Take your time and sort it properly in admin.

Related

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

Category coming at inappropriate place

I worked on this and it is live now. Please have a look at it. The problem is that the category named "Carbide rod" is not assigned to that "PCD inserts" category. But still showing. I don't know whats the problem in it. Please help me on it.
You probably have enabled flat tables to be used on front, which is generally a good idea. The thing to remember, though, is to perform a Category Flat Data re-index in Admin Panel. Go to System > Index Management and do a reindex of Category Flat Data.
Next you should Flush Cache Storage at System > Cache Management.
If that won't help, then check how your category tree looks on a Store View level. Magento allows to have different category trees for each store view.
Carbide rods category is not displayed under PCD inserts, whats your question exactly I'm not getting it.

Is this possible to get categories sorted by RAND()?

I want to display product categories in random order.
Is that possible?
How can I achieve this?
I'm using custom theme, and categories are fetch by:
$_categories = $this->getStoreCategories();
Maybe it will be easier to use php shuffle function?
After you have got the collection of the categories, do you want to try using this
$arrCategories->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit('5');
I have used this on a different collection of items, so I am not sure if this can work on Categories. But I think its worth trying..It is just writing a select query with Order by RAND().
Hope it works.
Cheers,
Swapna

Can I Make A Product Collection Use Flat Tables in Magento

In my Magento store I currently have Flat Catalog Product turned Off because it breaks some custom code I have on my site.
However, I am have a single instance where it would greatly help me to have the collection use the flat table as opposed to the EAV tables.
Is there a way I can tell the product collection to use the flat tables as opposed to the EAV tables in my code?
This is a great opportunity to fix your custom code :) In the default system, flat catalog is either turned on or off, not with any level of granularity. Depending on the nature of the hack that breaks the flat catalog, it may be possible to avoid that issue and just it turned on globally.
Perhaps more detail into the issue that caused you to leave it turned off?

Magento: Add a "fake" product to cart/quote

I understand how to programmatically create a product and also add to cart. I know this might sound dumb but is it is possible to generate a product on the fly and add that to the cart/quote but never actually save it in the database.
We want to create a made to order interface and I was thinking at the end it could add a bundle product with all the selections but that bundle product wouldn't actually exist in the backend.
I figured as long as you can make sure the quote and order has what it needs in terms of the product it would be ok, but obviously there is probably a lot that is tied to looking up stuff in the db on a specific sku or ID. I know that if you delete a product and then look at an order in the admin that causes issues, at least it did for this one scenario I was dealing with.
I was thinking of creating a giant bundle product that had like 6 different bundle items and each item could potentially have like 500 products and then based on what the user selects I programmatically add the bundle to cart. But then I wasn't sure if there would be a negative affect with having a gigantic bundle product like that as well.
UPDATE:
I don't think this will work, obviously there are a lot of information tied to the product in the database and we setup a test and right away we get an error for $item->getProduct(). We are moving forward with creating a giant bundle product and also the generic product with adding custom options on the fly, which Anda pointed out below. Any other suggestions will be greatly appreciated.
I'm not sure that clockworkgeek's approach is going to work. On every page load, Magento loads the items from the cart to make sure that they are still valid (in-stock, prices correct, etc), and amends the cart to reflect those values. My understanding of the system in the past has been that a product in the cart needs to have a corresponding database value to survive this process.
The "giant bundle product" approach is a pain, but in the past has been the best approach I have found. Attempting to change the values of the product (such as price or attributes) will be overridden by the cart checks, so you need a product w/ maximal flexibility, such as an overly-customized bundle product or configurable product.
Hope that helps!
Thanks,
Joe
Why not create a generic product in db and then set the product customization as custom options (additional_options) on the fly depending on the user selection. You can add custom options to the product (actually to the quote item) without having to save them in the database. I did this once for a website that sells glasses with prescription. The prescription was added as an option.
You can programmatically create Mage_Sales_Model_Quote_Items and add them to the cart. You've noticed it needs a product to match it's product ID but it needn't be a useful one. It could be a blank, disabled product, also created in code. All that's needed is a stub.
The necessary stuff for the cart is stored in the quote item - fields like name, value and quantity. Those fields are then copied directly to the order without using a product.
Mage::getModel('catalog/product')
creates a new product. you can add it to a cart, by doing something like this:
$cart = Mage::getSingleton('checkout/cart');
$product = Mage::getModel('catalog/product')
->setStoreId($storeid)
->setTypeId($type_id)
->setQty($quantyty)
->setWhatAttributYouWant($attribute);
$cart->addProduct($product);
product attributes you can find in the DB in tables that start like catalog_product_... or take an already created product, and see what attributes it has in the _data array (with debugger or just print_r($product->getData))

Resources