Update Magento product meta description for different store - magento

i am trying to update meta description for different store but magento keep showing error :
Product with URL key already exist.
what i am trying to do i have two stores on one magento. so we want seprate meta information for different stores. so when i try to update meta info for specific store then shows me error. any one facing same problem :
$pid = Mage::getModel('catalog/product')->getResource()->getIdBySku($sku);
$product = Mage::getModel('catalog/product')->load($pid);
if($product)
{
$product->setStoreId(6);
$product->setmeta_keyword($newKeyWord);
$product->setmeta_description($newmeta_metadescription);
try {
$product->save();
echo 'Product Updated successfully --- '.$sku."\n";
}
catch (Exception $ex) {
echo $ex->getMessage();
}

There are lot of issue in your code:
Setter function is wrong:
$product->setMetaKeyword($newKeyWord);
$product->setMetaDescription($newmeta_metadescription);
Best way to do this:
$product = Mage::getModel('catalog/product')->load($pid);
Please use below format,which more faster then your code
$product->addAttributeUpdate($Attributecode, $value, $storeId)
then
$product->addAttributeUpdate('meta_keyword', $newKeyWord, $storeId=6);
$product->addAttributeUpdate('meta_description', $newmeta_metadescription, $storeId=6);
No need use save() function in this case.

Related

Magento add product for different storeview in one step

currently when I add or edit products for magento via api I will do something like this for each storeview:
$product = Mage::getModel('catalog/product');
$product->setStoreId($default_store_id); // 0 = default/all store view.
// do something
try
{
$product->save();
}
catch (Exception $e)
{
echo $e->getMessage();
}
This will takes 1 second for each storeview. When I edit 1 product for 10 storeviews it will takes 10 seconds.
Is there any way to edit alle storeview data (different languages) in one step?
No is the simple answer. You can create optimised code though for editing and only save the attribute you are updating as opposed to the entire product object but this depends on the scenario you are catering for.
$product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($product_id);
$product->setName('Product Name');
$product->getResource()->saveAttribute($product, 'name');
Other option is to work directly on the database for updates rather than use magento objects.

how can i fetch cart data from joomla session?

Hi I am working on a module where i have to fetch cart data from the session. For that I am use below mention code. I am able to fetch the data but all in one. Now my question is how do I get those data individually(product_id individually cart id individually etc)? Please guide me regarding this issue.
Code
$_data=unserialize($_SESSION['__vm']['vmcart']);
print_r($_data);
did you check the vm cart module? your solution seams to be there.
try :
foreach ($_data->products as $product){
//var_dump($product);
echo $product->virtuemart_product_id.' || '.$product->product_name.' || '.$product->product_price;
}
I got the solution with help of below mentioned code
$cart = VirtueMartCart::getCart(); //getting cart object
$cmpny=$cart->BT; // accessing cart's elements
foreach($cmpny as $key =>$data)
{
some usfull code
}

Reindex Product after Cronjob in Magento - Configurable Products showing Out Of Stock

I have written a Extension that updates some custom shipping value Attributes on save for Magento 1.7. All working fine, when saving the product all is updated as it should. However I also have the need for a cronjob to update them each night in case I need to change shipping costs throught the board.
Is all working, and is updating the attribute values correctly, however on the frontend all configurable products are showing as Out of Stock, Simple Products are fine.
If I go to the admin, just click in the master product and save it without doing anything it shows back as In Stock on the frontend. Also if I go to Indexes and reindex Product Attributes it again shows as In stock on the frontend. I presume then that my cronjob needs to update the indexer on saving each product.
Looking around I used the following code, however it doesn't seem to update the product, and wondered if anyone could help. I have tried different variations around the Mage_Catalog_Model_Product and TYPE_SAVE but can't find what I am supposed to use!
$updateProduct = Mage::getModel('catalog/product')->load($_product->getId());
$updateProduct->setShippingLabel($shippData['delivery_type']);
$updateProduct->setShippingPrice($shippData['price']);
$updateProduct->setShippingNote($shippData['notes']);
try {
$updateProduct->save();
$updateProduct->setForceReindexRequired(true);
Mage::getSingleton('index/indexer')->processEntityAction(
$updateProduct,
Mage_Catalog_Model_Product::ENTITY,
Mage_Index_Model_Event::TYPE_SAVE
);
echo $updateProduct->getId()." Successfully Updated \n";
} catch(Exception $e){
echo $e->getMessage().$updateProduct->getId()."\n";
}
Update 17/5/2013 20:28
Have been playing with the code and this amendment seems to work, if it is totally useless and a stupid way of doing it please let me know
$updateProduct = Mage::getModel('catalog/product')->load($_product->getId());
$updateProduct->setShippingLabel($shippData['delivery_type']);
$updateProduct->setShippingPrice($shippData['price']);
$updateProduct->setShippingNote($shippData['notes']);
try {
$updateProduct->save();
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product->getId());
$stockItem->setForceReindexRequired(true);
Mage::getSingleton('index/indexer')->processEntityAction(
$stockItem,
Mage_CatalogInventory_Model_Stock_Item::ENTITY,
Mage_Index_Model_Event::TYPE_SAVE
);
echo $updateProduct->getId()." Successfully Updated \n";
} catch(Exception $e){
echo $e->getMessage().$updateProduct->getId()."\n";
}
After the execution of your cron job you can update your indexer:
$indexingProcesses = Mage::getSingleton('index/indexer')->getProcessesCollection();
foreach ($indexingProcesses as $process) {
$process->reindexEverything();
}
After the execution of your cron job you can update your indexer:
$indexingProcesses = Mage::getSingleton('index/indexer')->getProcessesCollection();
foreach ($indexingProcesses as $process) {
$process->reindexEverything();
}
hello dhawal if i have to run a cron job for to re-index product on site, I have to placed the code that you have mention with this code
$updateProduct = Mage::getModel('catalog/product')->load($_product->getId());
$updateProduct->`setShippingLabel`($shippData['delivery_type']);
$updateProduct->setShippingPrice($shippData['price']);
$updateProduct->setShippingNote($shippData['notes']);
try {
$updateProduct->save();
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product->getId());
$stockItem->setForceReindexRequired(true);
Mage::getSingleton('index/indexer')->processEntityAction(
$stockItem,
Mage_CatalogInventory_Model_Stock_Item::ENTITY,
Mage_Index_Model_Event::TYPE_SAVE
);
echo $updateProduct->getId()." Successfully Updated \n";
} catch(Exception $e){
echo $e->getMessage().$updateProduct->getId()."\n";
}
at same page ??

