0

I want to save some customer's custom attributes after Order is placed By checking order item. So i am using 'sales_order_place_after' event, But in Observer Class I could Not Save it See My Code :

  namespace MagentoPro\Customer\Observer;

  use Magento\Customer\Api\CustomerRepositoryInterface;
  use Magento\Catalog\Model\Product;
  use Magento\Framework\Event\Observer;

  class SaveCompanySubscription implements \Magento\Framework\Event\ObserverInterface
  {
      public function __construct(
          CustomerRepositoryInterface $customer,
          Product $product
      ) {
           $this->product = $product; 
           $this->_customerRepositoryInterface = $customer;    
      }

      public function execute(Observer $observer)
      {
           $order = $observer->getEvent()->getOrder();
           $orderId = $order->getIncrementId();
           $customerId = $order->getCustomerId();
           $itemCollection = $order->getAllItems();

           foreach($itemCollection as $item){

               $productId = $item->getProductId(); 
               $prod = $this->product->load($productId);

               $is_subscribed = 1;
               $subscirption_ends_on = date('Y-m-d', strtotime("+".$prod->getData('package_validity')." days"));
               $subscription_type = $prod->getData('package_type');

               $customer = $this->_customerRepositoryInterface->getById($customerId);
               $customer->setCustomAttribute('is_subscribed_company', $is_subscribed);
               $customer->setCustomAttribute('subscription_ends_on', $subscription_ends_on);
               $customer->setCustomAttribute('subscription_type', $subscription_type);

               $this->_customerRepositoryInterface->save($customer);

           }

           return $this;

      }
  }
  • Try setData instead of the setCustomAttribute. – Amit Naraniwal Mar 5 '19 at 8:11
0

I got my answer. I used 'checkout_onepage_controller_success_action' event. and my observer class is :

    <?php
    /**
     * Saving Customer Data After Order Has Been Placed
     * Event : MagentoPro\Customer\etc\events.xml
    **/

    namespace MagentoPro\Customer\Observer;

    use Magento\Customer\Api\CustomerRepositoryInterface;
    use Magento\Catalog\Model\Product;
    use Magento\Sales\Model\Order;
    use Magento\Framework\Event\Observer;

    class SaveCompanySubscription implements \Magento\Framework\Event\ObserverInterface
    {
        protected $orderFactory;

        public function __construct(
            CustomerRepositoryInterface $customer,
            Product $product,
            Order $orderFactory
        ) {
             $this->orderFactory = $orderFactory;
             $this->product = $product; 
             $this->_customerRepositoryInterface = $customer;    
        }

        public function execute(Observer $observer)
        {
             $orderIds = $observer->getEvent()->getOrderIds();
             $lastorderId = $orderIds[0];
             $order = $this->orderFactory->load($lastorderId);
             $customerId = $order->getCustomerId();
             $itemCollection = $order->getAllItems();

             foreach($itemCollection as $item){

                 $productId = $item->getProductId(); 
                 $prod = $this->product->load($productId);

                 $is_subscribed = 1;
                 $subscription_ends_on = date('Y-m-d', strtotime("+".$prod->getData('package_validity')." days"));
                 $subscription_type = $prod->getData('package_type');

                 $customer = $this->_customerRepositoryInterface->getById($customerId);
                 $customer->setCustomAttribute('is_subscribed_company', $is_subscribed);
                 $customer->setCustomAttribute('subscription_ends_on', $subscription_ends_on);
                 $customer->setCustomAttribute('subscription_type', $subscription_type);

                 $this->_customerRepositoryInterface->save($customer);

             }

             return $this;

        }
    }
| improve this answer | |
0

Please use the below code to save custom customer attribute.

$customermodel = $objectManager->create ('Magento\Customer\Model\Customer');
$customerData = $customermodel->getDataModel();
$customerData->setId($order->getCustomerId());
$customerData->setCustomAttribute('customer_price_limit', $customerPriceLimit - $quote->getSubtotal());
$customermodel->updateData($customerData);

$customerResource = $objectManager->create('\Magento\Customer\Model\ResourceModel\CustomerFactory')->create();
$customerResource->saveAttribute($customermodel, 'customer_price_limit');
| improve this answer | |
  • Use of $objectmanager is not good and recommended. Please check? Our Answer should work. If any query? – Sujeet Pandit Sep 27 '18 at 11:36
  • I am using tthe below code and does not work for me. – Divya Muralidharan Sep 27 '18 at 12:33
-1

You should try following event:


checkout_submit_all_after

Now you can able to observe from backend order too.

And for paypal you need to use following one:


paypal_express_place_order_success

Get order information:


$order = $observer->getEvent()->getOrder();

For more

| improve this answer | |
  • magento.stackexchange.com/a/196398/59400 : Is this is Correct What I am Doing Please Check and Comment . I need help for That. Is 'checkout_onepage_controller_success_action' event is not fired Everytime when user Places an Order ?? – Sujeet Pandit Oct 9 '17 at 10:22

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.