I've added a custom filed to the customer addresses, but I don't know how to query using this field.
I'm executing the following piece of code:
$collection = Mage::getModel('customer/address')
->getCollection()
->addAttributeToSelect('custom_field')
->addFieldToFilter('custom_field', '1');
echo var_dump($collection);
But I'm facing this error:
Fatal error: Call to a member function getBackend() on a non-object in
app\code\core\Mage\Eav\Model\Entity\Abstract.php on line 816
My question is: how can I query through this field?
Thanks.
After a few tests, I could solve the problem. Here's the code:
$collection = Mage::getResourceModel('customer/address_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('custom_field', 1)
->getItems();
echo print_r($collection, true);
Try using
Mage::getResourceModel('customer/address_collection')
Instead of
Mage::getModel('customer/address') ->getCollection()
See /app/code/core/Mage/Customer/Model/Customer.php
$collection = Mage::getResourceModel('customer/address_collection')
->addAttributeToSelect('custom_field')
->addAttributeToFilter('custom_field', '1')
->getItems();
Related
I've written a method that I'm calling from my list.phtml file.
The logic should simply output the number of products in the currently viewed category and the highest and lowest prices. Pretty straight forward really.
For reasons beyond my understanding it's always returning that the current category contains no products, and I can't figure out why but I presume it's because I'm not fetching the product collection correctly.
Here's the code for my method.
public function writeCatInfo(){
if(Mage::registry('current_category')){
$_productCollection=$this->getLoadedProductCollection;
$catname = Mage::registry('current_category')->getName();
$priceArray = array();
foreach ($_productCollection as $_product){
$priceArray[] = $_product->getSpecialPrice();
}
$numItems = count($_productCollection);
$highPrice = number_format(max($priceArray),2);
$lowPrice = number_format(min($priceArray),2);
$infostring = ucwords($catname)." contains ".$numItems." products with prices ranging from £".$lowPrice." to £".$highPrice;
echo $infostring;
}
}
this will help you to get current category product collection.
$category_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();
$category = Mage::getModel('catalog/category')->load($category_id);
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addCategoryFilter($category)
->setOrder('price', 'ASC')
->load();
Turns out my helper was extending the wrong class - Mage_Core_Helper_Abtract instead of Mage_Catalog_Block_Product_List. Changing this fixed the problem.
Please see the below code to get product collection by Category id
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*') // add all attributes - optional
->addAttributeToFilter('status', 1) // enabled
->addAttributeToFilter('visibility', 4) //visibility in catalog,search
->setOrder('price', 'ASC'); //sets the order by price
I have a custom block for my customer/dashboard page, and while everything works, I cannot use the following:
public function getOrders(){
$collection = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect('*')
->addFieldToFilter('customer_id', Mage::getSingleton('customer/session')->getCustomer()->getId())
->setOrder('created_at', 'desc')
;
return $collection;
}
I get a blank page if I access this method. I want to load orders and display them on my dashboard. I took this query from the sales order Mage module.
Am I able to access resources from different modules?
Edit:
I limited it to return one order, but no dice.
$collection = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect('*')
->addFieldToFilter('customer_id', Mage::getSingleton('customer/session')->getCustomer()->getId())
->setOrder('created_at', 'desc')
->setPageSize(1)
->setCurPage(1);
Above code is working there is no problem in code,
Now you need to check is you are getting customer id from
Mage::getSingleton('customer/session')->getCustomer()->getId();
If you are getting customer id then error is in other code of you file or another file. It may have syntax error.
I'm trying to display the number of total number of sales for the currently viewing product on a block, so far i have, with the help of a stackoverflow thread:
$product = Mage::registry('current_product')->getId();
$productID = Mage::getModel('catalog/product')->load($product)->getId();
$productReport = Mage::getResourceModel('reports/product_sold_collection')->addOrderedQty()->addAttributeToFilter('id',$productID);
foreach ($productReport as $product) {
$product1 = $product->getOrderedQty();
var_dump($product1);
}
I am able to load all sales quantities, but when i add the addAttributeToFilter
Fatal error: Call to a member function getBackend() on a non-object in C:\wamp\www\magento\app\code\core\Mage\Eav\Model\Entity\Abstract.php on line 816
It also happens if i pass $product directly, both are strings though. i don't know how to get the object i should pass to the addAttributeToFilter method, or if it should work with a string parameter.
Without trying it myself I suspect you need to filter by entity_id (and your second line is redundant).
$product = Mage::registry('current_product');
$productReport = Mage::getResourceModel('reports/product_sold_collection')
->addAttributeToFilter('entity_id', $product->getId())
->addOrderedQty();
$qty = $productReport->getFirstItem()->getOrderedQty();
EDIT: Product and category collections also have an addIdFilter() method for that purpose, e.g
->addIdFilter($product->getId())
See Mage_Catalog_Model_Resource_Product_Collection::addIdFilter() for more details.
$category = Mage::getModel('catalog/category')->load($currentCategory->getId());
$_productCollection = $category->getProductCollection()->addFieldToFilter('genre', array('finset' => '126'))->addAttributeToFilter('category_ids',array('finset'=>'14'));
Mage::getModel('catalog/layer')->prepareProductCollection($_productCollection);
$_productCollection = $_productCollection->load();
The above statement is giving an error, please help me run this.
What are you trying to do exactly?
If you call $category->getProductCollection, you will only get the products in the category you loaded so no need for a category filter. I think for Magento above 1.4, category_ids doesn't exist anymore.
I'm successfully running this query:
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->setOrder('created_at', 'desc')
->setPage(1, 5);
but when I add
->addCategoryFilter('3')
I got the following error:
Fatal error: Call to a member function getId() on a non-object in /home/sitename/public_html/app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php on line 556
The block it is running at is defined as
<block type="catalog/product_list" name="catalog.right.bestsellers" template="catalog/navigation/right.phtml"/>
at catalog.xml
This covers 1.4.2. Based on your error, I think you're running a slightly older version, but the root cause of your error should be the same.
The class alias reports/product_collection resolves to
Mage_Reports_Model_Mysql4_Product_Collection
in resource mode context. The class Mage_Reports_Model_Mysql4_Product_Collection is a child of Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract. The addCategoryFilter on Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract looks like this
public function addCategoryFilter(Mage_Catalog_Model_Category $category)
{
$this->_productLimitationFilters['category_id'] = $category->getId();
if ($category->getIsAnchor()) {
unset($this->_productLimitationFilters['category_is_anchor']);
}
else {
$this->_productLimitationFilters['category_is_anchor'] = 1;
}
($this->getStoreId() == 0)? $this->_applyZeroStoreProductLimitations() : $this->_applyProductLimitations();
return $this;
}
Notice that in 1.4.2 there's some type checking going on in the paramaters
public function addCategoryFilter(Mage_Catalog_Model_Category $category)
I suspect your version doesn't have that checking. Therefore, it fails when it reaches
$this->_productLimitationFilters['category_id'] = $category->getId();
You're passing in a string, and then Magento tries to call getId on the string. That's why you get an error.
Magento expects you to pass in a category object, and not a category ID. Give this a try
$category = Mage::getModel('catalog/category')->load(3);
$p = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->setOrder('created_at', 'desc')
->addCategoryFilter($category)
->setPage(1, 5);
The parameter for addCategoryFilter() must be an object of type Mage_Catalog_Model_Category.