{"record":{"id":"a94c24764fe47ca0","repo":"nopSolutions/nopCommerce","slug":"shopping-was-not-initiated-by-customer","errorCode":null,"errorMessage":"Shopping was not initiated by customer","messagePattern":"Shopping was not initiated by customer","errorType":"exception","errorClass":"NopException","httpStatus":null,"severity":"error","filePath":"src/Plugins/Nop.Plugin.Widgets.FacebookPixel/Services/FacebookPixelService.cs","lineNumber":652,"sourceCode":"    /// <summary>\n    /// Prepare add to cart event model\n    /// </summary>\n    /// <param name=\"item\">Shopping cart item</param>\n    /// <returns>\n    /// A task that represents the asynchronous operation\n    /// The task result contains the ConversionsEvent model\n    /// </returns>\n    protected async Task<ConversionsEvent> PrepareAddToCartEventModelAsync(ShoppingCartItem item)\n    {\n        ArgumentNullException.ThrowIfNull(item);\n\n        //check whether the shopping was initiated by the customer\n        var customer = await _workContext.GetCurrentCustomerAsync();\n\n        var store = await _storeContext.GetCurrentStoreAsync();\n\n        if (item.CustomerId != customer.Id)\n            throw new NopException(\"Shopping was not initiated by customer\");\n\n        var eventName = item.ShoppingCartTypeId == (int)ShoppingCartType.ShoppingCart\n            ? FacebookPixelDefaults.ADD_TO_CART\n            : FacebookPixelDefaults.ADD_TO_WISHLIST;\n\n        var product = await _productService.GetProductByIdAsync(item.ProductId);\n        var categoryMapping = (await _categoryService.GetProductCategoriesByProductIdAsync(product?.Id ?? 0)).FirstOrDefault();\n        var categoryName = (await _categoryService.GetCategoryByIdAsync(categoryMapping?.CategoryId ?? 0))?.Name;\n        var sku = product != null ? await _productService.FormatSkuAsync(product, item.AttributesXml) : string.Empty;\n        var quantity = product != null ? (int?)item.Quantity : null;\n        var (productPrice, _, _, _) = await _priceCalculationService.GetFinalPriceAsync(product, customer, store, includeDiscounts: false);\n        var (price, _) = await _taxService.GetProductPriceAsync(product, productPrice);\n        var currentCurrency = await _workContext.GetWorkingCurrencyAsync();\n        var priceValue = await _currencyService.ConvertFromPrimaryStoreCurrencyAsync(price, currentCurrency);\n        var currency = currentCurrency?.CurrencyCode;\n\n        var eventObject = new ConversionsEventCustomData\n                {","sourceCodeStart":634,"sourceCodeEnd":670,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Plugins/Nop.Plugin.Widgets.FacebookPixel/Services/FacebookPixelService.cs#L634-L670","documentation":"Thrown by PrepareAddToCartEventModelAsync when item.CustomerId does not equal the current customer's Id (from _workContext). The event builder refuses to construct a ConversionsEvent for a cart item that belongs to a different customer, as that would leak cross-customer data into the Facebook Pixel event.","triggerScenarios":"A ShoppingCartItem belonging to customer A is passed while the work context current customer is B. Happens during admin impersonation, background order processing for another customer, or cart-merge edge cases where the current-customer context is stale.","commonSituations":"Admin placing an order on behalf of a customer; a queued/background job processing cart items without setting the correct work-context customer; a signed-in admin viewing another customer's cart; cart migration between guest and registered accounts where context lags.","solutions":["Ensure the work-context customer matches item.CustomerId before calling the event preparer (set the correct customer context in background jobs).","Skip event emission (don't throw) when the item is not owned by the current customer — logging a warning instead.","In admin/impersonation flows, explicitly bypass Conversions API event preparation."],"exampleFix":"// before\nvar customer = await _workContext.GetCurrentCustomerAsync();\nif (item.CustomerId != customer.Id)\n    throw new NopException(\"Shopping was not initiated by customer\");\n\n// after — skip silently for non-owner contexts (admin/background)\nvar customer = await _workContext.GetCurrentCustomerAsync();\nif (item.CustomerId != customer.Id)\n{\n    _logger.Debug($\"Skipping AddToCart event: item {item.Id} belongs to customer {item.CustomerId}, not current {customer.Id}\");\n    return null;\n}","handlingStrategy":"validation","validationCode":"var current = await _workContext.GetCurrentCustomerAsync();\nif (item.CustomerId != current.Id) return null; // not the owner's session","typeGuard":"static bool IsCartOwner(ShoppingCartItem item, Customer current) => item.CustomerId == current.Id;","tryCatchPattern":"try { await PrepareAddToCartEventModelAsync(item); }\ncatch (NopException ex) when (ex.Message.Contains(\"not initiated by customer\"))\n{ _logger.Debug(ex, \"Skipping event for non-owner cart item\"); }","preventionTips":["Set the correct work-context customer in background jobs.","Skip (don't throw) event preparation for admin/impersonation flows.","Run conversions events inline in the customer's own session."],"tags":["facebook-pixel","conversions-api","authorization","customer-context","nopcommerce"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}