Sigh. This STILL doesn't work, and it should. It really, really should. Now I'm even clearing the user basket, adding 1 item, estimating shipping and then restoring the user basket contents to their original glory. This gets around the stupid not-the-users-actual-basket issue and shipping methods calculate correctly for the single basket item.
The problem is, if I start with 3 items in the basket before the test code runs, I wind up with 4 items in the basket after it's done. I should only have 3! I clear the basket at the end. I even save the basket at the end.
Even if I pause the code and do a token.instance.user.basket.items.count, the numbers will be correct. 1 item in the basket when the estimator is called, 3 items after the basket is reloaded.
Yet when I refresh to store page, I now have 4 items in the basket and I should only have 3. How maddening!! It's as if the basket class is pulling from a cache somewhere else and ignoring what I've done to it in the code.
Code: Select all
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommerceBuilder.Stores;
using CommerceBuilder.Common;
using CommerceBuilder.Orders;
using CommerceBuilder.Users;
using CommerceBuilder.Shipping;
using CommerceBuilder.Utility;
using System.Text.RegularExpressions;
using CommerceBuilder.Products;
public partial class test_shipestimate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Define a new empty collection of basket items
BasketItemCollection _OldBasketItems = new BasketItemCollection();
// Copy the current basket into a temporary basket
foreach (BasketItem _basketitem in Token.Instance.User.Basket.Items)
{
_OldBasketItems.Add(_basketitem);
}
// pull in the product
BasketItem _BasketItem = BasketItemDataSource.CreateForProduct(833, 1);
// Now clear the basket of all its contents
Token.Instance.User.Basket.Items.Clear();
Token.Instance.User.Basket.Package(true);
Token.Instance.User.Basket.Save();
// add item to new basket and package it up
Token.Instance.User.Basket.Items.Add(_BasketItem);
Token.Instance.User.Basket.Package(true);
Token.Instance.User.Basket.Save();
// build a shipping address for estimate
Address estimateAddress = new Address();
estimateAddress.PostalCode = "46239";
estimateAddress.City = "Indianapolis";
estimateAddress.Province = "IN";
estimateAddress.CountryCode = "US";
// pull in shipment, set the estimate address and calculate rates
Token.Instance.User.Basket.Shipments[0].SetAddress(estimateAddress);
BasketShipment _shipment = Token.Instance.User.Basket.Shipments[0];
GridView1.DataSource = ShipRateQuoteDataSource.QuoteForShipment(_shipment);
GridView1.DataBind();
// Clear the temp product from the basket and
// reload the store basket with the saved items
Token.Instance.User.Basket.Items.Clear();
Token.Instance.User.Basket.Package(true);
Token.Instance.User.Basket.Save();
foreach (BasketItem _OldBasketItem in _OldBasketItems)
{
Token.Instance.User.Basket.Items.Add(_OldBasketItem);
}
// repackage basket - at this point basket should be exactly
// the way it was before this started.
Token.Instance.User.Basket.Package(true);
}
}