Programmatic Possibilities to work with M2E TikTok Shop Connect

Modified on Wed, 22 Jan at 5:05 PM

M2E TikTok Shop Connect 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 TikTok Shop Connect 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 TikTok Shop Connect notified about Magento Product data changes:

  • Object method (model) observes the changes made in all Magento Product Attributes and informs M2E TikTok Shop Connect 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 TikTok Shop Connect about these changes.

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

app/code/M2E/TikTokShop/PublicServices

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 TikTok Shop Connect Listings (Auto Add/Remove Rules), as well as List, Relist, Stop, and all available options of Revise Rules in the 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 TikTok Shop Connect Object model

The code snippet below shows how an M2E TikTok Shop Connect Object Model is created.

// $this->_objectManager instanceof \Magento\Framework\ObjectManagerInterface
    
$model = $this->_objectManager->create('\M2E\TikTokShop\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 for these products by direct SQL

$model->applyChanges();


How to use the Object method

Follow these steps in order to properly implement an M2E TikTok Shop Connect 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 TikTok Shop Connect 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 TikTok Shop Connect will automatically observe all the product changes that have originated outside of Magento Core Models.

Important! We do not recommend creating an individual M2E TikTok Shop Connect 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 TikTok Shop Connect 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 TikTok Shop Connect: 

  • 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 TikTok Shop Connect Structural model

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

// $this->_objectManager instanceof \Magento\Framework\ObjectManagerInterface

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

// notify M2E TikTok Shop about some change of product with ID 17

$model->markProductChanged(17);

// make price change of product with ID 18 and then notify M2E TikTok Shop

$model->markPriceWasChanged(18);

// make QTY change of product with ID 19 and then notify M2E TikTok Shop

$model->markQtyWasChanged(19);

// make status change of product with ID 20 and then notify M2E TikTok Shop

$model->markStatusWasChanged(20);

$model->applyChanges();


How to use the Structural method

Follow these steps in order to properly implement an M2E TikTok Shop Connect 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 TikTok Shop Connect 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 TikTok Shop Connect will automatically observe all the product changes that have originated outside of Magento Core Models.

Important! We do not recommend creating an individual M2E TikTok Shop Connect 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 TikTok Shop Connect 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

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