Magento: ResourceModels throwing exceptions - magento

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.

Related

How to decrement product quantity after checkout in Laravel (With Session)

Can you help me how to decrement after doing a checkout. So far this is the only idea I have but it shows an error called "Object of class Illuminate\Database\Eloquent\Collection could not be converted to number
". Thank you for the reply. Also I am using the one with session.
Note. I edit and put another image for more clearer explanation and here is the code
public function postCheckout(Request $request){
$books = Product::all();
if (!Session::has('cart')){
return view('shop.shoppingcart');
}
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
$order = new Order();
$order->cart = serialize($cart);
$order->address = $request->input('address');
$order->name = $request->input('name');
Auth::user()->orders()->save($order);
$q=$cart->totalQty;
$r=$books-$q;
Product::table('books')
->where('id',$id)
->update(array('quantity' => $r));
Session::forget('cart');
return redirect("home");
}
And the error is "Object of class Illuminate\Database\Eloquent\Collection could not be converted to number"
You have this line of code:
$books = Product::all();
Here, $books is a collection.
And after a few lines, this one:
$r=$books-$q;
which of course can't execute without error because you're trying to subtract a number from a collection.
I assume you don't actually want the whole book collection, but the number of books. In that case, replace $books = Product::all(); with $books = Product::count(); and your code should work fine.
However, that line counts all the books, and I guess you only want to decrease the quantity of the purchased books. Unfortunately, that's not what you code does. For example, in this code:
Product::table('books')
->where('id',$id)
->update(array('quantity' => $r));
where does the $id variable come from? You don't set its value anywhere. What you should do is iterate every item of your cart, get the ID and quantity of each item, and perform an update query for each item.

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

Get product sku in Magento using product ID for big collections

I have a catalog with near 90000 products. I need to retrieve all the products sku that have no image associated. With a sql query I can get the id list of all the products without image. From that ID I need to get the product sku. So, I have an $ids array with all the products without image (near 60000).
I'm trying to get all the corresponding skus by using something easy with the magento api:
foreach ($ids as $id){
$product = Mage::getModel('catalog/product')->load($id);
echo $product->getSku()."\n";
}
But this causes a PHP Fatal error: Allowed memory size... (memory size is 1024Mb and I cannot change it).
My question is: from this $ids array, how can I get all the corresponding sku without causing a memory size error? Is there a lighter way of getting a product attribute having the product id?
Currently you are loading a lot of not needed product data and this causes a fatal error. In your case you need only product sku. I am suggestion to use Magento collections.
I guess, that the following code snippet will work for you:
$productsCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter('entity_id', array('in' => $ids));
foreach($productsCollection as $product) {
echo $product->getSku();
}
Magento automatically adds the sku to the select, but pay attention, that sometimes you may want to get some custom product attribute, e.g. attribute with code "color". Then you need to add ->addAttributeToSelect('color') and the code will look like:
$productsCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter('entity_id', array('in' => $ids))
->addAttributeToSelect('color');
foreach($productsCollection as $product) {
echo $product->getSku();
echo $product->getColor();
}
If you want to get all product attributes you can use ->addAttributeToSelect('*')
To get separate attribute without loading product
$resource = Mage::getResourceSingleton('catalog/product');
$sku = $resource->getAttributeRawValue($productId, 'sku', $storeId)

Magento - Retrieve highest product price in current collection from layered nav

Layered navigation filters are created in
app/design/frontend/base/theme/template/catalog/layer/filter.phtml
How can I retrieve the value of the highest product price in the current product collection from within this file?
I've tried what I thought was the obvious choice $this->getMaxPriceInt() from Mage_Catalog_Model_Layer_Filter_Price but that does not seem to work within filter.phtml.
Assuming $collection is a collection of 'catalog/product', this should do the trick:
$product = $collection->setOrder('price', 'DESC')->getFirstItem();
Get highest and lowest price in category listing you can use below code.
$min_price = Mage::getSingleton('catalog/layer')->setCurrentCategory(Mage::registry('current_category'))->getProductCollection()->getMinPrice();
$max_price = Mage::getSingleton('catalog/layer')->setCurrentCategory(Mage::registry('current_category'))->getProductCollection()->getMaxPrice();
But it is not working for search and advance search layer navigation.so you can use below code for all pages layer navigation.$min_price = $this->getLayer()->getProductCollection()->getMinPrice();
$max_price = $this->getLayer()->getProductCollection()->getMaxPrice();
You can use below code on app/design/frontend/THEME/default/template/catalog/layer/view.phtml file.
Could you please try this ( let me know the result )
$model = Mage::getModel('catalog/product');
$collection = $model->getCollection()
->addStoreFilter()
->addAttributeToSelect('price')
->addAttributeToSort('price', 'asc');
if(!emtpy($collection)):
foreach($collection as $products) {
echo $products->getPrice();
echo $products->getName();
}
endif;

Get shopping cart details in Magento

I want to get the shopping cart details, by using Magento's getQuote function. How can I do this?
$cart = Mage::getModel('checkout/cart')->getQuote();
When I print the $cart Page stops execution and blank page is shown.
But when I write
$cart = Mage::getModel('checkout/cart')->getQuote()->getData();
and print the $cart an array will show. But I want to track the complete cart data (product Id,Product price like all information).
Is there any other method by which I can find the shopping card data?
The object returned by getQuote is a Mage_Sales_Model_Quote. It has a method getAllItems which in turn returns a collection of Mage_Sales_Model_Quote_Item objects.
All this means you can inspect products like this:
$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $item) {
$productId = $item->getProduct()->getId();
$productPrice = $item->getProduct()->getPrice();
}
PS. The reason you get a blank page is because dumping a whole object likely fell into recursion and the page timed out, or PHP ran out of memory. Using getData or debug is safer but, as you saw, doesn't return the protected/private variables.
Get shopping cart items using getQuote() method
$cart = Mage::getModel('checkout/cart')->getQuote()->getItemsCollection();
echo "<pre>";
print_r($cart->getData());
Get Shopping Cart items without getQuote() method
$cart = Mage::getModel('checkout/cart')->getItems();
echo "<pre>";print_r($cart->getData());

Resources