0

I need to send SMS (By using any SMS API) to customers when products are back in stock.

I have rewrite Mage_ProductAlert_Model_Observer class and added SMS API in my Custom Module's Observer.php file but its not sending SMS to customers. Why?

Also, its only showing two customers details instead of all customers subscribed to product stock alert. Here is my code

Observer.php

<?php
class My_Alert_Model_Productalert_Observer extends Mage_ProductAlert_Model_Observer
{
         * Error email template configuration
     */
    const XML_PATH_ERROR_TEMPLATE   = 'catalog/productalert_cron/error_email_template';

    /**
     * Error email identity configuration
     */
    const XML_PATH_ERROR_IDENTITY   = 'catalog/productalert_cron/error_email_identity';

    /**
     * 'Send error emails to' configuration
     */
    const XML_PATH_ERROR_RECIPIENT  = 'catalog/productalert_cron/error_email';

    /**
     * Allow price alert
     *
     */
    const XML_PATH_PRICE_ALLOW      = 'catalog/productalert/allow_price';

    /**
     * Allow stock alert
     *
     */
    const XML_PATH_STOCK_ALLOW      = 'catalog/productalert/allow_stock';

    /**
     * Website collection array
     *
     * @var array
     */
    protected $_websites;

    /**
     * Warning (exception) errors array
     *
     * @var array
     */
    protected $_errors = array();

    /**
     * Retrieve website collection array
     *
     * @return array
     */
    protected function _processStock(Mage_ProductAlert_Model_Email $email)
    {
        $email->setType('stock');
        $originalStore = Mage::app()->getStore();

        foreach ($this->_getWebsites() as $website) {
            /* @var $website Mage_Core_Model_Website */

            if (!$website->getDefaultGroup() || !$website->getDefaultGroup()->getDefaultStore()) {
                continue;
            }
            if (!Mage::getStoreConfig(
                self::XML_PATH_STOCK_ALLOW,
                $website->getDefaultGroup()->getDefaultStore()->getId()
            )) {
                continue;
            }
            try {
                $collection = Mage::getModel('productalert/stock')
                    ->getCollection()
                    ->addWebsiteFilter($website->getId())
                    ->addStatusFilter(0)
                    ->setCustomerOrder();
            }
            catch (Exception $e) {
                $this->_errors[] = $e->getMessage();
                return $this;
            }

            $previousCustomer = null;
            $email->setWebsite($website);
            Mage::app()->setCurrentStore($website->getDefaultGroup()->getDefaultStore());
            foreach ($collection as $alert) {
                try {
                    if (!$previousCustomer || $previousCustomer->getId() != $alert->getCustomerId()) {
                        $customer = Mage::getModel('customer/customer')->load($alert->getCustomerId());
                        if ($previousCustomer) {

                            $email->send();
                        }
                        if (!$customer) {
                            continue;
                        }
                        $previousCustomer = $customer;
                        $email->clean();
                        $email->setCustomer($customer);
                    }
                    else {
                        $customer = $previousCustomer;
                    }

                    $product = Mage::getModel('catalog/product')
                        ->setStoreId($website->getDefaultStore()->getId())
                        ->load($alert->getProductId());
                    /* @var $product Mage_Catalog_Model_Product */
                    if (!$product) {
                        continue;
                    }
                    //FIRE SMS

                    $product->setCustomerGroupId($customer->getGroupId());

                    if ($product->isSalable()) {
                        $email->addStockProduct($product);

                        $alert->setSendDate(Mage::getModel('core/date')->gmtDate());
                        $alert->setSendCount($alert->getSendCount() + 1);
                        $alert->setStatus(1);
                        $alert->save();
                    }
                }
                catch (Exception $e) {
                    $this->_errors[] = $e->getMessage();
                }
            }

            if ($previousCustomer) {
                try {
                    $email->send();
                }
                catch (Exception $e) {
                    $this->_errors[] = $e->getMessage();
                }
            }
        }
        Mage::app()->setCurrentStore($originalStore);

        return $this;
    }

public function process()
{
     $email = Mage::getModel('productalert/email');
    /* @var $email Mage_ProductAlert_Model_Email */
    $this->_processStock($email);

$customer_stock_alerts = Mage::getModel('productalert/stock')
                    ->getCollection()
                    ->addStatusFilter(0)
                    ->setCustomerOrder();

foreach ($customer_stock_alerts as $alert){

  $stock_back_product = $alert->getProductId();
  $current_product=Mage::getModel('catalog/product')->load($stock_back_product);
  $current_product_name = $current_product->getName();

  $customer_info = Mage::getModel('customer/customer')->load($alert->getCustomerId());
  $customer_name = $customer_info->getPrimaryBillingAddress()->getFirstname();
  $customer_mobile = $customer_info->getPrimaryBillingAddress()->getTelephone(); 

        $message = 0;


        $message = "Dear $customer_name, Product $current_product_name  is back in Stock";

        $message=urlencode($message);


$sendsms = "http://api.msg91.com/api/sendhttp.php?sender=MSGIND&route=4&mobiles=$customer_mobile&authkey=xxxxxxxxxxxxxxxxxxx&country=91&message=$message";


       file_get_contents($sendsms);     
}


}

}

