Page 1 of 1

How to run a custom basket through shipping estimator?

Posted: Mon Aug 03, 2009 9:47 am
by AbleMods
I'm trying to build a custom basket separate from the Token.Instance.User.Basket and run it through the shipping estimator.

Estimator keeps throwing back only my custom shipping methods - none of the gateway (UPS, FedEx) methods calculate a rate.

What do I need to populate the new basket class with so estimator will work? I dug through addtobasket and basketshippingestimate but they all use Token.Instance.User.Basket. I didn't see anything that indicated something special about the token basket as it pertains to shipping estimator.

This is what I'm trying:

Code: Select all

Basket myBasket = new Basket();
BasketItem _BasketItem = BasketItemDataSource.CreateForProduct(_ProductId, 1);

myBasket.Items.Add(_BasketItem);
myBasket.Package();
Thoughts?

Re: How to run a custom basket through shipping estimator?

Posted: Mon Aug 03, 2009 1:03 pm
by jmestep
Does your basket have a shipping address connected to it?

Re: How to run a custom basket through shipping estimator?

Posted: Mon Aug 03, 2009 2:14 pm
by AbleMods
Yeah I believe it's getting added where it should.

Maybe this is a bigger question...

How do you get shipping rate estimates on something other than the Token.Instance.User.Basket ?

I want to build a completely new basket that is separate from the default user basket, populate it with products and then generate a list of estimated shipping.

This code (from basketshippingestimate.ascx) doesn't specify a basket as the command line parameter, so I'm assuming it only acts on the default user basket.

Code: Select all

                ShipRateGrid.DataSource = ShipRateQuoteDataSource.QuoteForShipment(shipment);
but when I try using the .QuoteforBasket() method, it doesn't work. There's something I'm missing........

Re: How to run a custom basket through shipping estimator?

Posted: Mon Aug 03, 2009 2:36 pm
by AbleMods
Ok I figured out how to better explain what I want...

I want to clone the Token.Instance.User.Basket class and its current contents to a temporary variable so I can mess with it without affecting the original basket.

There. I knew what I wanted, I just couldn't say it right :wink:

Re: How to run a custom basket through shipping estimator?

Posted: Mon Aug 03, 2009 5:09 pm
by jmestep
It would have to be for the shipment, not the basket, because a basket can have more than one shipment.
Are you using something like this for the fake basket in the ShipmentList_ItemDataBound?
BasketShipment shipment = (BasketShipment)e.Item.DataItem;

Re: How to run a custom basket through shipping estimator?

Posted: Tue Aug 04, 2009 6:40 am
by AbleMods
Ok, here's the nuts and bolts. The big question is, why doesn't this code work? All it returns are my flat-rate shipping methods. None of the standard methods (UPS, FedEx) get calculated. Based on everything I've seen, this should work :?

Code: Select all

        // Build a new empty basket and populate it with the product
        Basket myBasket = new Basket();

        // pull in the product
        BasketItem _BasketItem = BasketItemDataSource.CreateForProduct(833, 1);   // ProductId 833 used for testing

        // add item to new basket and package it up
        myBasket.Items.Add(_BasketItem);
        myBasket.Package();

        // 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
        myBasket.Shipments[0].SetAddress(estimateAddress);
        GridView1.DataSource = ShipRateQuoteDataSource.QuoteForBasket(myBasket);
        GridView1.DataBind();
ASPX File

Code: Select all

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test-shipestimate.aspx.cs" Inherits="test_shipestimate" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Re: How to run a custom basket through shipping estimator?

Posted: Tue Aug 04, 2009 7:07 am
by heinscott
Joe... trying moving your test onto your production server. I have been having the exact same problem. On my dev machine, I can't get any calculated methods to show up... only flat rate ones. When I run the same test from production, everything seems to work. Go figure, huh?

Hope that helps.

Scott

Re: How to run a custom basket through shipping estimator?

Posted: Thu Aug 06, 2009 8:36 am
by AbleMods
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);

    }
}

Re: How to run a custom basket through shipping estimator?

Posted: Tue Feb 08, 2011 3:06 pm
by PRS
Hi Joe,


Were you able to finish this?

Re: How to run a custom basket through shipping estimator?

Posted: Wed Feb 09, 2011 3:57 pm
by AbleMods
Nope, sorry. I finally had to give up on it. Spent hours trying to troubleshoot but didn't get anywhere with it :(

Re: How to run a custom basket through shipping estimator?

Posted: Wed Feb 09, 2011 4:11 pm
by PRS
Darn, I was hoping you could get it. Sorry, man.