Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

New answers tagged

0

Did you try one of the suggestions in the magento git issue report? Only reason I ask is because he seems so certain. I'm away from computer at the moment so can't test. $customerData = $observer->getEvent()->getData('customer'); $newData = $customerData->getData('attriubute_code'); $previousData = $customerData->getOrigData('attribute_code')


0

$customer->getOrigData()  return null because it never loaded. Reference Link


0

I don't think you need an observer for that. Just add a conditon in your template (catalog/product/view.phtml), like this: //$_product is Mage_Catalog_Model_Product object <?php if ($this->displayProductStockStatus() && $_product->isAvailable()) :?> Stock is back <?php //echo $this->getLayout()->createBlock('cms/block')...


0

You can use an alternative way: Create an attribute say back_on_stock with type Yes/No. Change you event form catalog_product_save_commit_after to catalog_product_save_before. Now in observer you can set this new attribute value in your if condition, like: $product->setBackOnStock(1); return $this; Now in Your phtml, you can check this attribute ...


2

The observer is a class same as any other class in Magento and can be initialized via Dependency Injection in the constructor or via ObjectManager (not preferred). For Example: <?php namespace MyVendor\MyModule\Observer; use Magento\Framework\Event\ObserverInterface; class SaveObserver implements ObserverInterface { public function execute(\...


2

If you want to get order id on checkout_onepage_controller_success_action then you have to call $observer->getEvent()->getOrderIds() $observer->getEvent()->getOrderIds() provide an array which consist of one order id of single checkout and provide multiple order ids. <?php nameSpace YourNameSpace; use Magento\Framework\Event\Observer as ...


0

Its very easy, Please find below code. To get customer name find the code below. $customer = $observer->getData('data_object'); if ($customer) { $customerName = $customer->getFirstName(); } To get the order use the code below: $observer->getOrder()


0

To get order ID change your code like below : $order_ids = $observer->getEvent()->getOrderIds(); $order_id = $order_ids[0]; To get Shipping Adress load order: //\Magento\Sales\Api\OrderRepositoryInterface $_order = $this->orderRepository->get($order->getEntityId()); $shippingAddress = $_order->getShippingAddress(); $...


0

Try this $orderId=$observer->getEvent()->getOrderIds(); $order = $this->order->load($orderId); $orderid=$order->getEntityId(); /* Order id */ $quoteid=$order->getQuoteId(); $customer_first_name=$order->getCustomerFirstname(); $customer_last_name=$order->getCustomerLastname(); /* customer name */


0

I also found this now in the dev docs: https://devdocs.magento.com/guides/v2.1/coding-standards/technical-guidelines.html#14-events 14.1. All values (including objects) passed to an event MUST NOT be modified in the event observer. Instead, plugins SHOULD BE used for modifying the input or output of a function. So I think if we follow this rule, events ...


1

Depends how you look at it. Magento is (MySQL) transactional which means things are getting queued whilst tables are locked. They are then processed in a first come first serves basis when locks are released. Observers would also work on this basis. I don't believe community edition has anything more complex then that. I've read about enterprise edition ...


2

I think in you can mock your Observer class with getData method i.e $eventObserverMock = $this->getMockBuilder(Observer::class) ->setMethods(['getData']) ->disableOriginalConstructor() ->getMock(); Now getData method is mock so you can set the some value with return type which you want true or false ...


0

Try this code, It may be helpful to resolve your issue: public function execute(\Magento\Framework\Event\Observer $observer) { $item = $observer->getEvent()->getData('quote_item'); $item = ( $item->getParentItem() ? $item->getParentItem() : $item ); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $prodObj = $...


0

You Can Get Product Special Price Like This :- $price = $item->getProduct()->getSpecialPrice();


0

I found answer myself. Sharing here so it may help someone. We can get Order object inside execute method of observer as below: public function execute(\Magento\Framework\Event\Observer $observer) { $transport = $observer->getTransport(); $order=$transport['order']; $orderId=$order->getEntityId(); /** We can call any ...


0

Turned out Mage_ProductAlert was false in /app/etc/modules/Mage_All.xml. Change to true and the error not showing anymore.


0

productalert/observer::process no longer exists. Search codebase for productalert. XML is making magento look for [Vendor]_[Extension]_Model_Observer public function process() { productalert will be in XML. https://devdocs.magento.com/guides/m1x/magefordev/mage-for-dev-5.html There is a refence to this event in XML too under <events> node in XML ...


1

Save Product Using ProductRepositoryInterface :- $option = $this->_objectManager->create(\Magento\Catalog\Model\Product\Option::class) ->setProductId($_product->getId()) ->setStoreId($_product->getStoreId()) ->addData($arrayOption); $option->save(); $product->addOption($option); $productRepo = $...


1

You can try using a Product Repository Magento\Catalog\Api\ProductCustomOptionRepositoryInterface Like this: $product->setHasOptions(1); $product->setCanSaveCustomOptions(true); foreach ($options as $arrayOption) { $isHasOptions = $product->getTypeInstance()->hasOptions($product); $option = \Magento\Framework\App\ObjectManager::...


2

As per your requirement, you have mentioned that the functionality of the observer has nothing to do with the observer argument and is independent. In this case, according to me, the best solution is to create and call a single observer file and function. The main benefit of it is that if in future there are some changes, change needs to be in one single ...


3

Logically I don't see any benefit of creating multiple classes for same functionality. Moreover having same class it will help in code redundancy, upgrading and also debugging. In case of Plugin you should have different class defined even if the functionality is same . But in case of observer it would be better if you use the same class in my opinion.


0

In my option, it is the best idea to a create Class on Model and also define a Method. After that, you have to call that method on your observer classes. Model Class: namespace {Vendor}\{ModuleName}\Model; class {MyClass} { public function myFunction() { // do whatever you need } } After that called myFunction() on MyObserver using ...


1

Saving happens in a MySQL transaction and the delete_after event is triggered before the transaction is committed, so that you can do additional updates in the database within the same transaction. The delete_commit_after event is triggered after the transaction has been committed, i.e. when the changes were written to the database.


0

According to Magento technical guide (https://devdocs.magento.com/guides/v2.1/coding-standards/technical-guidelines.html#14-events): All values (including objects) passed to an event MUST NOT be modified in the event observer. Instead, plugins SHOULD BE used for modifying the input or output of a function. For me the main difference between plugins and ...


1

Above answer is not working for me I have updated my observer code public function execute(\Magento\Framework\Event\Observer $observer) { $params = []; // If you want to redirect with data // $url = $this->_urlInterface->getUrl('checkout/cart/index',$params); $url = $this->_urlInterface->getUrl('checkout/...


0

I had a problem with the sales_order_save_after event as it triggers twice after orders are placed. Maybe that's why your saveData() function tries to save data twice. Try sales_order_place_after instead.


0

This was an issue with the store id, it shouldn't to be get from the identityContainer, this is my solution $url = \Magento\Framework\App\ObjectManager::getInstance(); $storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface'); $websiteId = $storeManager->getWebsite()->getWebsiteId(); // Get Store ID ...


1

It is because you have printed information in the observer. I think it is not because you have used getRequest() in the observer. Try to see the result in this way. $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log'); $logger = new \Zend\Log\Logger(); $logger->addWriter($writer); $logger->info(print_r($this->request->getParams(), ...


1

Did you try with : //\Magento\Framework\App\RequestInterface $this->_request->getParam('field_name'); Its working for me. Hope it will work for you too.


0

Try this way: /** * @var \Magento\Framework\App\RequestInterface */ public $request; public function __construct( ... \Magento\Framework\App\RequestInterface $request ){ ... $this->request = $request; } public function execute(Observer $observer) { $post = $this->request->getPost(); /* or */ $param = $...


1

Try using \Magento\Framework\App\RequestInterface::getParams() In replacement for: \Magento\Framework\App\Request\Http::getParams()


Top 50 recent answers are included