How to get product name in magento? - magento

I am trying to get only product name using the product id.
I have tried the below:
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
$product->getName();
It load the whole resource object and than we can get the name using $product->getName() but I need only single name so we can reduce the extra overheads.
$product = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('name')->addAttributeToFilter('entity_id', PRODUCT_ID)->load();
It also load the resource object.
is there any easiest way to get the product name( for exa. 'Test product') without loading the resource object?

Use getAttributeRawValue() With this you can get the attribute value of your product :
$attribute_value = Mage::getResourceModel('catalog/product')->getAttributeRawValue(PRODUCT_ID, 'attribute_code');

Try this,hope it's work for you.
Try this :
$product = Mage::registry('current_product'); then
$product->getName();
$product->getId();
You can try this for product :
$id = $this->getRequest()->getParam('id');
$current_product=Mage::getModel('catalog/product')->load($id);

We need to pass store value as well to get product attribute directly from resource model.
You can get store id by using this code:- Mage::app()->getStore().
So the final query will be :-
$attribute_value = Mage::getResourceModel('catalog/product')->getAttributeRawValue(PRODUCT_ID,'attribute_code',Mage::app()->getStore());

Related

How to programmatically get option id's and values from configurable product in phtml?

I am modifying phtml code were I have access to
$this which happens to be Mage_Checkout_Block_Cart_Item_Renderer_Configurable class
and
$_item = $this->getItem();
$_product = $_item->getProduct();
where product is a configurable product in a cart.
Based on this I need to get a list of option id (swatches) and their values.
For example, to get specific size option id :
$product = $_item->getProduct();
$idStore = $_item->getOrder()->getStoreId();
$sizeSpecificAttributeCode = 'specific_size';
$sizeAttributeOptionId = $product->getResource()
->getAttributeRawValue($product->getId(), $sizeSpecificAttributeCode,$idStore);
from the same logic you can get the swatches or other options

Magento: Get a custom attribute value without loading the entire product

