oroinc / magento-orocrm-bridge

Magento extension, extends SOAP API for OroCRM integration.
http://www.magentocommerce.com/magento-connect/orocrm-bridge.html
16 stars 8 forks source link

Subscriber change_status_at updated everytime #15

Open luigifab opened 6 years ago

luigifab commented 6 years ago

In Oro_Api_Model_Observer::beforeNewsletterSubscriberSave() (for the _newsletter_subscriber_savebefore event), why you are updating change_status_at everytime?

aalgogiver commented 6 years ago

Hi, @luigifab38. AFAIK, this behavior is related to a bug in old versions of magento when this field wasn't updated automatically.

luigifab commented 6 years ago

Okay, but now there is a problem, every time I or Magento save the customer, the newsletter _change_statusat get a new date, even if no change occur.

Where do you use this field (_change_statusat)? In OroCRM? For synchronisation? Or just for information? Can I remove the Magento observer in Magento 1.9.3.2?

luigifab commented 6 years ago

So, here is a suggestion.

    public function beforeNewsletterSubscriberSave($observer)
    {
        $subscriber = $observer->getSubscriber();

        if (in_array($subscriber->getData('change_status_at'), array('', '0000-00-00 00:00:00', null))) {
            $now = new \DateTime('now', new \DateTimeZone('UTC'));
            $subscriber->setData('change_status_at', $now->format('Y-m-d H:i:s'));
        }
        else {
            $oldSubscriber = clone $subscriber;
            $oldSubscriber->load($subscriber->getId());
            if ($subscriber->getData('subscriber_status') != $oldSubscriber->getData('subscriber_status')) {
                $now = new \DateTime('now', new \DateTimeZone('UTC'));
                $subscriber->setData('change_status_at', $now->format('Y-m-d H:i:s'));
            }
        }
    }