How to create an invoice for magento downloadable product?

I have an problem with invoice generating for my downloadable product.
I tried to create an invoice. But it shows like this error message Cannot create an invoice without products.
Can any one help to solve this issue.
Thanks
meaning that you have not selected the products that you need to invoice if you look at the code that gives this error in following file
app/code/core/Mage/Adminhtml/controllers/Sales/Order/InvoiceController.php:88: Mage::throwException($this->__('Cannot create an invoice without products.'));
you see that this error is given when no items are found
$savedQtys = $this->_getItemQtys();
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($savedQtys);
if (!$invoice->getTotalQty()) {
Mage::throwException($this->__('Cannot create an invoice without products.'));
}
and you can also see that the items are looked from your post
/**
* Get requested items qty's from request
*/
protected function _getItemQtys()
{
$data = $this->getRequest()->getParam('invoice');
if (isset($data['items'])) {
$qtys = $data['items'];
} else {
$qtys = array();
}
return $qtys;
}
meaning that you have not selected or posted whatever needs to be posted to this method.

Magento: Rebuilding Flat Catalog Programmatically

I am using a cron to import inventory changes nightly. When I try to change a product's information (price, etc) I get the following error:
Column not found: 1054 Unknown column 'e.display_price_group_0' in 'field list'
I can fix this by clicking "Rebuild Flat Catalog Product" in the Cache Management panel. I setup a cron to do this programmatically using the following code:
Mage :: getResourceModel( 'catalog/product_flat_indexer' ) -> rebuild();
I don't get any errors when I run the script, but the "Column not found" error persists.
Does anyone know how I can rebuild the flat catalog other than through the admin interface?
Previous I said to do this:
Mage::getModel('catalog/product_flat_indexer')->rebuild();
Note: it's getModel and NOT getResourceModel.
This is not true. Either works. However, I have found through a rather painful trial and error process that the flat product tables don't rebuild correctly unless I also rebuild the entire catalog. This is how I finally solved my problem:
Mage::getSingleton('catalog/index')->rebuild();
Mage::getResourceModel('catalog/product_flat_indexer')->rebuild();
Mage::getSingleton('catalog/url')->refreshRewrites();
Mage::getModel('catalog/product_image')->clearCache();
Mage::getSingleton('catalogsearch/fulltext')->rebuildIndex();
Mage::getSingleton('cataloginventory/stock_status')->rebuild();
$flag = Mage::getModel('catalogindex/catalog_index_flag')->loadSelf();
if ($flag->getState() == Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_RUNNING) {
$kill = Mage::getModel('catalogindex/catalog_index_kill_flag')->loadSelf();
$kill->setFlagData($flag->getFlagData())->save();
}
$flag->setState(Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_QUEUED)->save();
Mage::getSingleton('catalogindex/indexer')->plainReindex();
Basically, just rebuild everything. Don't worry about optimization. Like someone once said, "Premature optimization is the root of all evil."
I've found that there's a more efficient way to update only specific product attributes.
Mage::getModel('catalog/product_flat_indexer')->updateAttribute($attributeCode, null, $productIds);
Or you can update the entire product in the flat table:
Mage::getModel('catalog/product_flat_indexer')->updateProduct($productIds, null);
Where $productIds is an array of the product entity ids to update. These functions will also update other indexed data related to the products you update. Hope this helps.
See this script.
I personally had some trouble with it, but others seem to be quite happy with it.
If you don't want the whole thing, you can easily pull out the part that rebuilds the flat catalog product and point a cron job at it.
* Rebuild Catalog Index
Mage::getSingleton('catalog/index')->rebuild();
* Rebuild Flat Catalog Product
Mage::getResourceModel('catalog/product_flat_indexer')->rebuild();
* Inventory Stock
Mage::getSingleton('cataloginventory/stock_status')->rebuild();
public function rebuildIndexes(){
$processes = array();
$collection = Mage::getSingleton('index/indexer')->getProcessesCollection();
foreach ($collection as $process) {
try {
$process->reindexEverything();
$this->_message($process->getIndexer()->getName() . " index was rebuilt successfully");
} catch (Mage_Core_Exception $e) {
$this->_throwException($e->getMessage());
} catch (Exception $e) {
$this->_throwException($process->getIndexer()->getName() . " index process unknown error:\n" . $e);
}
}
}
people why dont you research a little bit before post somethig that doesn't work, open the shell/indexer.php, inside you will find all the answers related to indexing.
I also can't get it to work correctly.
When I rebuild the Rebuild Flat Catalog Product from the Admin it works fine and I don't get the SQL column error, but when I do it programmatically it doesn't work via:
Mage::getResourceModel('catalog/product_flat_indexer')->rebuild();
I have just written this code, based on the Shell reindex script.
I have tested it in Magento 1.5.1 using a web script (with long max_execution_time).
if ( !empty($_SERVER['HTTP_HOST']) )
{
header('Content-Type: text/plain');
}
$oIndexer = Mage::getSingleton('index/indexer');
/* #var $oIndexer Mage_Index_Model_Indexer */
$oProcessCollection = $oIndexer->getProcessesCollection();
/* #var $oProcessCollection Mage_Index_Model_Mysql4_Process_Collection */
foreach ( $oProcessCollection as $oProcess )
{
/* #var $oProcess Mage_Index_Model_Process */
echo 'Rebuilding ' . $oProcess->getIndexer()->getName() . ' index...';
outputFlush();
$oProcess->reindexEverything();
}
echo 'Done.';
outputFlush()
function outputFlush()
{
while ( ob_get_length() )
{
ob_end_flush();
}
if ( !empty($_SERVER['HTTP_HOST']) )
{
echo str_repeat(' ',4096);
}
echo "\n";
flush();
}

Resources