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

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

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

How to get product name in 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());

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

Magento 1.5.1.0 - While trying to update stock quantity on products, getStockData is returning NULL

I have products loaded in Magento that I am trying to bulk update the inventory qty on. I created all the products using Mage_Catalog_Model_Product and set the qty on them using setStockData like:
$product = new Mage_Catalog_Model_Product();
$product->setTypeId('simple');
$product->setStatus(1);
$product->setSku($sku);
$product->setStockData(array(
'is_in_stock' => 1,
'qty' => $record['stockstatus'],
'manage_stock' => 0,
));
...
So I've seen setStockData work ... 13,000+ times.
Now, as I said, I'm trying to update the inventory on the products I created using a variation on what I did to create the products ... a variation that I see here and virtually identically elsewhere on the web.
My problem is that I can use a variation of the code at the link above and get a valid product object, but when I call getStockData on the object, it returns NULL:
$product = Mage::getModel('catalog/product')
->loadByAttribute('product_code', '678910');
var_dump($product->getName()); // returns 'Hello My Name is Product'
var_dump($product->getProductCode()); // returns '678910'
var_dump($product->getSku()); // returns 'SKU1234'
var_dump($product->getStockData()); // returns NULL (and there is a qty of 52 set)
(I've also tried getting the product with no variations on the code at the link above and have had the same results as I expected.)
$product_id = Mage::getModel('catalog/product')->getIdBySku('SKU1234');
$product = Mage::getModel('catalog/product');
$product->load($product_id);
$stockData = $product->getStockData();
var_dump($product->getName()); // returns 'Hello My Name is Product'
var_dump($product->getProductCode()); // returns '678910'
var_dump($product->getSku()); // returns 'SKU1234'
var_dump($product->getStockData()); // returns NULL (and there is a qty of 52 set)
So, if I can't get the stock data with getStockData, I can't set the stock data like:
$product = Mage::getModel('catalog/product')
->loadByAttribute('product_code', $record['productcode']);
$stockData = $product->getStockData();
$stockData['qty'] = $record['stockstatus'];
$stockData['is_in_stock'] = ($record['stockstatus'] > 0) ? 1 : 0;
$product->setStockData($stockData);
$product->save();
Is there something I'm missing? I don't understand why getStockData is returning NULL. Can someone help me understand what I may be doing wrong?
Use getStockItem() instead of getStockData()
$stockData = $product->getStockItem();
$stockData->setData('qty',555);
$stockData->setData('is_in_stock',1);
$stockData->setData('manage_stock',1);
$stockData->setData('use_config_manage_stock',1);
$stockData->save(); // This enough to save stock data.
Use getData for arrays, use getItem for objects.
yeah no why getStockData() returns null.. But I was able to get the stock quantity using the following:
$qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId())->getQty();
You should call save() on stock_item rather than product after changing qty. Following code worked for me:
$product = Mage::getModel('catalog/product')
->loadByAttribute('product_code', '678910');
$stock_obj = Mage::getModel('cataloginventory/stock_item')
->loadByProduct($product->getId());
$stockData = $stock_obj->getData();
$stockData['qty'] = $record['stockstatus'];
$stock_obj->setData($stockData);
$stock_obj->save();

Magento: Get product price from another store?

I have multi-store Magento installation, and different product prices are set in different stores. I want to display on one page the actual product price from the current store, and the price from other store (I have its ID), but I'm not sure how to get that info?
The prices are set for each store view for each product, no tier-pricing or special-pricing was used.
If you know the storeId, set in setStoreId :
/**
* call the Magento catalog/product model
* set the current store ID
* load the product
*/
$product = Mage::getModel('catalog/product')
->setStoreId($storeId)
->load($key);
Display in a block :
echo $product->getName();
We can also use print_r to see the values :
print_r($product->getData());
The following code will show current store ID :
$storeId    = Mage::app()->getStore()->getId();
To get all product ID's with each store view :
$product    = Mage::getModel('catalog/product');
$products   = $product->getCollection()->addStoreFilter($storeId)->getData();
If you change the $storeId will show different product.

Resources