magento non object in search - magento

i am getting an error during search ,i want to load the details from the products but
i am getting an error with an certain category and i am getting back this error:
Fatal error: Call to a member function getName() on a non-object in /home/xxxxxx/home/xxxxxxx/www/test/app/design/frontend/default/blank/template/catalog/product/view.phtml on line 130
the code i am using is:
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct();
$cat=$_product->getCategory()->getName();
the same code work fine when approaching the products without the search option.(directly)
Could someone tell me why i get this error?
By the way this error only occur for the category name not the rest
Or could someone tell me how to get the category by product id in magento.
Txs in advance...

Try inserting the following before your final line:
$product = Mage::getModel('catalog/product')->load($_product->getId());
That will load an instance of the product that contains all the attributes, including the category.
Cheers,
JD

Related

magento get custom attribute label

I've a problem, I want to show the label of a custom attribute in the product page.
I explain me better, starting from this link because is what I want to do:
http://www.customy.com/blog/how-to-display-video-on-magento-product-page/
I want a video product in the sidebar of the product page, so I create a new custompage.phtml and i put this in the sidebar from catalog.xml, in my custompage.phtml I put this code to have the custom label:
getResource()->getAttribute('video')->getStoreLabel();?>
but I have this error:
"Fatal error: Call to a member function getResource() on a non-object in ..path//"
I have try different code but still have this problem.
I think that I forget to put something in my .phtml but I'm new of Magento and I don't know what!
Thank in advance!
Since $_product didn't work then you'll need to load the object before attempting to access the attribute. Try this:
$product_id = Mage::registry('current_product')->getId();
$_product=Mage::getModel('catalog/product')->load($product_id);
echo $_product->getResource()->getAttribute('video')->getStoreLabel();
If you don't have access to the product model, I wrote a small query to get it from DB. This could be done better, but should be a decent starting point for your class:
protected $_dbConn;
public function __construct()
{
$this->_dbConn = Mage::getSingleton('core/resource')->getConnection('core_read');
}
public function getAttributeLabel($code)
{
$query = "
SELECT b.value
FROM eav_attribute a
JOIN eav_attribute_label b
ON a.attribute_id = b.attribute_id
WHERE a.attribute_code = '".$code."'";
return $this->_dbConn->fetchOne($query);
}

Page Not Appearing In Magento

IM having a weird instance where a page created via the CMS backend is not showing on the site. This is only happening on one of the pages out of 40 that i have newly created. I have tried deleting the page and remaking it but this produces the same error. I have also tried changing the status to Disabled and back to Enabled but that didn't work either.
I simply just get a white page with no errors in the console or otherwise. Any thoughts?
Your post is a little light on the details a programmer needs to help you, so here's how I'd debug this
Magento looks for a CMS page in the following method.
#File: app/code/core/Mage/Cms/Model/Resource/Page.php
public function checkIdentifier($identifier, $storeId)
{
$stores = array(Mage_Core_Model_App::ADMIN_STORE_ID, $storeId);
$select = $this->_getLoadByIdentifierSelect($identifier, $stores, 1);
$select->reset(Zend_Db_Select::COLUMNS)
->columns('cp.page_id')
->order('cps.store_id DESC')
->limit(1);
return $this->_getReadAdapter()->fetchOne($select);
}
This code generates a SQL query that compares the identifier in the URL with what it can find in the cms_page table. For someone reason this query is returning zero rows in your Magento instance. Add some temporary debugging code to output the SQL query
$select->reset(Zend_Db_Select::COLUMNS)
->columns('cp.page_id')
->order('cps.store_id DESC')
->limit(1);
echo '<pre>';
echo $select->__toString();
echo '</pre>';
exit(__METHOD__ . '::' . __LINE__);
return $this->_getReadAdapter()->fetchOne($select);
and determine why your query is returning no rows.

Magento Mage::getUrl with suffix

