/// <summary> /// Post cart to google /// </summary> /// <param name="Req">Pre-generated request</param> /// <param name="Cart">Shopping cart</param> /// <returns>Response</returns> public GCheckoutResponse PostCartToGoogle(CheckoutShoppingCartRequest Req, NopSolutions.NopCommerce.BusinessLogic.Orders.ShoppingCart Cart) { foreach (NopSolutions.NopCommerce.BusinessLogic.Orders.ShoppingCartItem sci in Cart) { ProductVariant productVariant = sci.ProductVariant; if (productVariant != null) { string pvAttributeDescription = ProductAttributeHelper.FormatAttributes(productVariant, sci.AttributesXML, NopContext.Current.User, ", ", false); string fullName = productVariant.FullProductName; string description = pvAttributeDescription; decimal unitPrice = TaxManager.GetPrice(sci.ProductVariant, PriceHelper.GetUnitPrice(sci, NopContext.Current.User, true)); Req.AddItem(fullName, description, sci.ShoppingCartItemID.ToString(), unitPrice, sci.Quantity); } } decimal shoppingCartSubTotalDiscount; decimal shoppingCartSubTotal = ShoppingCartManager.GetShoppingCartSubTotal(Cart, NopContext.Current.User, out shoppingCartSubTotalDiscount); if (shoppingCartSubTotalDiscount > decimal.Zero) Req.AddItem("Discount", string.Empty, string.Empty, (decimal)(-1.0) * shoppingCartSubTotalDiscount, 1); bool shoppingCartRequiresShipping = ShippingManager.ShoppingCartRequiresShipping(Cart); if (shoppingCartRequiresShipping) { string shippingError = string.Empty; //TODO AddMerchantCalculatedShippingMethod //TODO AddCarrierCalculatedShippingOption ShippingOptionCollection shippingOptions = ShippingManager.GetShippingOptions(Cart, NopContext.Current.User, null, ref shippingError); foreach (ShippingOption shippingOption in shippingOptions) Req.AddFlatRateShippingMethod(shippingOption.Name, TaxManager.GetShippingPrice(shippingOption.Rate, NopContext.Current.User)); } //add only US, GB states //CountryCollection countries = CountryManager.GetAllCountries(); //foreach (Country country in countries) //{ // foreach (StateProvince state in country.StateProvinces) // { // TaxByStateProvinceCollection taxByStateProvinceCollection = TaxByStateProvinceManager.GetAllByStateProvinceID(state.StateProvinceID); // foreach (TaxByStateProvince taxByStateProvince in taxByStateProvinceCollection) // { // if (!String.IsNullOrEmpty(state.Abbreviation)) // { // Req.AddStateTaxRule(state.Abbreviation, (double)taxByStateProvince.Percentage, false); // } // } // } //} XmlDocument customerInfoDoc = new XmlDocument(); XmlElement customerInfo = customerInfoDoc.CreateElement("CustomerInfo"); customerInfo.SetAttribute("CustomerID", NopContext.Current.User.CustomerID.ToString()); customerInfo.SetAttribute("CustomerLanguageID", NopContext.Current.WorkingLanguage.LanguageID.ToString()); customerInfo.SetAttribute("CustomerCurrencyID", NopContext.Current.WorkingCurrency.CurrencyID.ToString()); Req.AddMerchantPrivateDataNode(customerInfo); Req.ContinueShoppingUrl = CommonHelper.GetStoreLocation(false); Req.EditCartUrl = CommonHelper.GetStoreLocation(false) + "ShoppingCart.aspx"; GCheckoutResponse Resp = Req.Send(); return Resp; }
/// <summary> /// Post cart to google /// </summary> /// <param name="req">Pre-generated request</param> /// <param name="cart">Shopping cart</param> /// <returns>Response</returns> public GCheckoutResponse PostCartToGoogle(CheckoutShoppingCartRequest req, NopSolutions.NopCommerce.BusinessLogic.Orders.ShoppingCart cart) { //items foreach (NopSolutions.NopCommerce.BusinessLogic.Orders.ShoppingCartItem sci in cart) { ProductVariant productVariant = sci.ProductVariant; if (productVariant != null) { decimal taxRate = decimal.Zero; string description = ProductAttributeHelper.FormatAttributes(productVariant, sci.AttributesXml, NopContext.Current.User, ", ", false); string fullName = productVariant.LocalizedFullProductName; decimal unitPrice = TaxManager.GetPrice(sci.ProductVariant, PriceHelper.GetUnitPrice(sci, NopContext.Current.User, true), out taxRate); req.AddItem(fullName, description, sci.ShoppingCartItemId.ToString(), unitPrice, sci.Quantity); } } //decimal subtotalBase = decimal.Zero; //string subTotalError = ShoppingCartManager.GetShoppingCartSubTotal(cart, // NopContext.Current.User, out subtotalBase); bool shoppingCartRequiresShipping = ShippingManager.ShoppingCartRequiresShipping(cart); if (shoppingCartRequiresShipping) { string shippingError = string.Empty; //AddMerchantCalculatedShippingMethod //AddCarrierCalculatedShippingOption List<ShippingOption> shippingOptions = ShippingManager.GetShippingOptions(cart, NopContext.Current.User, null, ref shippingError); foreach (ShippingOption shippingOption in shippingOptions) req.AddFlatRateShippingMethod(shippingOption.Name, TaxManager.GetShippingPrice(shippingOption.Rate, NopContext.Current.User)); } //add only US, GB states //CountryCollection countries = CountryManager.GetAllCountries(); //foreach (Country country in countries) //{ // foreach (StateProvince state in country.StateProvinces) // { // TaxByStateProvinceCollection taxByStateProvinceCollection = TaxByStateProvinceManager.GetAllByStateProvinceID(state.StateProvinceID); // foreach (TaxByStateProvince taxByStateProvince in taxByStateProvinceCollection) // { // if (!String.IsNullOrEmpty(state.Abbreviation)) // { // Req.AddStateTaxRule(state.Abbreviation, (double)taxByStateProvince.Percentage, false); // } // } // } //} //if (subTotalDiscountBase > decimal.Zero) //{ // req.AddItem("Discount", string.Empty, string.Empty, (decimal)(-1.0) * subTotalDiscountBase, 1); //} //foreach (AppliedGiftCard agc in appliedGiftCards) //{ // req.AddItem(string.Format("Gift Card - {0}", agc.GiftCard.GiftCardCouponCode), string.Empty, string.Empty, (decimal)(-1.0) * agc.AmountCanBeUsed, 1); //} XmlDocument customerInfoDoc = new XmlDocument(); XmlElement customerInfo = customerInfoDoc.CreateElement("CustomerInfo"); customerInfo.SetAttribute("CustomerID", NopContext.Current.User.CustomerId.ToString()); customerInfo.SetAttribute("CustomerLanguageID", NopContext.Current.WorkingLanguage.LanguageId.ToString()); customerInfo.SetAttribute("CustomerCurrencyID", NopContext.Current.WorkingCurrency.CurrencyId.ToString()); req.AddMerchantPrivateDataNode(customerInfo); req.ContinueShoppingUrl = CommonHelper.GetStoreLocation(false); req.EditCartUrl = CommonHelper.GetStoreLocation(false) + "ShoppingCart.aspx"; GCheckoutResponse resp = req.Send(); return resp; }