2
namespace send\sms\Cron; 

use Magento\Framework\ObjectManagerInterface;

class Run  /*extends send\sms\Controller\Customr*/  {

   public function execute()
   {

    $om = \Magento\Framework\App\ObjectManager::getInstance();
     $customerSession = $om->get('Magento\Customer\Model\Session');
     if($customerSession->isLoggedIn()) {

         $idd =$customerSession->getCustomer()->getId();

     }
       $this->_resources = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\App\ResourceConnection');  
    $connection= $this->_resources->getConnection();
    //$sql = "UPDATE `total_cashback` SET is_active= 100 WHERE user_id = '801' ";  
     $sql = "UPDATE `wallet_discounts` SET is_active = 12000 WHERE customer_id = $idd ";  
    $connection->query($sql);
   }



}

Cannot get customer Id in the above snippet.

  • Can you tell me what you are trying to achive using above code? – Dhiren Vasoya Jan 9 '18 at 12:52
  • I'm trying to update is_active column for logged in customer. But cannot get customer id in cron jobs.plz help me – Ramesh KR Jan 9 '18 at 12:59
  • You can not access customer session in cron. You could try to create an observer for 'customer_login' and do your logic within the observer. Eg in M1 magento.stackexchange.com/questions/58947/… – Renon Stewart Jan 9 '18 at 13:23
  • I'm not experienced in M1. So can you please tell me how to use cron in observers – Ramesh KR Jan 9 '18 at 13:31
1

You can not access customer session in cron (not impossible, but very hard to do). You could try creating an observer for customer_login and add your custom logic there.

To create an observer for customer_login

in app/code/Send/Sms/etc/frontend/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_login">
        <observer name="send_sms_customer_login" instance="Send\Sms\Observer\Frontend\CustomerLoginObserver"/>
    </event>
</config>

In Send\Sms\Observer\Frontend\CustomerLoginObserver.php

<?php
namespace Send\Sms\Observer\Frontend;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;


class CustomerLoginObserver implements ObserverInterface
{

     * @param EventObserver $observer
     * @return void
     */
    public function execute(EventObserver $observer)
    {
        $customerId = $observer->getCustomer()->getId()
        //custom logic here
    }
}

If you want to go the cron route, you could take a look at how Magento implement the "Customers Now Online" feature in Admin -> Customers -> Now Online.

  • Thanks for your answer. Really helpful. But I need to get id of the user from session and make a crob job function for that specific user. – Ramesh KR Jan 10 '18 at 7:10
  • 1
    You could use the above logic to store information into a db and then run a cron against that db. – Renon Stewart Jan 10 '18 at 15:08

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.