0

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.

Thanks.

3
  • 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. Oct 4 '18 at 10:36
  • when customer already subscribed then why you subscribed customer again? can you please explain more your requirement? Oct 4 '18 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. Oct 4 '18 at 10:38
1

Create a module with name STech_Autosubscription like below:

Step 1: Create file STech_Autosubscription.xml under:

app/etc/modules/STech_Autosubscription.xml

with content:

<?xml version="1.0"?>
<config>
  <modules>
    <STech_Autosubscription>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.0.1</version>
    </STech_Autosubscription>
  </modules>
</config>

Step 2: Create file config.xml under:

app/code/local/STech/Autosubscription/etc/config.xml

with content:

<?xml version="1.0"?>
<config>
  <modules>
    <STech_Autosubscription>
      <version>0.0.1</version>
    </STech_Autosubscription>
  </modules>
  <global>
    <models>
      <autosubscription>
        <class>STech_Autosubscription_Model</class>
        <resourceModel>autosubscription_mysql4</resourceModel>
      </autosubscription>
    </models>
    <events>
      <checkout_onepage_controller_success_action>
        <observers>
          <checkout_onepage_controller_success_action_handler>
            <type>model</type>
            <class>autosubscription/observer</class>
            <method>autoSubscribe</method>
            <args></args>
          </checkout_onepage_controller_success_action_handler>
        </observers>
      </checkout_onepage_controller_success_action>
    </events>
  </global>
</config>

Step 3: Create file Observer.php under:

app/code/local/STech/Autosubscription/Model/Observer.php

with content:

<?php
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){
            $this->_autoSubscribe($customerEmail);
        }
    }

    protected function _autoSubscribe($email)
    {
        Mage::getModel('newsletter/subscriber')->subscribe($email);
    }    

}

Flush the cache and test.

4
  • Thanks Sukmar Gorai, i have made same module then also not get actually output. Oct 4 '18 at 12:19
  • @sarfaraj, can you please share your output/result/error occurred after using above module.? Oct 4 '18 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. Oct 4 '18 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 Oct 4 '18 at 13:40
0

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

7
  • 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. Oct 4 '18 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 Oct 4 '18 at 12:02
  • this variable give me correct ordered count value. Oct 4 '18 at 12:20
  • have you modified your if condition. try this if($_orderCnt <= 2){ Oct 4 '18 at 12:34
  • send your code here. Oct 4 '18 at 12:35

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.