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!
Related
Actually I have two questions:
1) Is it possible to configure Laravel temporarily for a call to be database-agnostic and work on models instantiated in code only? In other words to prevent any database access when working with models and relations.
2) I tried to refactor this code to allow 1 but I'm hitting problems
/** #var Collection $collection */
$collection = $model->relation()->whereIn('MY_ID', [
RelationModel::IDENTIFIER_FOO,
RelationModel::IDENTIFIER_BAR,
// ...
])->get();
if (0 === $collection->count()) {
throw new \InvalidArgumentException('no entry found but mandatory');
}
Which I tried to change to this:
/** #var Collection $collection */
// note the missing parentheses
$collection = $model->relation->whereIn('MY_ID', [
RelationModel::IDENTIFIER_FOO,
RelationModel::IDENTIFIER_BAR,
// ...
])->get();
if (0 === $collection->count()) {
throw new \InvalidArgumentException('no entry found but mandatory');
}
But now I'm getting the error
Type error: Too few arguments to function Illuminate\Support\Collection::get(), 0 passed
It seems omitting the parentheses there is a different type of collection returned which requires to pass a parameter instead of none for the other type of collection.
Is it possible to refactor this to allow both kinds of requests, with and without database access?
Having trouble with Model Bind on davejamesmiller/laravel-breadcrumbs.
I'll try to be brief, but if you need more data just ask =D
This is my controller/action signature:
public function edit(Request $request, Vertical $vertical, UserMacro $macro)
And this is my BC for the corresponding route:
Breadcrumbs::register('macro-edit', function (Generator $breadcrumbs, $vertical, $macro) {
$breadcrumbs->parent('macro-index');
$breadcrumbs->push($macro->name, route('macro-edit', [$vertical->_id, $macro]));
});
I'm getting the string ID on $vertical and $macro, breaking on $macro->name. If I add Hints as the action have I get aType error.
Trying to get property of non-object (View: /.../resources/views/layouts/app.blade.php) (View: /.../resources/views/layouts/app.blade.php)
Type error: Argument 2 passed to DaveJamesMiller\Breadcrumbs\BreadcrumbsServiceProvider::{closure}() must be an instance of App\Vertical, string given, called in /.../vendor/davejamesmiller/laravel-breadcrumbs/src/BreadcrumbsGenerator.php on line 68 (View: /.../resources/views/layouts/app.blade.php) (View: /.../resources/views/layouts/app.blade.php)
I didn't analyze core code of library, so i don't know why controllers work, but breadcrumbs don't. Recipe to working model binding is use proper route naming convention.
Breadcrumbs::for('messages.show', function($trail, \App\Models\MassMessage $massMessage) {
$trail->parent('Index');
$trail->push('Show', route('messages.show', $massMessage));
});
Route::get('messages/{massMessage}', 'MessageController#show')->name('messages.show');
// error (controllers are fine)
Route::get('mass-mmessages/{massMessage}', 'MessageController#show')->name('messages.show');
// works both
The same problem is with resource route.
edit:
I was wrong. Controller also doesn't work. It not raise error but it pass empty Eloquent object. In my case i needed to change $massMessage variable's name into $message in both places and works fine, now.
I've loaded New Order transactional email to insert a custom block. I inserted it under the order items table layout handle call, and try to pass it $order variable.
{{layout handle="sales_email_order_items" order=$order}}
...
{{block type="mymodule/sales_order_email_description" order=$order}}
In Mymodule_Block_Sales_Order_Email_Description class I wrote:
protected function _construct() {
$this->setTemplate('email/order/description.phtml');
}
And finally in description.phtml I try to access order:
$order = $this->getOrder();
...
$order->getId()
At this point, exception rise:
Fatal error: Call to a member function getId() on a non-object ... ...
I followed several tutorials, like this magento email templates but I'm still stuck with this fatal error.
Any ideas?
Let's take a look;
You can define the block with a template, the construct isn't needed any more.
{{block type="mymodule/sales_order_email_description" template="email/order/description.phtml" order=$order}}
Indeed you could get the parameter values with $this->getOrder(). But $order->getId() may not work, $order->getEntityId().
Maybe you could try a var_dump to see if there is any order data.
Try this in your .phtml file:
$order = $this->getData('order');
As described here: http://www.webspeaks.in/2011/06/customize-new-order-email-template-in-magento.html
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.
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