config.xml

<?xml version="1.0"?>
<config>
<modules>
<My_Alert>
<version>0.0.1</version>
</My_Alert>
</modules>
<global>
  <models>
    <my_alert>
      <class>My_Alert_Model</class>
      <resourceModel>alert_mysql4</resourceModel>
    </my_alert>
            <productalert>
                <rewrite>
                    <observer>My_Alert_Model_Productalert_Observer</observer>
                </rewrite>
            </productalert>
  </models>
</global>
    <frontend>
        <routers>
            <My_Alert_First>
                <use>standard</use>
                <args>
                    <module>My_Alert</module>
                    <frontName>my-alert</frontName>
                </args>
            </My_Alert_First>
        </routers>
    </frontend>    
</config>

Can anyone share what's the issue this & why SMS is not sending?

Thanks.

  • Try to add your log when you call API for SMS what error you are getting ? – Dhaval Solanki Jan 25 '18 at 4:01
  • I have already set shortest cron time for every five minutes and its working fine. So confirmed that product alert mails are sending to customers without any issues. But only thing is I am not receiving SMS. Why? – Mesk Jan 25 '18 at 7:47
  • I have created custom module. Please check my updated question. Still SMS is not sending. – Mesk Jan 25 '18 at 9:50
  • Try simple extension magecomp.com/magento-sms-notification.html – Gaurav Jain Jun 15 '18 at 11:03
  • I am not asking this. Read my question again. – Mesk Jul 6 '18 at 3:47
2

First of all, you are editing a core file in Magento which is surely big NO NO. Instead, you really need to create a module for this. However, giving explanation for creating a module is surely an out of scope job and hence I am not doing it here.

The Mage_ProductAlert_Model_Observer::process() method is triggered as a cron job in Magento. Hence, the first thing that you need to check here is whether the cron in your Magento instance is configured correctly and make sure it works.

The cron time for the product alert can be set in backend: System > Configuration > Catalog > Catalog > Product Alerts. Try to put a short time here and thus check whether the process() is working or not by put some log in that file.

Please refer this answer for more details regarding the debugging of the product alert

  • For your information I have already set shortest cron time for every five minutes and its working fine. So confirmed that product alert mails are sending to customers without any issues. But only thing is I am not receiving SMS. Why? – Mesk Jan 25 '18 at 7:22
  • I have created custom module. Please check my updated question. Still SMS is not sending. – Mesk Jan 25 '18 at 9:49
0

You need to rewrite the class Mage_ProductAlert_Model_Observer

https://code.tutsplus.com/tutorials/understand-overriding-in-magento-models--cms-23354

<?xml version="1.0"?>
<config>
  <modules>
    <My_Alert>
      <version>0.0.1</version>
    </My_Alert>
  </modules>
  <global>
    <models>
      <my_alert>
        <class>My_Alert_Model</class>
        <resourceModel>alert_mysql4</resourceModel>
      </my_alert>
            <productalert>
                <rewrite>
                    <observer>My_Alert_Model_Productalert_Observer</observer>
                </rewrite>
            </productalert>
    </models>
    ......
  </global>
  ..
