Product Collection not find in module observer file - magento

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.

Related

Looking up model in Laravel after returning array of objects in Controller

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'));
}

Magento sorting related products by price

i've been trying to sort related products by price in descending oreder (cheapest at the top and expensive at bottom) without any luck so far, maybe someone could guide me on how to do that?
See this file app\code\core\Mage\Catalog\Model\Product.php and check this function line 814
public function getRelatedProductCollection()
{
$collection = $this->getLinkInstance()->useRelatedLinks()
->getProductCollection()
->setIsStrongMode();
$collection->setProduct($this);
return $collection;
}
You need to modify this function by extend this Model Class so first Create an Module and Write this function inside your function. and add Order to collection.
$collection->setOrder('price', 'DESC');
public function getRelatedProductCollection()
{
$collection = $this->getLinkInstance()->useRelatedLinks()
->getProductCollection()
->setIsStrongMode();
$collection->setProduct($this);
$collection->setOrder('price', 'DESC');
return $collection;
}
You need to Extend this Module Mage_Catalog_Model_Product and also modify this function
Go to /app/code/core/Mage/Catalog/Model/ path and add the below code in Product.php
public function getRelatedProductCollection()
{
$collection = $this->getLinkInstance()->useRelatedLinks()
->getProductCollection()
->setIsStrongMode();
$collection->setProduct($this);
$collection->setOrder('price', 'DESC');
return $collection;
}
You can also use this for weight. Just write 'weight' in place of 'price'.
For ascending just write 'ASC' in place of 'DESC' .

magento product collection

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); ?>

Better way to add attribute to collection

I am overriding the Mage/Adminhtml/Sales/Order/Grid.php and adding some data to the prepareCollection. This is how I got the customer EAV Attribute campaign_id to be included in the collection, but it is kind of hacky. I was wondering if there was a better way.
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
foreach ($collection as &$object){
$customer = Mage::getModel('customer/customer')
->setId($object->getCustomerId())
->load();
$object->setCampaignId($customer->getCampaignId());
}
$this->setCollection($collection);
return parent::_prepareCollection();
}
You'll need to join the data from customer records onto the order collection before its loaded.
You can observe the collection before & after load events. For sales/order_grid_collection collection these events are sales_order_grid_collection_load_before and sales_order_grid_collection_load_after - you'll want to use the former. The collection can be accessed in your _before_load event observer via $observer->getOrderGridCollection().
protected function _prepareCollection() {
$collection = Mage::getResourceModel($this->_getCollectionClass());
$class = get_class($collection);
$attribute = Mage::getModel('eav/config')
->getAttribute('customer', 'campaign_id');
$attributeId = $attribute->getAttributeId();
$backendType = $attribute->getBackendType(); //probably varchar
$tableName = Mage::getSingleton('core/resource')
->getTableName('customer_entity_' . $backendType);
$collection->getSelect()
->joinLeft(array('v' => $tableName),
'main_table.customer_id = v.entity_id AND attribute_id = 153',
array('v.value', 'v.attribute_id'));
$this->setCollection($collection);
return parent::_prepareCollection();
}

Magento - making a new getProductCollection() function

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()

Resources