Category of Products with Observer condition Error?

Condition: I have restricted category of products [category id = 680, 894, 895], this category of products should be purchase alone, not mixed with other products.

Workout: Case 1: If cart had other products, if the customer tries to add restricted category products trigger observer like not eligible to add to cart and display a message like If you want this product, Purchase alone not mixed with other Products

case 2: If cart had a restricted category of products if customer try to add non-restricted products trigger observer like not eligible to add to cart and display a message like Cart has Special Product you can not add another

code :


<?xml version="1.0" encoding="UTF-8"?> <config>     <modules>         <Gta_KolupadiRestrict>             <active>true</active>             <codepool>local</codepool>         </Gta_KolupadiRestrict>     </modules> </config> 


<?xml version="1.0"?> <config>     <modules>         <Gta_KolupadiRestrict>             <version>1.0.0</version>         </Gta_KolupadiRestrict>     </modules>     <global>          <models>             <gta_kolupadirestrict>                 <class>Gta_KolupadiRestrict_Model</class>             </gta_kolupadirestrict>         </models>         <events>             <checkout_cart_product_add_after>                 <observers>                     <Gta_KolupadiRestrict_Model_Observer>                            <type>singleton</type>                         <class>Kolupadi_Restrict_Model_Observer</class>                         <method>cartevent</method>                     </Gta_KolupadiRestrict_Model_Observer>                 </observers>             </checkout_cart_product_add_after>               </events>     </global>    </config> 


<?php      Mage::log('fine dude', null, 'logfile.log');     //create class     class Gta_KolupadiRestrict_Model_Observer     {          //create function         public function cartevent(Varien_Event_Observer $  observer)         {             $  category_id = array(680, 894, 895) ; //category ids              $  category_products  = Mage::getModel('catalog/category')                                         ->setWebsiteId(2);           // load website id                                         ->load($  category_id);        // load category               // check cart qty status                                         $  cart_qty = (int) Mage::getModel('checkout/cart')->getQuote()->getItemQty();                //logic             if($  category_products && $  cart_qty > 0  )             {                 Mage::throwException("If you want Kolu Padi, Purchase alone not mixed with other Products");                 }               //check if cart have products               $  quote = Mage::getModel('checkout/cart')->getQuote();              foreach($  quote->getAllItems() as $  item)              {                 $  productId  = $  item->getCategoryId();                  if($  productId == $  category_id)                 {                     Mage::throwException("Cart has Special Product you can not add another");                 }              }               }            } ?> 

Observer not trigger. Does anyone help me?

Magento 2.3: How to get tracking info from an order in observer

We use ShipStation to send tracking information to Magento and I’m trying to retrieve the order shipping / tracking information once this is saved to the order.

If I use sales_order_shipment_track_save_after for my event / observer, I can get the tracking information, however, it doesn’t seem like this event is called when updated via ShipStation api.

If I change the event to sales_order_shipment_save_after, this gets triggered by ShipStation but it doesn’t appear that I can get tracking information from this event.

Any ideas how I can get tracking information when it’s updated via api from ShipStation?

What’s the difference between Observer pattern and listeners?

I have used some kind of “listeners” where I have an interface implemented by classes that need to be notified of some event (e.g.: CurrencyListener, with a method currencyUpdated(Currency currency)) Then, the object that needs to send a notification, has a list of listeners (List) and just iterates over this list invoking the currencyUpdated(Currency currency) method.

The structure of this listener, is very similar to the Observer pattern, there is no Observer and Observable class, just the listener interface(CurrencyListener).

What are the advantages/disadvantages of using one approach over the other?

Magento 2 how to get Postcode from billing address in observer?

I cannot get the postcode from Users’s billing Address,this is my code, everything works fine except for the postcode, which i’m not able to pull.

$  objectManager = \Magento\Framework\App\ObjectManager::getInstance();          $  customer = $  observer->getEvent()->getCustomer();         $  customer_email=$  customer->getEmail();         $  first_name= $  customer->getFirstname();         $  last_name= $  customer->getLastname();         $  taxVat = $  customer->getTaxvat();           //Billing Address variables          $  billingID = $  customer->getDefaultBilling();         $  billingAddress = $  objectManager->create('Magento\Customer\Model\Address')->load($  billingID);         $  billingCompany = $  billingAddress->getCompany();         $  billingZipcode = $  billingAddress->getPostCode();         $  billingCity = $  billingAddress->getCity();         $  billingState = $  billingAddress->getRegion();         $  billingStreet = $  billingAddress->getStreet();         $  billingStreet1 = $  billingStreet[0];         $  billingStreet2 = $  billingStreet[1];         $  billingTelephone = $  billingAddress->getTelephone(); 


Magento 2 – Event Observer for user has downloaded downloadable product

I want to fire an action when a user has downloaded a downloadable product (i.e. they have clicked the link for the download). I don’t know what event I should be observing to do this. I tried to search through the list of Events here:

Magento 2 Events List and How to Use Them

But none of them seem to correspond to the event I’m looking for. Any ideas on what event corresponds to this? Or another way of observing this action? (i.e. via plugin perhaps?)

Magento Observer to block spam orders not working

I’m facing some issues with orders placed by bots.

enter image description here

I noticed that all orders are made by a specific phone number and also by a specific street. So I looked for a way to block orders that are made with that phone number or this street.

I found the following link
I followed the steps to create an observer with the event sales_order_place_before, to compare the parameters sent in the order to the parameters that I want to block.

  1. I created the file Blockbots_Observer.xml on /html/app/etc/modules;
  2. I created the file Config.xml on /html/app/code/local/Blockbots/Observer/etc;
  3. Finally, I created the file Observer.php on /html/app/code/local/Blockbots/Observer/Model;

After all these steps, the implementation did not work (I am still receiving orders from bots).

Here is the code on files:

Blockbots_observer.xml: enter image description here

Config.xml: enter image description here

Observer.php: enter image description here

What could be wrong with implementation? Any idea how I can fix it?

Custom product Attribute in observer Magento 2

Anyone know how to get product custom attribute in observer:

My current code is giving me:

Uncaught Error: Call to a member function getProduct() on null in    $  product = $  this->productRepository->getById($  observer->getItem()->getProduct()->getId());         $  attr = $  product->getData('product_customattribute'); 

Also I tried this (same error):

 $  product = $  observer->getEvent()->getProduct();    $  attr = $  product->getProduct()->getAttributeText('product_customattribute'); 

Magento 2 observer not working

I have added a custom step in checkout which has a form, I need an observer to save that data to the database. I have tried using sales_model_service_quote_submit_before and sales_order_place_before but neither executes the observer.

Which event should I use? What are the other ways to save data from a custom step?