0

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;
        }
    }
4
  • can you share observer.php file
    – Pramod
    Jul 10 '20 at 9:15
  • 1
    Maybe you call it twice. Share your code so we can see what's happening.
    – Marius
    Jul 10 '20 at 9:16
  • 1
    yes exactly @marius you are right, we cant assume anything without having a look at the code
    – Pramod
    Jul 10 '20 at 9:18
  • @Marius and Chikku, I have updated the question, kindly take a look.
    – Yogita
    Jul 10 '20 at 11:28
1

save and edit maybe triggers the product save twice. there's the same behavior saving it without continue?

you've tryed even using catalog_product_save_before event?

One thing you can do is to set a Flag in the execution so that he wont do your logic twice:

    if(Mage::registry('catalog_product_save_after_observer_executed')){
        return $this; //this method has already been executed once in this request
    }

    ...

    Mage::register('catalog_product_save_after_observer_executed',true); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.