can't work $itemObject->getProduct()->getName() in magento - magento

I m trying to get product information through item object using getProduct() method of Mage_Sales_Quote_Item class without a for loop. Below is my nonworking code. How do I get data of product using the getProduct() method?
$quoteId = 5;
$quoteItemObject = Mage::getModel('sales/quote')->load($quoteId)->getAllItems();
echo $quoteItemObject->getProduct()->getName();

$quoteItemObject returns an array of all items in the quote but not a separate item. You need to "foreach" it and get information for each item separately. Try something like this:
foreach ($quoteItemObject as $item) {
echo $item->getProduct()->getName();
}

Related

Can't modifiy eloquent query result

So I got the following code in my controller's show function which just returns a page with the tags:
$page = Post::with('tags')->findOrFail($id);
$page->tags->lists('name');
return response($page);
When I try to to execute this, it won't change the tags key, which is an array with the tags from the eloquent belongsToMany relationship.
Why isn't this working? To me it seems pretty handy to just change a value like this.
When I change it to $page->test = $page->tags->lists('name') it will add the test key as usual.
How would I modify a eloquent value in a easy way?
What works pretty well for such cases is overriding toArray in your Model:
public function toArray(){
$array = parent::toArray();
$array['tags'] = $this->tags->lists('name');
return $array;
}
After the $page = Post::with('tags')->findOrFail($id); line is executed, $page->tags is going to be an Illuminate\Database\Eloquent\Collection object containing all the related Tags for the Post. From your provided code and question, it sounds like you want to then change $page->tags to be an array containing just the related tag names.
The statement $page->tags->lists('name') is only going to return an array of all the names of the related tags; it does not modify the underlying collection. If you wanted to modify the $page->tags attribute, you would need to assign it the result of your statement:
$page->tags = $page->tags->lists('name');
However, $page->tags was an attribute that was dynamically created and assigned by the Model, and is expected to hold the contents of a relationship. Manually modifying the contents like this may have unintended consequences, but I do not know.
Edit
The Model::toArray() method merges in the relationship information over the attribute information. So, you can change the attribute, but if you echo the model, the relationship information will show up over your attribute change.
$page->tags = $page->tags->lists('name');
// this will echo the tags attribute, which is now the array of tags
echo print_r($page->tags, true);
// this will echo the model, with the tags attribute being
// overwritten with the related data
echo $page;
One option would be to unset the attribute (which also unsets the relationship) and then reassign the attribute to your desired data:
$page = Post::with('tags')->findOrFail($id);
$temp = $page->tags;
unset($page->tags); // you must unset the attribute before reassigning it
$page->tags = $temp->lists('name');
return response($page);
A little bit cleaner would be to use a different attribute name:
$page = Post::with('tags')->findOrFail($id);
$page->tagNames = $page->tags->lists('name');
unset($page->tags);
return response($page);
And another option is to do what #lukasgeiter suggested and override the Model::toArray method:
class Post extends Model {
public function toArray() {
// call the parent functionality first
$array = parent::toArray();
if (isset($this->tags)) {
$array['tags'] = $this->tags->lists('name');
}
return $array;
}
}
If you want to change the output of one of the relationships in the toArray/toJson methods, then use accessor:
// in order to not show the underlying collection:
protected $hidden = ['tags'];
// in order to append accessor to toArray output
protected $appends = ['allTags'];
// mutate the collection to be just an array of tag names
public function getAllTagsAttribute()
{
$collection = return $this->getRelation('tags');
return ($relation) ? $collection->lists('name') : [];
}
then you will get simple array instead of collection when you do $page->allTags or in the toArray/toJson output, while not showing the real collection.
It is allTags not `tags, since the latter should remain eloquent dynamic property, so you can work with it as usual before outputting anything.
not sure if this helps. To be honest, I do not get your point. But I guess there is something wrong with this line:
$page->tags->lists('name');
If $page->tags is a belongsToMany relationship and you want to add more query conditions after this relationship, you should query like this:
$page->tags()->lists('name');

laravel access model properties

I am looking for solution how to access eloquent model items by 'alias' field.
There is no problem accessing items by 'id'. But building a custom query I find myself unable to access item properties.
This piece of code works perfect
$cat = Category::find(1);
return $cat->title;
But if I am querying items with any other argument - properties are inaccessible
This code
$cat = Category::where('alias','=','vodosnab')->get();
return $cat->title;
throws an exception
Undefined property: Illuminate\Database\Eloquent\Collection::$title
Could you please help.
You already got the answer but here are some insights, when you use get() or all(), it returns a collection of model objects, which is an instance of Illuminate\Database\Eloquent\Collection, so here you'll get a Collection object
$cat = Category::where('alias','=','vodosnab')->get();
Now, you can use, $cat->first() to get the first item (Category Model) from the collection and you may also use $cat->last() to get the last item or $cat->get(1) to get the second item from the collection. These methods are available in the Collection object.
Using the first() method like Category::where('alias','=','vodosnab')->first(); will return you only a single (the first mathing item) model which is an instance of your Category model. So, use all() or get() to get a collection of model objects and you can loop through the collection like:
foreach(Category::all() as $cat) { // or Category::get()
$cat->propertyName;
}
Or you may use:
$categories = Category::where('alias','=','vodosnab')->get();
foreach($categories as $category) {
$category->propertyName;
}
Also, you may use:
$categories = Category::where('alias','=','vodosnab')->get();
$firstModel = $categories->first();
$lastModel = $categories->last();
$thirdModel = $categories->get(2); // 0 is first
If you need to get only one then you may directly use:
$category = Category::where('alias','=','vodosnab')->first();
$category->fieldname;
Remember that, if you use get() you'll get a collection of Model objects even if there is only one record available in the database. So, in your example here:
$cat = Category::where('alias','=','vodosnab')->get();
return $cat->title;
You are trying to get a property from the Collection object and if you want you may use:
$cat = Category::where('alias','=','vodosnab')->get();
return $cat->first()->title; // first item/Category model's title
return $cat->last()->title; // last item/Category model's title
return $cat->get(0)->title; // first item/Category model's title
You may read this article written on Laravel's Collection object.
get() returns a Collection of items. You probably need first() that returns a single item.

Getting an indexed collection

I would like to get an indexed collection from an eloquent call to get a specific item:
$items = FeedItem::all();
$specific_item = $items[4];
Or is it possible to do something like:
$items->get('id', 4);
where id is the attribute and 4 is the value of the attribute.
FeedItem::all() will return a Illuminate\Database\Eloquent\Collection.
To get a specific model you can use the find method:
$items = FeedItem::all();
$item = $items->find($id);
For more methods of the Collection class see the docs and the api.

Looping through Magento attribute values skips the first item

This is an ajax file running the following code:
$model = Mage::getModel('catalog/product'); //getting product model
foreach ($violins as $k => $v)
{
$_product = $model->load($v); //getting product object for particular product id
$violinmodel = $_product->getAttributeText('Violinmodel'); //grabbing the violinmodel attribute value
echo $violinmodel;
}
$violin contains an array with three product ID's. My output is echoing the attribute value for the second and third ID's fine, but is NOT echoing the first ID!
I don't get this at all! Why would it completely skip the first ID in the loop and not echo anything, while echoing the following ID's without a problem?
The attributes are set up properly and no matter how I rearrange the ID's in the $violins array, the first attribute value is always skipped. What am I missing?
Initializing $model outside of your loop is unsafe. You might think that you are being more efficient with memory and/or function calls but you're asking for trouble. The Mage_Catalog_Model_Product object is being loaded at that time, and calling ->load() isn't giving you a new object, it's just setting the data of your existing one. Except, you'll get strange behavior when not all of the data gets overwritten (for example if ProductA has a Violinmodel attribute and ProductB doesn't... it will look like ProductA.Violinmodel == ProductB.Violinmodel). For that reason, you should always put your model inside your loop.
foreach ($violins as $k => $v) {
$_product = Mage::getModel('catalog/product')->load($v); //getting product
if ($_product->getId() == $v) { // sanity check
$violinmodel = $_product->getAttributeText('Violinmodel'); //grabbing the violinmodel attribute value
echo $violinmodel;
}
}
Or, as benmarks suggested, load this data via a collection:
$_products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('Violinmodel')
->addIdFilter($violins);
foreach ($_products as $_product) {
echo $_product->getAttributeText('Violinmodel'));
}
When you iterate over a product collection in Magento, the items it contains are actually product object instances. What you are doing here (hitting the database multiple times, loading all attribute) is unnecessary given what you are trying to accomplish (getting one attribute). Try adding the attribute to the collection to begin with and iterate over it:
$coll = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('Violinmodel');
//be certain that the attribute code is capitalized...
foreach ($coll as $product) {
//var_dump($product->debug()); //for example
var_dump($product->getAttributeText('Violinmodel));
}

Magento Shipping Module: Get all items currently in cart

I'm in the middle of developing a shipping module for Magento but get stuck on How to get the items that's currently in the cart for that session.
I follow some tutorial on the internet, they use:
if ($request->getAllItems()) {
foreach ($request->getAllItems() as $item) {
//do something here.....
}
}
My problem is that I don't know exactly what info/data that's on the $item variable??.
I want to get the weight and the price of the product that's currently on the cart to calculate the shipping fee. I tried to print the $item value by using Mage::log or printing it to the screen using print_r or var_dump but it's not successful. The log is empty and the variable won't be printed on screen.
Can somebody inform me of how to get the $item attributes/method or is there any other way to get the product information that's currently in cart?
you can achive this by using one of three methods which are available in Mage::getSingleton('checkout/session')->getQuote();
getItemsCollection() - retrive sales/quote_items collection
getAllItems() - retrive all items
getAllVisibleItems() - retrive items which aren't deleted and have parent_item_id != null
I was searching for this solution and found below. but not tested.
$CartSession = Mage::getSingleton('checkout/session');
foreach($CartSession->getQuote()->getAllItems() as $item)
{
$productWeight = $item->getWeight();
$productExPrice = $item->getPrice(); // price excluding tax
$productIncPrice = $item->getPriceInclTax(); // price excluding tax
}
If you want to know information about $item, you can do this:
print_r($item->getData());
If you want to get item weight, here is it:
$item->getWeight();

Resources