Programmatic Possibilities to work with the Extension

Modified on Fri, 20 Sep at 12:36 PM

M2E Pro works based on core Magento functionality. The Module identifies all of the product updates made through the Magento interface and synchronizes them on the Channels. 

It is also possible to update Magento Product data (quantity, price, status, etc.) without using core Magento functionality. But M2E Pro will not detect the changes in case they are made via:

  • direct SQL injections

  • various import/export/update tools.

There are two programmatic methods to keep M2E Pro notified about Magento Product data changes:

  • Object method (model) observes the changes made in all Magento Product Attributes and informs M2E Pro about them. 

  • Structural method (model) tracks the changes only in the most crucial Magento Product Attributes, such as Price, Quantity, and Status. Then it notifies M2E Pro about these changes.

To find the models in your Magento File system, follow this path:

app/code/Ess/M2ePro/PublicServices

If you update Magento Product information with the help of the Magmi import tool, we recommend using a dedicated M2E Pro plug-in. It allows reflecting the product updates correctly.

Object method

The Object method allows you to track changes in all attributes used in Magento Product settings. It triggers automated adding/removal of Products from M2E Pro Listings (Auto Add/Remove Rules), as well as List, Relist, Stop, and all available options of Revise Rules in Synchronization Policy.

This method starts picking up changes from the moment you register Magento Products (i.e. add them for observation).

Code snippet for creating an M2E Pro Object model

The code snippet below shows how an M2E Pro Object Model is created.

// $this->_objectManager instance of \Magento\Framework\ObjectManagerInterface

$model = $this->_objectManager->create('\Ess\M2ePro\PublicServices\Product\ObjectChange');

// you have a product ID for observing

$model->observeProduct(561);

// you have '\Magento\Catalog\Model\Product' object for observing

$product = $this->productFactory->create();

$product->load(561);

$model->observeProduct($product);

// make changes to these products via direct sql

$model->applyChanges();

How to use the Object method

Follow these steps in order to properly implement an M2E Pro Object model and make it inform the extension about all Magento Product data changes. Before you start, examine the code snippet above.

Step 1. Create an M2E Pro Object model. 

Step 2. Register the Magento Products that you want to observe changes in. There are two ways to do it – either use the $model->observeProduct method and pass Product ID through it or use an object of the class ‘\Magento\Catalog\Model\Product’.

Step 3. Once the Products have been registered, you can manipulate data for these products using SQL injections or by any other means.

Step 4. Initiate the applyChanges() method for all the registered products. While this method is running, M2E Pro will automatically observe all the product changes that have originated outside of Magento Core Models.

Important! We do not recommend creating an individual M2E Pro Object model for each product. The best practice here is to register all the required Magento products, make the necessary changes, and initiate the applyChanges() method for all these products at the end. This way, you will make one injection, instead of numerous ones for each product individually.

Also, the Object method is not suitable for a large number of product changes (i.e. in 10.000’s) since it is very resource-consuming. This method observes changes in all Magento Attributes and slows down your code execution. As a result, your import/update tools (if any) will work at poor speeds.

Structural method

Using the Structural method, you can inform M2E Pro about changes made to particular Attributes of your Magento Products, such as Price, Quantity, and Status.

This method requires only notifications confirming changes in certain Magento Product data.

Structural method triggers the following automatic actions of M2E Pro: 

  • List, Relist, Stop Rules of Synchronization Policy

  • Price/Quantity Revise Rules

The rest of the automatic actions will not be initiated if you use the Structural model.

Code snippet for creating an M2E Pro Structural model

The code snippet below shows how an M2E Pro Structural Model is created in regard to Price, Quantity, and Status changes.

// $this->_objectManager instance of \Magento\Framework\ObjectManagerInterface

$model = $this->_objectManager->create('\Ess\M2ePro\PublicServices\Product\SqlChange');

// notify M2E Pro about some change in product with ID 17

$model->markProductChanged(17);

// make price change to product with ID 18 and then notify M2E Pro

$model->markPriceWasChanged(18);

// make QTY change to product with ID 19 and then notify M2E Pro

$model->markQtyWasChanged(19);

// make status change to product with ID 20 and then notify M2E Pro

$model->markStatusWasChanged(20);

// make attribute 'custom_attribute_code' value change from 'old' to 'new' in product with ID 21

// in store with ID 1 and then notify M2E Pro

$model->markProductAttributeChanged(21, 'custom_attribute_code', 1, 'old', 'new');

$model->applyChanges();

How to use the Structural method

Follow these steps in order to properly implement an M2E Pro Structural model and make it notify the extension about the particular Magento Product data changes. Before you start, examine the code snippet above.

Step 1. Create an M2E Pro Structural model. You can choose to detect changes in either of two ways:

  • $model->markPriceWasChanged / $model->markQtyWasChanged / $model->markStatusWasChanged – if you need to manipulate data only in particular Magento Attributes

  • $model->markProductChanged – if you need to manipulate data in all three Magento Attributes at once

Step 2. Make changes in Magento Product Attributes (for example, using direct SQL requests).

Step 3. Initiate the applyChanges() method of the Structural model for all products. While this method is running, M2E Pro will automatically observe all the product changes that have originated outside of Magento Core Models.

Important! We do not recommend creating an individual M2E Pro Structural model for each product. The best practice here is to create one model for all the required Magento products, make the necessary changes, and initiate the applyChanges() method for all these products at the end. This way, you will make one injection, instead of numerous ones for each product individually.

Always use the Structural model for a large number of Magento Products’ changes. Compared with the Object method, the Structural one works faster and is not resource-consuming. 

Do not apply this method if your system is making use of all automatic M2E Pro actions.

Instead of programmatic methods, you can enable the Track Direct Database Changes option to detect Magento Product Price and Quantity changes made without using core Magento functionality. Click here to learn about this option in detail.

Object method vs Structural method

This comparison may help you figure out which method will work best depending on your needs, the number of Magento Products, and the kind of changes you make, available system resources, etc.

Criterion

Object method

Structural method

Criterion

Object method

Structural method

Magento Product data changes available for tracking

Each attribute used in your Magento Product settings

Only Price, Quantity and Status attributes in Magento

Appropriate for large Magento Inventories 

No

Yes

Difficulty level

Easier to implement

More complex, requires in-depth technical skills

Operating speed

Slow

Faster

Impact on system resources

Resource-intensive

Does not create a considerable load on the system

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article