Product details in magento - php

I have this code in my magento app
<?php
$order_id = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order_details = Mage::getModel('sales/order')->loadByIncrementId($order_id);
foreach ($order_details->getAllItems() as $item) {
//here i know we can get item name as $item->getName(), sku as $item->getSku()
//but how do we get the following details
//1.category name,2.store,3.tax,city,country,state
<?php } ?>
I know by simply print_r($order_details->getAllItems()) will get the array list.but im in no situation to do this

This items are not as same as products, they don't have all the properties. You can either do a Mage::getModel('catalog/product')->load($item->getProductId()); fast way to test something, but this adds an extra query to mysql for every item in the loop. Or I recommend to edit the sales xml -> Mage > Sales > etc > config.xml (in a local module). You should add to the nodes sales_convert_quote_item or sales_convert_order_item the attributes you want to be copied from product to item. In your case it would be the sales_convert_order_item node since you are dealing with an order.

You need to load the full product model to access that data.
The collection you are loading is not of products, it's a collection of Order Items, which has minimal data associated with it. You need to load the full product model, by getting the product if from the Order Item.
try this:
<?php
$order_id = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order_details = Mage::getModel('sales/order')->loadByIncrementId($order_id);
?>
<?php foreach ($order_details->getAllItems() as $item): ?>
<?php $product = Mage::getModel('catalog/product')->load($item->getProductId()) ?>
<?php echo $product->getName() ?>
<?php // now you have the full product model/data loaded ?>
<?php endforeach ?>

Related

Magento get weigth of configurable product

good afternoon,
How can I recover the value in phtml from the weight of the product? this product is configurable, so it has variations, it needed to recover the weight of the variation
i use this for Default products:
$_product->getWeight()
Lets assume that the product model is loaded.
You need to load the model of the configurable in order to access its child products (variations).
Here is an example
<?php $configurable= Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$simpleCollection = $configurable->getUsedProductCollection()->addAttributeToSelect('*'); // selecting all attributes?>
<?php foreach($simpleCollection as $simple): ?>
<?php echo $simple->getWeight(); ?>
<?php // or?>
<?php echo $simple->getAttributeText('weight'); ?>
<?php endforeach; ?>
Hope this helps

Getting the last sub-category of a product in magento

I having been searching for days to get the last subcategory of a product in magento.
Actually what i have to do is display the last subcategory the product is placed in. for example i have plastic and glass as products. I want to display the last subcategory i.e cups or plates.
|Party
--|boys
----|batman
--------|cups
-----------|plastic
-----------|glass
--------|plates
----|Superman
i have edited the list.phtml file, i can get the category id's and name from the array but they are all mixed up. So there is no way to figure out which one is the last category.
Is there any default functionality in magento? or someone be kind enough to help me out?
Thanks in advance.
Edit
Okay, from your description it sounds like you want to get the child categories from the current category you're in. (I.E. Get cups and plates while in batman category view).
The following should be just about as little as you need to get the current children.
<?php
$_helper = Mage::helper('catalog/category');
$children = Mage::registry( 'current_category' )->getChildrenCategories();
?>
<ul>
<?php foreach( $children as $child ): ?>
<li><?php echo $child->getName() ?></li>
<?php endforeach; ?>
</ul>
Previous Answer(s)
It's a little roundabout, but this can get you the parent category id from a product object.
//If you don't have a product to start with, load by ID.
$_product = Mage::getModel( 'catalog/product' )->load($id);
//Assign Category Model
$categoryModel = Mage::getModel( 'catalog/category' );
//Get Array of Category Id's with Last as First (Reversed)
$_categories = array_reverse( $_product->getCategoryIds() );
//Get Parent Category Id
$_parentId = $categoryModel->load($_categories[0])->getParentId();
//Load Parent Category
$_category = $categoryModel->load($_parentId);
//Do something with $_category
echo $_category->getName();
It works better when the product only has one category Id assigned to it. You may not get the category you want if multiple categories have been assigned to that one product.
Also, you can get it a slightly quicker way, but this method doesn't work if the product was searched for:
$_category = Mage::getModel( 'catalog/category' )->load( Mage::registry( 'current_category' )->getParentId() );
I didn't test the line above, but it should work. Only if the product was reached by browsing through the categories though.

Using magento product model in footer

I am trying to display the Swatch attributes of all the associated simple products to the configurable product that the user is viewing.
I need to do this in the footer which is proving more difficult than I thought as a lot of methods etc are not available in the footer.
I have this code which just shows the Swatch attribute for the configurable product, I need this modified to show the Swatch attribute for all the simple products associated to this configurable.
<?php
$SKU = "2726578";
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$SKU);
echo $product->getSwatch();
?>
$sku = "2726578";
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
if($product->getTypeId() == "configurable"){
$childs = $product->getTypeInstance()->getUsedProducts();
}
Hope this helps! Just iterate over the childs and fetch the values.
cheers!

Magento - Adding a random product to .../template/catalog/product/view.phtml

I would like to add 3 links to random products on the .../template/catalog/product/view.phtml template.
Based on list.phtml, I tried adding:
<?php
$_productCollection=$this->getLoadedProductCollection();
foreach ($_productCollection as $_randomProduct) {
echo $_randomProduct->getProductUrl();
}
?>
But I'm not getting a list back of URL's. How can I get an array of other product's links and images?
That code lets you down because $this in view.phtml is not the same $this as in list.phtml.
If you wanted to get some products relevant to the current product's category try this:
$_productCollection = $_product->getCategory()->getProductCollection();
Alternatively if you didn't care where they come from:
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->setStore(Mage::app()->getStore());
To get the randomness this little trick will help:
$_productCollection->setPageSize(3)
->getSelect()->order('RAND()');
Now you can use the collection in a foreach loop.

Get product id in magento

In magento,i want to add quick look feature like this http://www.timberlandonline.co.uk/on/demandware.store/Sites-TBLGB-Site/default/Link-Category?cgid=men_footwear_boots.I have added a hidden input & a div in list.phtml.If i click the div of any product javascript returns product id of first product in that category page.But it should return product id of the selected div.
You need to look into this page (<path_to_your_template_folder>/template/catalog/product/list.phtml) carefully. You will find the following lines of code in different places of this page only:-
$_productCollection = $this->getLoadedProductCollection();
foreach ($_productCollection as $_product):
$reqProductId = $_product->getId();
endforeach;
If you carefully match the above code & the code in the above-mentioned page, you will know that you need to use the variable "$reqProductId" properly in your required "INPUT" element of type "hidden". So you will require it to do your part in the main "foreach" loop.
Hope it helps.
Try below code to get currently loaded product id:
$product_id = $this->getProduct()->getId();
When you don’t have access to $this, you can use Magento registry:
$product_id = Mage::registry('current_product')->getId();
Also for product type i think
$product = Mage::getModel('catalog/product')->load($product_id);
$productType = $product->getTypeID();

Categories