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.
- Create a customization project
- Create a new code file, and extend the Graph from “PX.Commerce.Shopify.SPTemplateItemProcessor”.
- Select the “OVERRIDE METHOD” from the menu, and then select the Method “SaveBucketExport” from the list.
- 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);
tPXOverride]
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
} -
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.
- 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!