At the moment I use this to get a custom attribute value:
$_item = $this->getProduct()->getId();
$_product = $_product = Mage::getModel('catalog/product')->load($_item);
$optionvalue = $_product->getCustomAttributeValue();
echo $optionvalue;
I wonder is there an easier way to get this custom value without loading the entire product?
This depends on which version of Magento you're running. Different versions have different offerings. If you're running Community edition 1.6+, the Catalog module has a very nice method just for you!
Try the following:
$_item = $this->getProduct()->getId();
$_resource = $this->getProduct()->getResource();
$optionValue = $_resource->getAttributeRawValue($_item, 'custom_attribute_value', Mage::app()->getStore());
echo $optionvalue;
If you're interested, you could dive down into Mage_Catalog_Model_Resource_Abstract to see what this little guy is doing. It's essentially just a query (admittedly a rather complex one, as EAV tends to be) to retrieve the one attribute you asked for (or the multiple attributes you asked for, since you can pass an array as well).
I just want to improve #JMTyler answer, because I found out you don't need a real product model to get the getResource()
So you can just do it having a product id and using a singleton ( this would be better in case you are doing it in a loop so you don't actually create the model many time )
$product_id = 10075;
$_resource = Mage::getSingleton('catalog/product')->getResource();
$optionValue = $_resource->getAttributeRawValue($product_id, [ATTRIBUTE_ID/ATTRIBUTE_CODE], Mage::app()->getStore());
echo $optionValue;
Other simple way is add this "custom_attribute" to the list of attributes to get by default when you check product data from a quote item.
If you already created a custom module in config.xml add this.
<global>
...
<sales>
<quote>
<item>
<product_attributes>
<custom_attribute />
</product_attributes>
</item>
</quote>
</sales>
...
</global>
This may not provide much, if any performance benefit; however, it will fetch only the attribute value and no other columns:
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter('entity_id', $_item);
$collection->getSelect()
->reset('columns')
->columns(array('[custom attribute code]'));
$value = $collection->getFirstItem()
->getData('[custom attribute code]');
You could also use direct SQL, though I wouldn't recommend it unless performance were a real issue:
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = <<<SQL
SELECT `value`
FROM catalog_product_entity_[backend type]
WHERE entity_id = {$_item}
AND attribute_id = [attribute ID]
SQL;
$row = $connection->fetchRow($sql);
$value = $row['value'];
If the desired product value is based on a dropdown/source-attribute, you can do the following:
$resource = $magentoProductInstance->getResource();
$value = $resource->getAttributeRawValue($productId, $attributeCode, Mage::app()->getStore());
//if the value is an option id continue with this
$optionId = $value;
return $resource->getAttribute($attributeCode)
->setStoreId(Mage::app()->getStore()->getId())
->getSource()
->getOptionText($optionId);

Creating programatically a category when Flat mode is enabled in Magento

I want to create programatically a category in my data folder of my module. The Flat categories option is enabled.
When a I try to create a category like so:
$category
->setStoreId(0)
->setName('My category')
->setUrlKey('club-campaigns')
->setPath($rootCategory->getPath())
->setIsActive(1)
->setIsAnchor(1)
->setIncludeInMenu(1)
->addData($data)
->setCustomDesignApply(1)
->save();
I get an error which says that catalog_category_flat doesn't exists. Ok, so I know that flat categories info is kept in catalog_category_flat_store_storenumber table. I looked in the database and I have the following tables:
catalog_category_flat_store_1
catalog_category_flat_store_2
catalog_category_flat_store_3
catalog_category_flat_store_4
catalog_category_flat_store_5
catalog_category_flat_store_6
and I want to create a category for store 6. Good, now if I do like this:
$category
->setStoreId(6)
->setName('My category')
->setUrlKey('club-campaigns')
->setPath($rootCategory->getPath())
->setIsActive(1)
->setIsAnchor(1)
->setIncludeInMenu(1)
->addData($data)
->setCustomDesignApply(1)
->save();
the category is created without errors, and it sets the info in catalog_category_flat_store_6 but if I go to admin>Manage Categories and cant't see my category created.
I think that when I create a category I should set te store id of the admin(0) so I can see it in the admin panel but then I get the error above, and if I create with store 6 I don't see it in the admin. I'm really stuck.
How can I properly create my category programatically without problems?
Create category dynamically:
$category = Mage::getModel('catalog/category');
$category->setStoreId(Mage::app()->getStore()->getId());
$cat['name'] = "Custom Category Name here";
$cat['path'] = "1/2/30"; //parent relationship..
$cat['description'] = "categorie's description";
$cat['is_active'] = 1;
$cat['is_anchor'] = 0; //for layered navigation
$cat['page_layout'] = 'two_columns_left';
$cat['url_key'] = "custom-category"; //url to access this category
$cat['image'] = "custom-category.jpg";
$category->addData($cat);
$category->save();
Then reindex catalog_category_flat dynamically:
$process = Mage::getSingleton('index/indexer')->getProcessByCode('catalog_category_flat');
$process->reindexEverything();

Magento: How to get the id of the just duplicated product programmatically

How can I get the id of the just duplicated product programmatically in Magento ?
(or the last inserted product id, if the first solution don't exits)
Thanks for help.
[EDIT]
Here is the code that I have used to duplicate my product:
$sku = '123456';
$product = Mage::getModel('catalog/product')
->loadByAttribute('sku',$sku);
$newProduct = $product->duplicate();
$newProduct->setStatus(1);
$newProduct->setSku($sku.'-v2');
$newProduct->save();
Thanks for help.
you can find the new prodect ID in:
$newProduct->getId()
This is available just after calling :
$newProduct = $product->duplicate();
FYI: to get the last insertedId (in general) you can use:
Mage::getSingleton('core/resource')->getConnection('core_read')->fetchOne('SELECT last_insert_id()')

How do I get details for the products that were ordered on the success page in Magento?

I was able to get order details and customer details inside template/checkout/success.html file, but not sure how to get details for the exact products that were ordered? I need their names for some third party stat appliances. I bet it's simple - just another weird chained call to some "where-did-this-come-from?" method...
Help?
do (as you have order model)
$order_items = $order->getAllItems();
foreach($order_items as $item) {
$product = Mage::getModel('catalog/product')->load($item->getProductId());
//and you can do what ever you want with the product
}
You can retrieve the order details like:
$order = Mage::getModel('sales/order')->load(Mage::getSingleton('checkout /session')->getLastOrderId());
Then you can pull in all the various data with this:
$subtotal = $order->getSubtotal();
$order->getId();
$order->getIncrementId();
$order->getGrandTotal();

Resources