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

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

Related

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.5 sort categories by name in head-navigation

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.

magento - differences with collections

I'm starting to gain a little experience with magento but collections are are thrown up some inconsistencies.
Some collections use addAttributeToSelect and addAttributeToFilter whereas others use addFilter and addSelect.
Why is this and how do you tell straight off which collection uses which?
How the hell do you join collections. I've tried every single example out there which never works. Specifically I would like to join shipping, invoice and order collections
As far as I know there are no addFilter and addSelect functions in Magneto. What you might refer to is differences between addFieldToFilter and addAttributeToFilter.
The difference between those two is that there is a specific data type in Magento called EAV. You can read more about it anywhere else. Products and their attributes, customers and sales are EAV in Magneto. The quick background under EAV is that the data is kept in a different tables that's why functions to filter a collection is absolutely different.
This brings us to your second questions. Since there is not single table for products, customers or orders in Magento you can't apply normal SQL join to it. However it's quite unlikely that you will need it often. For all other non-EAV cases you can use the following syntax:
$collection = Mage::getModel('group/model')->getCollection();
$collection->getSelect()->join(array(
'your_alias' => Mage::getSingleton('core/resource')->getTableName('group2/model2')),
'main_table.your_id = your_alias.your_id',
array());
Please let me know if I can make it more clear.

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