How To: Export Matrix Item cost to Shopify product variant

  • 14 February 2024
  • 4 replies
  • 107 views

Userlevel 5
Badge +1

In this topic, I want to share how to create a customization to export matrix item cost info to Shopify product variant. This customization can work well on 2023R2.

  1. Create a customization project
  2. Create a new code file, and extend the Graph from “PX.Commerce.Shopify.SPTemplateItemProcessor”.
     

     

  3. Select the “OVERRIDE METHOD” from the menu, and then select the Method “SaveBucketExport” from the list.
     

     

  4.  Modify the code as below.
    • Create the new instance of ProductRestDataProvider and InventoryItemRestDataProvider object
    • Use the ProductRestDataProvider instance to get the existing Product/Product variant info from Shopify, cost info doesn’t save in Product variant object, it’s in the InventoryItem object, so we need to get the InventoryItemId from the variant object.
    • We save the variant mapping info(ExternID is Product variant ID, LocalID is the Matrix item NoteID) in BCSyncDetails, so we can find the associated Matrix item by NoteID. All matrix items are saved in Bucket.Product.Local.Matrix object. 
    • Get the cost info from LastCost field or CurrentStdCost field from the Matrix item object, if you want to get the cost from other field, you can implement your own logic.
    • Create the new InventoryItem object and add to the list.
    • Call InventoryItemRestDataProvider to update the InventoryItem one by one
    	public class SPTemplateItemProcessor_Extension : PXGraphExtension<PX.Commerce.Shopify.SPTemplateItemProcessor>
    {
    public static bool IsActive() => CommerceFeaturesHelper.ShopifyConnector;
    #region Event Handlers
    public delegate Task SaveBucketExportDelegate(SPTemplateItemEntityBucket bucket, IMappedEntity existing, String operation, CancellationToken cancellationToken);

    [PXOverride]
    public Task SaveBucketExport(SPTemplateItemEntityBucket bucket, IMappedEntity existing, String operation, CancellationToken cancellationToken, SaveBucketExportDelegate baseMethod)
    {
    var baseResult = baseMethod(bucket, existing, operation, cancellationToken);

    var client = SPConnector.GetRestClient(Base.GetBindingExt<BCBindingShopify>());
    ProductRestDataProvider productRestDataProvider = new ProductRestDataProvider(client);
    InventoryItemRestDataProvider itemRestDataProvider = new InventoryItemRestDataProvider(client);

    List<InventoryItemData> inventoryItems = new List<InventoryItemData>();

    ProductData data = productRestDataProvider.GetByID(bucket.Product.ExternID, false, cancellationToken);
    if (data?.Variants?.Count > 0)
    {
    var syncDetails = bucket.Product.Details?.Where(x => x?.EntityType == BCEntitiesAttribute.Variant).ToList();
    foreach(var variant in data.Variants)
    {
    if (variant.InventoryItemId == null) continue;

    //Get the NoteID from the sync detail
    var localId = syncDetails.FirstOrDefault(x => string.Equals(x.ExternID, variant.Id.ToString()))?.LocalID;
    if (localId == null) continue;

    var matchMatrixItem = bucket.Product.Local?.Matrix?.Where(x => x.Id == localId).FirstOrDefault();
    if (matchMatrixItem == null) continue;

    var itemCost = matchMatrixItem.LastCost?.Value ?? matchMatrixItem.CurrentStdCost?.Value ?? 0;
    //Depends on the type of item, you need to provide the RequiresShipping and Tracked field value
    inventoryItems.Add(new InventoryItemData() { Id = variant.InventoryItemId, Cost = itemCost, Sku = variant.Sku, RequiresShipping = true, Tracked = true});
    }

    if(inventoryItems.Count > 0)
    {
    inventoryItems.ForEach(x => itemRestDataProvider.Update(x));
    }
    }

    return baseResult;
    }

    #endregion
    }
  5. Save the changes and publish the customization package, and then re-sync the TemplateItem from Sync history screen, you will see the cost info in Shopify product variant.
     

     

  6. If you wan to export the cost info from stock item or non stock item, you need to override SPStockItemProcessor or SPNonStockItemProcessor, and the stock item or non-stock item is saved in Bucket.Product.Local, you have to modify the code. 

I attached the customization package for your reference. 

Hope it helps! 


4 replies

Userlevel 7
Badge

Thank you for sharing this tip with the community @simonliang91!

Userlevel 5
Badge

@simonliang91 - Will this work in 23R1?

Userlevel 5
Badge +1

@StevenRatner, if you want to implement it into 2023r1, you have to modify the code to get the matrix item’s cost info from ERP. In 2023r1, there is no Cost data in Matrix item object.

Userlevel 5
Badge

@simonliang91 - Could I pull the cost from the stock item (INItemCost - AvgCost) or would you see that being an issue?

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved