I have created one function in Observer and set it called on product save after via config.xml file events section.
When I press "save & edit continue", this function log all the information twice.
What is the reason, why this happened?
Config.xml file code:
<global>
<events>
<catalog_product_save_after>
<observers>
<blogapi>
<type>singleton</type>
<class>testapi/observer</class>
<method>postProductAsPost</method>
</blogapi>
</observers>
</catalog_product_save_after>
</events>
</global>
Observer.php file code:
<?php
class Custom_Testapi_Model_Observer
{
const API_URL = "http://localhost/testsite/api/index.php/posts";
public function postProductAsPost(Varien_Event_Observer $observer)
{
$product = $observer->getEvent()->getProduct();
$postResponse = array();
$post_id = $name = $title = $conditionType = $condition = $productUrl = $imgUrl = $capacity = $sku = $shortDesc = $otherInfo = $postContent = '';
$metaInfo = $categories = array();
$updateImage = false;
$price = '';
if(!empty($product) && $product->getIsOption() != 1)
{
$sku = trim($product->getSku());
$shortDesc = strtoupper(trim($product->getShortDescription()));
$name = trim($product->getName());
$machineUrl = trim(Mage::getBaseUrl().$product->getUrlPath());
$imgUrl =(string)Mage::helper('catalog/image')->init($product, 'image')->resize(500);
$capacity = $product->getResource()->getAttribute('capacity')->getFrontend()->getValue($product);
$condition = trim($product->getCondition());
$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
$categories[] = $_cat->getName();
}
$metaTitle = trim($product->getMetaTitle());
$metaKeywords = trim($product->getMetaKeyword());
$metaDesc = trim($product->getMetaDescription());
$metaInfo = array('title' =>$metaTitle,'keywords'=>$metaKeywords,'description'=>$metaDesc);
$title = strtoupper(trim($capacity.$condition." ".$name));
if(!empty($shortDesc))
{
$postContent .= '<p>'.$shortDesc.'</p>';
}
if($product->getPrice() > 0 && $product->getPrice() != '')
{
$price = Mage::helper('core')->currency($product->getPrice(), true, false);
}
else
{
$price = "Call for price";
}
if($price != '')
{
$postContent .= '<p> Cost: '.$price.'</p>';
}
if(!empty($machineUrl))
{
$postContent .= '<p><a href="'.$productUrl.'" title="productUrl">'.$productUrl.'</a></p>';
}
$postDatas = array(
'title' => $title,
'categories' => $categories,
'machine_url' => $machineUrl,
'short_description' => $shortDesc,
'image_url' => $imgUrl,
'other_info' => $otherInfo,
'blog_post_content' => $postContent,
'meta_info'=>$metaInfo
);
/*if($post_id != '')
{
array_merge($postDatas,array('post_id'=>$post_id));
}*/
try {
$url = self::API_URL;
$curl = new Varien_Http_Adapter_Curl();
$curl->setConfig(array('timeout' => 0));
$headers= array('Content-Type: application/json','Authorization: Basic username:password');
$curl->write(Zend_Http_Client::POST, $url, '1.0', $headers, json_encode($postDatas));
$res = $curl->read();
$response = '';
$httpCode = Zend_Http_Response::extractCode($res);
$response = Zend_Http_Response::extractBody($res);
$curl->close();
if ($httpCode == 200) //if success
{
if (!empty($response)) {
Mage::log("Response -- " . print_r($response, true), null, 'blog.log', true);
}
}
}catch (Exception $e)
{
Mage::log("Error Msg -- " . $e->getMessage(), null, 'blog.log', true);
}
return;
}
else{
return;
}
}