Currently, if I want to get a certain collection of products, for example best-selling, I use the following, direct in my template file:
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('name')
->addAttributeToFilter('visibility', $visibility)
->addOrderedQty()
->setOrder('ordered_qty', 'desc')
$_productCollection->load();
...and then pull out the products with a foreach statement.
Can anyone explain how to make a new block to do this, that can be re-used? I've found a few examples but they always call the product list from a CMS page, whereas I want to have the call to the function embedded directly in a template file, that I can call from anywhere.
So presume I have my module set up, and my Bestseller.php file in my Block folder. In it I guess I put my function for the collection, something like
protected function _getBestsellingCollection()
{
$_BestsellingCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('name')
->addAttributeToFilter('visibility', $visibility)
->addOrderedQty()
->setOrder('ordered_qty', 'desc');
$_BestsellingCollection->load();
}
public function getLoadedBestsellingCollection()
{
return $this->_getBestsellingCollection();
}
And if so, how then do I call that from my template? Something like?
$_productCollection = $this->getLoadedBestsellingCollection()
Any help, or pointers to decent tutorials, much appreciated!
UPDATE:
I'm getting closer, but I'm having trouble with extending the Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection class. If I add my code to the end of the Collection.php file, like
public function addBestSelling()
{
$this->addAttributeToSelect('*')->addOrderedQty()->setOrder('ordered_qty', 'desc');
return $this;
}
and then use
$_productCollection = Mage::getResourceModel('reports/product_collection')->addBestSelling();
in my phtml template file, it works fine. But if I separate that code into my Bestseller.php, in my Models folder of my module, like so
class Samsmodule_FeaturedProducts_Model_Bestseller extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
{
public function addBestSelling()
{
$this->addAttributeToSelect('*')->addOrderedQty()->setOrder('ordered_qty', 'desc');
return $this;
}
}
and then try and use it with the following, I get an error where the page doesn't finish loading (no error message)
$_productCollection = Mage::getResourceModel('featuredproducts/bestseller')
->addMostViewed();
What am I missing?
Blocks are for rendering HTML. Each Block Object has a phtml template object. When you use $this from a phtml template you're referring back to the containing block object.
It doesn't sounds like you're rendering HTML. It sounds like you want to fetch a specific list of products to use in any block/template.
If the above assumptions are correct, instead of creating a new Block you want to create a new Model Collection class that extends the class of the object returned by
Mage::getResourceModel('reports/product_collection').
Add your method to that class, and call it with something like
Mage::getResourceModel('mymodule/my_collectionclass')-> getLoadedBestsellingCollection()
Related
I am trying to do something I've never done before in Laravel and cannot figure out how to do it.
I have the following code in my Controller:
public function show($id)
{
//Get application for drug
$application = PharmaApplication::where('ApplNo', $id)->first();
//Get all products for given application (i.e. the different quantities and forms drug comes in)
$product = PharmaProduct::where('ApplNo', $id)->get();
foreach($product as $product){
$product->ProductNo;
}
//Get Marketing Status for drug
$marketingStatus = DB::table('pharma_marketing_statuses')
->where('ApplNo', $id)
->where('ProductNo', $product->ProductNo)
->get();
//Lookup marketing status Description
$marketingStatusDescription = PharmaMarketingSatusLookup::where('MarketingStatusID', $marketingStatus->MarketingStatusID);
return view('profiles.drug', compact('application', 'product', 'marketingStatus', 'marketingStatusDescription'));
}
I am trying to accomplish the following:
Get the application for a drug - this part of my code works
Return an array of objects for the products (i.e. 7 products that belong to one application). I can do this but get stuck going to the next part.
Next, I have to use the array of objects and search a table with the following columns: MarketingStatusID, ApplNo, ProductNo. I know how to query this table and get one row, but the problem is I have an array that I need to search. I imagine I have to use a loop but don't know where.
Finally, I use the MarketingStatusID to retrieve the MarketingStatusDescription which I will know how to do.
I am also getting an error message that says:
Class 'App\Http\Controllers\profiles\PharmaMarketingSatusLookup' not found
In my Controller, I have use App\PharmaMarketingStatusLookup; so I am not sure why it is searching the Controllers folder
You have a typo in your class
From PharmaMarketingSatusLookup change to PharmaMarketingStatusLookup
App\Http\Controllers\profiles\PharmaMarketingStatusLookup
USE whereIn
use App\PharmaApplication;
use App\PharmaProduct;
use App\PharmaMarketingSatusLookup;
public function show($id)
{
$application = PharmaApplication::where('ApplNo', $id)->first();
$products = PharmaProduct::where('ApplNo', $id)->get();
$productid = array();
foreach($products as $product){
$productid[] = $product->ProductNo;
}
$marketingStatus = DB::table('pharma_marketing_statuses')
->where('ApplNo', $id)
->whereIn('ProductNo', $productid)
->get();
$marketingStatusDescription = PharmaMarketingSatusLookup::where('MarketingStatusID', $marketingStatus->MarketingStatusID);
return view('profiles.drug', compact('application', 'product', 'marketingStatus', 'marketingStatusDescription'));
}
My controller method like below
public function index($book_id) {
print_r($book_id);
}
I will get the id from view. View like below
I need to get specific row according id on model how can I do that if I use query in controller its not working
If I use like below in controller it gives something else as result
public function index($book_id) {
print_r($book_id);
$this->db->select('*');
$this->db->from('books')->where('book_id', $book_id);
$query = $this->db->get();
print_r($query);
}
but if I use same query in model with hard coded id for testing it gives the expected out put
Please help me with this
You don't need to include "/index" in anchor tag href as controller default method will be index if found. And "print_r($book_id)" should be "echo $book_id" as $book_id is variable not array.
Please try this code (either in model/controller):
$query = $this->db->get_where('books', array('book_id' => $book_id));
$result = $query->row_array();
print_r($result);
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 have created a module. it is working properly but when we get product collection in Observer.php file then this is not return any objects and collection. it is empty and also not create any expection or log file. please help me.
I have this code in observer.php file
class GWB_ClearOrphan_Model_Observer
{
public function disableProducts(Varien_Event_Observer $observer)
{
try{
$collection = Mage::getModel('catalog/product')->getCollection();
}
catch(Exception $e) {
Mage::log($e->getMessage(), null, 'collection.log');
}
}
}
I am also trying both product collection model method but collection not found.
$collection = Mage::getResourceModel('catalog/product_collection');
$collection = Mage::getModel('catalog/product')->getCollection()->load();
Using
$collection = Mage::getModel('catalog/product')->getCollection();
will not load anything. You have to call load() method, like this:
$collection = Mage::getModel('catalog/product')->getCollection()->load();
It would be better to know what you want to achieve, which event do you observe and why you want to load all products in your observer? You do not use any filters on collection, so in fact statement above will load all products, which can kill your request.
In extention I'm getting product collection on catalog_product_view page like this:
if (!$product = Mage::registry('product')) {
return new Varien_Data_Collection();
}
$category = new Mage_Catalog_Model_Category();
$category->load($product->getCategoryId());
$collection = $category->getProductCollection();
and how can I add additional attributes to this collection?
for example I can't get something like this
$collection->addAttributeToSelect('manufacturer');
I wish to add some attribute by code (not id, because this may be different attributes added in layout) and then group data by this attribute
thnx
You could instantiate a product collection and filter it instead of getting the products of a specific category directly:
if (!$product = Mage::registry('product')) {
return new Varien_Data_Collection();
}
// get a product collection and filter it by category
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addCategoryFilter($product->getCategoryId());
// add the attributes you need
$collection->addAttributeToSelect('manufacturer');
$collection->setOrder('manufacturer', 'asc');
// load the collection and return it
$collection->load();
return $collection;
Be careful: You can not add attributes to the collection after loading it!
Additionally, you do not have to explicitely call load() - the collection will be loaded on demand.
Hope this helps.
Try this
<?php echo $_product->getResource()->getAttribute('manufacturer')->getFrontend()->getValue($_product); ?>