What is the correct way to handle generating urls in magento when urls have .html suffixes.
For example, to get the following product url:
category/product.html
You cannot simply do Mage::getUrl('mycategory/myproduct.html') or Mage::getUrl('mycategory/myproduct')
but instead
Mage::getUrl() . 'mycategory/myproduct.html'
You — dont?
The point of having a getUrl method is you provide the abstract, behind the scenes module/controller/action portions of the URL, and then the system handles generating actual HTML urls for you.
Best way to get the product url:
$productId = ***;
$productUrl = Mage::getBaseUrl().Mage::getResourceSingleton('catalog/product')->getAttributeRawValue($productId, 'url_key', Mage::app()->getStore()).Mage::helper('catalog/product')->getProductUrlSuffix();
If you want to get a product url, you should use the following
$product = Mage::getModel('catalog/product')->load($productId);
echo $url = $product->getProductUrl();
If you just have created the product from the backend, you need to get this url just after the save process, you have to get it by using the event catalog_product_save_after, creating an observer class and there you can get the product object thanks to $product = $observer->getEvent()->getProduct();

A model's collection turns out to be a non-object

I am trying to get a list of viewed products in Magento, but the following code:
$model = Mage::getModel('reports/product_index_viewed')->getCollection()
->addAttributeToFilter('store_id', array('eq' => 1));
created the error message:
Fatal error: Call to a member function getBackend() on a non-object in C:\xampp\htdocs\magento\app\code\core\Mage\Eav\Model\Entity\Abstract.php on line 816
Why is the collection returned in getCollection() a non-object?
You have an error in your filter call. Actually there is no store_id attribute for product and in your case collection tries to get this attribute, but since it doesn't exist an error occurs. In report collection there is a special method created to specify store filter, so you code should look like this (also I included construction for proper type hinting):
/* #var $collection Mage_Reports_Model_Resource_Product_Viewed_Collection */ // This enabled type hinting
$collection = Mage::getModel('reports/product_index_viewed')->getCollection();
$collection->setStoreId($storeId); // Setting data scope (e.g translated names, prices, etc)
$collection->addStoreFilter($storeId); // Set filter by exact availability on this store.
Have fun with Magento development!

Help with Magento and related products

I have a customer product page that literally lives beside the catalog/product/view.phtml page. It's basically identical to that page with a few small exceptions. It's basically a 'product of the day' type page so I can't combine it with the regular product page since I have to fetch the data from the DB and perform a load to get the product information
$_product = Mage::getModel('catalog/product')->load($row['productid']);
To make a long story short, everything works (including all children html blocks) with the singular exception of the related products.
After the load I save the product into the registry with
Mage::register('product', $_product);
and then attempt to load the related products with:
echo $this->getLayout()->createBlock('catalog/product_view')->setTemplate('catalog/product/list/related.phtml')->toHtml();`
All of which give back the error:
Fatal error: Call to a member function getSize() on a non-object in catalog/product/list/related.phtml on line 29`,
and line 29 is
<?php if($this->getItems()->getSize()): ?>`.
Any help getting the relateds to load would be appreicated.
I didn't quite follow what you're trying to do, but I know why you're getting your errors. You're creating a block whose class-alias/class is
catalog/product_view
Mage_Catalog_Block_Product_View
but you're setting this block's template as
catalog/product/list/related.phtml
The stock catalog/product/list/related.phtml template was built to be used with a catalog/product_list_related Block only, and not a catalog/product_view Block.
If you take a look at the class definition for a catalog/product_list_related Block (which is a Mage_Catalog_Block_Product_List_Related), you can see that there's a getItems() method.
public function getItems()
{
return $this->_itemCollection;
}
which returns a collection. The collection is set in the _prepareData method
protected function _prepareData()
{
$product = Mage::registry('product');
/* #var $product Mage_Catalog_Model_Product */
$this->_itemCollection = $product->getRelatedProductCollection()
...
This collection is never set with a catalog/product_view Block, which is why you're getting your errors.
In your code above, if you switch to creating a catalog/product_list_related block, your errors should go away.
public function relatedproductsAction(){
$this->loadLayout();
$relatedBlock = "";
$rec_prod_id = Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
$_product = Mage::getModel('catalog/product')->load($rec_prod_id);
Mage::register('product', $_product);
$relatedBlock = $this->getLayout()->createBlock('catalog/product_list_related')->setTemplate('catalog/product/related.phtml')->toHtml();
echo $relatedBlock;
exit;
}
Getting html of related block through ajax call, right after when product is added to cart. might be relatively helpful.

Resources