Using event and observer i have make auto subscription for customer but i want to do if customer has more than 2 order then customer should not auto subscribe to newsletter subscription.

Help me if you have any better suggestion.


  • but when customer placed order for a first time then customer autosubscribe to newsletter subscription then why you want to customer not subscribe when customer placed two or more order? i think after placed one order , next time customer should not autosubscribe. Commented Oct 4, 2018 at 10:36
  • when customer already subscribed then why you subscribed customer again? can you please explain more your requirement? Commented Oct 4, 2018 at 10:38
  • 1
    i have 10 years old customer and condition is that if they have already order before this and his/her subscription status not subscribe with newsletter and now they order for second time so their subscription status should not be changed. Commented Oct 4, 2018 at 10:38

2 Answers 2


Create a module with name STech_Autosubscription like below:

Step 1: Create file STech_Autosubscription.xml under:


with content:

<?xml version="1.0"?>

Step 2: Create file config.xml under:


with content:

<?xml version="1.0"?>

Step 3: Create file Observer.php under:


with content:

class STech_Autosubscription_Model_Observer
    public function autoSubscribe(Varien_Event_Observer $observer)
        $order = $observer->getEvent()->getOrder();
        $customerEmail = $order->getCustomerEmail();
        $orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_email', $customerEmail);
        if(count($orders) <= 2){

    protected function _autoSubscribe($email)


Flush the cache and test.

  • Thanks Sukmar Gorai, i have made same module then also not get actually output. Commented Oct 4, 2018 at 12:19
  • @sarfaraj, can you please share your output/result/error occurred after using above module.? Commented Oct 4, 2018 at 12:50
  • @shashank, there is not any error but when order completed the customer newsletter subscription check box checked automatic and this is not right. Commented Oct 4, 2018 at 13:13
  • @Sarfaraj if you used the same code, then $this->_autoSubscribe($customerEmail); should be not executed but according to you it's execute.then you should maintain logs so that you can ensure what data comes Commented Oct 4, 2018 at 13:40

i got your point.

use this sql query to get number of orders customer have

$result =   SELECT COUNT('customer_email') FROM  sales_order where customer_email = 'customer email address'; // this query give you no of record of order of customer

now check

if($result <= 2 ) {
    //here your autosubscrption code

Try This, i hope it would be helped you

  • thanks for reply , $_orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_email', $email); $_orderCnt = $_orders->count(); //orders count if ($_orderCnt == 1){ // subscribe function } but not working if order >= 2 then also auto subscribe. Commented Oct 4, 2018 at 11:53
  • $_orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_email', $email); $_orderCnt = $_orders->count(); //orders count if ($_orderCnt <= 2){ //this is your requirement // subscribe function } Change your if condition and make sure $_orderCnt gave correct value Commented Oct 4, 2018 at 12:02
  • this variable give me correct ordered count value. Commented Oct 4, 2018 at 12:20
  • have you modified your if condition. try this if($_orderCnt <= 2){ Commented Oct 4, 2018 at 12:34
  • send your code here. Commented Oct 4, 2018 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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