I want to get the product collection in magento.For that i use some code but i think this code is not what i need.I want to get collection on attribute base.I got some products but it didn't match to those products that are filters from advance results for that attribute.Means different results from my collection and advance search results.Also the product url is not valid one.May be someone know where is the problem ? thanks in advance.My code is :
<?php $collection = Mage::getModel('catalog/product')
->getCollection()->addAttributeToSelect('*')
->addFieldToFilter(array(
array('attribute'=>'manufacturer','eq'=>'23'),
));
foreach ($collection as $product) {
?>
<div class="brand_name">
<p>Audi</p>
<?php echo substr($product->getName(),0,10);?>
</div>
<?php } ?>
You have used 2 array in the field to filter. Try with one.
<?php $collection = Mage::getModel('catalog/product')
->getCollection()->addAttributeToSelect('*')
->addFieldToFilter('attribute'=>'manufacturer','eq'=>'23');
It should work correctly:
$attrToSelect = '*'; // or Mage::getSingleton('catalog/config')->getProductAttributes();
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect($attrToSelect)
->addAttributeToFilter('manufacturer', 23)
;
foreach ($collection as $product) {
echo $product->getProductUrl();
}
Also check in admin - Catalog->Attributes->Manage Attributes - Used in Product Listing set Yes.
please use addAttributeToFilter('manufucture',23)
instead of
adfieldtofilter
Related
I am running following code in Magento root to get the product urls
<?php
require_once('app/Mage.php');
umask(0);
Mage::app(1);
$collection = Mage::getModel('catalog/product')
->setStoreId(1)
->getCollection();
foreach( $collection as $product )
{
echo $product->getProductUrl();
echo "<br>";
}
?>
I am getting product urls like http://example.com/catalog/product/view/id/5/ , But these urls are invalid.
The product urls are as following in front end http://example.com/product.html
How do I get the correct product urls? I have multi store Magento set-up.
You need to get store url for each product separetly. In other words, you need to use something like this:
$collection = Mage::getModel('catalog/product')
->getCollection();
foreach( $collection as $product )
{
echo $product->setStoreId(5)->getProductUrl();
echo "<br>";
}
i am trying to get sub-categories from category id.
but looking for more optimized code as the database vast records and cannot afford to load whole category.
i have tried below codes they are working on localhost but as my server has huge data it doesn't work there
$cat = Mage::getModel('catalog/category')->load(13);
$subcats = $cat->getChildrenCategories();
//AND
$categories = Mage::getModel('catalog/category')->getCategories($cat_id);
please help
Hello check below code may be help you
<?php
$root = Mage::getModel('catalog/category')->load(13);
$subCat = explode(',',$root->getChildren());
$collection = $root
->getCollection()
->addAttributeToSelect("*")
->addFieldToFilter("entity_id", array("in", $subCat) );
foreach($collection as $catname){
echo $catname->getName();
}
?>
<?php foreach($this->getItems() as $_item): ?>
<?php echo $this->getItemHtml($_item) ?>
<?php endforeach ?>
the code is form cart.phtml if i want to add a product into $this->getItems() which from a specified category then output it? how do i do? thank you
i should know what information of the product, then i can put it into $this->getItems() then loop out the information?
The way to accomplish this would be to add the item to your cart using either method below.
Adding a Product to the Cart via Querystring read more
/path/to/app/checkout/cart/add?product=[id]&qty=[qty]
Add product to cart programmatically (read more)
`
$params = array(
'product' => 23,
'qty' => 2,
);
$cart = Mage::getSingleton('checkout/cart');
$product = Mage::getModel('catalog/product');
$product->load(23); // load product id 23
$cart->addProduct($product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
`
I want to retrieve the parent category only in current store of magento. I googled and get the result for all parent category with subcategories included. but want only parent top categories single depth only.
if(strlen(trim($primary_category_temp)) < 1)
{
$_categories = Mage::helper('catalog/category')->getStoreCategories();
if (count($_categories) > 0):
foreach($_categories as $_category):
$primary_category[] = $_category->getId();
endforeach;
endif;
}
$category = Mage::getModel('catalog/category')->getCollection()->addFieldToFilter('is_active',array('eq' => 1))->load();
from this i get whole category collection
foreach($category as $cat)
{
if($cat->getData('level')==2)
{
echo 'my code';
}
}
May be you need use:
Mage::app()->getStore($store)->getRootCategoryId()
or
Mage::app()->getStore()->getRootCategoryId()
for default store
Try below code
$categories=Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('level',2)
->addIsActiveFilter();
This will only retrieve parent top categories
I need a query which displays Category Name,Category url & value of the Custom catgory attribute in Magento
any Guess...
Thanks
$category = Mage::getModel('catalog/category')->load($my_category_id);
echo $category->getName();
echo $category->getUrlKey();
Something like this? I am not sure what you mean by custom category attribute and I don't know what a category value is though.
$categories = $_product->getCategoryIds();
foreach($categories as $k => $_category_id):
$_category = Mage::getModel('catalog/category')->load($_category_id)
echo $_category->getUrl();
echo $_category->getName();
echo $category->getUrlKey();
endforeach;
lets say your custom attribute is "custom_attribute", then do this:
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToFilter('custom_attribute', 'value_here');
foreach ($categories as $category){
echo $category->getUrl();
echo $category->getName();
}
Hope this helps.