</config>

And overwrite the function _processStock

<?php
class My_Alert_Model_Productalert_Observer extends Mage_ProductAlert_Model_Observer
{
         * Error email template configuration
     */
    const XML_PATH_ERROR_TEMPLATE   = 'catalog/productalert_cron/error_email_template';

    /**
     * Error email identity configuration
     */
    const XML_PATH_ERROR_IDENTITY   = 'catalog/productalert_cron/error_email_identity';

    /**
     * 'Send error emails to' configuration
     */
    const XML_PATH_ERROR_RECIPIENT  = 'catalog/productalert_cron/error_email';

    /**
     * Allow price alert
     *
     */
    const XML_PATH_PRICE_ALLOW      = 'catalog/productalert/allow_price';

    /**
     * Allow stock alert
     *
     */
    const XML_PATH_STOCK_ALLOW      = 'catalog/productalert/allow_stock';

    /**
     * Website collection array
     *
     * @var array
     */
    protected $_websites;

    /**
     * Warning (exception) errors array
     *
     * @var array
     */
    protected $_errors = array();

    /**
     * Retrieve website collection array
     *
     * @return array
     */
    protected function _processStock(Mage_ProductAlert_Model_Email $email)
    {
        $email->setType('stock');
        $originalStore = Mage::app()->getStore();

        foreach ($this->_getWebsites() as $website) {
            /* @var $website Mage_Core_Model_Website */

            if (!$website->getDefaultGroup() || !$website->getDefaultGroup()->getDefaultStore()) {
                continue;
            }
            if (!Mage::getStoreConfig(
                self::XML_PATH_STOCK_ALLOW,
                $website->getDefaultGroup()->getDefaultStore()->getId()
            )) {
                continue;
            }
            try {
                $collection = Mage::getModel('productalert/stock')
                    ->getCollection()
                    ->addWebsiteFilter($website->getId())
                    ->addStatusFilter(0)
                    ->setCustomerOrder();
            }
            catch (Exception $e) {
                $this->_errors[] = $e->getMessage();
                return $this;
            }

            $previousCustomer = null;
            $email->setWebsite($website);
            Mage::app()->setCurrentStore($website->getDefaultGroup()->getDefaultStore());
            foreach ($collection as $alert) {
                try {
                    if (!$previousCustomer || $previousCustomer->getId() != $alert->getCustomerId()) {
                        $customer = Mage::getModel('customer/customer')->load($alert->getCustomerId());
                        if ($previousCustomer) {

                            $email->send();
                        }
                        if (!$customer) {
                            continue;
                        }
                        $previousCustomer = $customer;
                        $email->clean();
                        $email->setCustomer($customer);
                    }
                    else {
                        $customer = $previousCustomer;
                    }

                    $product = Mage::getModel('catalog/product')
                        ->setStoreId($website->getDefaultStore()->getId())
                        ->load($alert->getProductId());
                    /* @var $product Mage_Catalog_Model_Product */
                    if (!$product) {
                        continue;
                    }
                    //FIRE SMS

                    $product->setCustomerGroupId($customer->getGroupId());

                    if ($product->isSalable()) {
                        $email->addStockProduct($product);

                        $alert->setSendDate(Mage::getModel('core/date')->gmtDate());
                        $alert->setSendCount($alert->getSendCount() + 1);
                        $alert->setStatus(1);
                        $alert->save();
                    }
                }
                catch (Exception $e) {
                    $this->_errors[] = $e->getMessage();
                }
            }

            if ($previousCustomer) {
                try {
                    $email->send();
                }
                catch (Exception $e) {
                    $this->_errors[] = $e->getMessage();
                }
            }
        }
        Mage::app()->setCurrentStore($originalStore);

        return $this;
    }
}

Donot need set another Cronjob by below code:

   <crontab>
        <jobs>
            <my_alert>
                <schedule>
                    <cron_expr>*/5 * * * *</cron_expr>
                </schedule>
                <run>
                    <model>my_alert_productalert/observer::process</model>
                </run>
            </my_alert>
        </jobs>
    </crontab> 
  • I have tested with your code but its not working. Please check my updated question. – Mesk Jan 29 '18 at 14:10

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.