Cant add more than 1 ship ment to a new basket

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
adamh
Ensign (ENS)
Ensign (ENS)
Posts: 17
Joined: Fri Nov 14, 2008 11:14 am

Cant add more than 1 ship ment to a new basket

Post by adamh » Wed Sep 30, 2009 4:10 pm

We need the ability for admins to go in and change existing orders. Ablecommerce has this built in to the admin, but it will not recalculate shipping and/or tax. To accomplish this, I try to create a new order from the existing one, and delete the old one after its done. The code is as follows:

Code: Select all

Basket basket = new Basket();
basket.UserId = this.Order.UserId;
basket.Save();

int i = 0;
foreach (OrderShipment shipment in this.Order.Shipments)
{
	GridView itemsGrid = OrderShipments.Items[i].FindControl("ShipmentItems") as GridView;

	BasketShipment basketShipment = new BasketShipment();
	basketShipment.BasketId = basket.BasketId;
	basketShipment.SetAddress(shipment.Address);
	basketShipment.WarehouseId = shipment.WarehouseId;

	basket.Shipments.Add(basketShipment);
	basketShipment.Save();

	for (int j = 0; j < shipment.OrderItems.Cast<OrderItem>().Count(item => item.OrderItemType == OrderItemType.Product); j++)
	{
		TextBox quantityText = itemsGrid.Rows[j].Cells[0].FindControl("Quantity") as TextBox;
		DropDownList productDropDown = itemsGrid.Rows[j].Cells[0].FindControl("Product") as DropDownList;

		short quantity;
		int productId;
		string optionList;

		if (short.TryParse(quantityText.Text, out quantity) &&
			ProductOptionContainer.TryParse(productDropDown.SelectedValue, out productId, out optionList))
		{
			BasketItem basketItem = BasketItemDataSource.CreateForProduct(productId, quantity, optionList, string.Empty);
			basketItem.BasketId = basket.BasketId;
			basketItem.BasketShipmentId = basketShipment.BasketShipmentId;

			basketItem.Save();
			basketShipment.Items.Add(basketItem);
		}
	}

	basketShipment.ApplyShipMethod(shipment.ShipMethodId);

	i++;
}

basket.Recalculate();
basket.Save();

// Do checkout and delete old order here...
This works fine if the order only contains 1 shipment. However if the order has multiple shipments, the basketitems will not be added to the basketshipment's item collection. On the line:

Code: Select all

basketShipment.Items.Add(basketItem);
the basketitem collection will remain unchanged with no shippable items and no basketitems in the collection.

The basketitems are inserted in to the database and appear fine there, but refuse to show up in the basketshipment's basketitem collection. This also leads to an invalid shipMethod error on the line:

Code: Select all

basketShipment.ApplyShipMethod(shipment.ShipMethodId);
because it does not believe there are items in the shipment, which there are.

Does anybody know why this is happening?

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

Re: Cant add more than 1 ship ment to a new basket

Post by AbleMods » Thu Oct 01, 2009 5:07 am

adamh wrote:has this built in to the admin, but it will not recalculate shipping and/or tax
7.0.3 has full support for recalc'ing shipping charges after order entry. 7.0.4 will support recalc'ing taxes after order entry.

Make sure you save your collections after you add to them i.e. basketShipment.Items.Save();

Working with baskets is painful - some of the logic isn't documented at all and some if it flat out doesn't make sense. From looking at your code, I can't see why it wouldn't work. I assume you've dug around the onepagecheckout code to see how Able does it? For that matter, it'd probably be easier to dig through the admin-side code...
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

adamh
Ensign (ENS)
Ensign (ENS)
Posts: 17
Joined: Fri Nov 14, 2008 11:14 am

Re: Cant add more than 1 ship ment to a new basket

Post by adamh » Thu Oct 01, 2009 9:35 am

Thank you for the quick response. I have tried saving the basketitems collection after I add an item, but the problem is the basketitems are never added after the first shipment. The list remains empty, with a count of 0, hasshippableitems = false and shippableproductcount = 0 no matter what I've tried.

I've simpled it down so you can reproduce (hopefully) this result:

Code: Select all


Basket basket = new Basket();
basket.UserId = 817;
basket.Save();

// first basket shipment will add items fine
BasketShipment basketShipment = new BasketShipment();
basketShipment.BasketId = basket.BasketId;

basket.Shipments.Add(basketShipment);
basketShipment.Save();

BasketItem basketItem = BasketItemDataSource.CreateForProduct(1, 1);
basketItem.BasketId = basket.BasketId;
basketItem.BasketShipmentId = basketShipment.BasketShipmentId;

basketItem.Save();
basketShipment.Items.Add(basketItem);
basketShipment.Items.Save();

// 2nd basketshipment will not add any items
BasketShipment basketShipment2 = new BasketShipment();
basketShipment2.BasketId = basket.BasketId;

basket.Shipments.Add(basketShipment2);
basketShipment2.Save();

BasketItem basketItem2 = BasketItemDataSource.CreateForProduct(1, 1);
basketItem2.BasketId = basket.BasketId;
basketItem2.BasketShipmentId = basketShipment2.BasketShipmentId;

basketItem2.Save();
basketShipment2.Items.Add(basketItem2);
basketShipment2.Items.Save();
Please change the userid and productid to something you can test with.

Can you please explain why the first basketshipment will add the basketitem fine, but the second basketshipment does not. The code is identical and I can not for the life of me figure out why this is happening. Thanks

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

Re: Cant add more than 1 ship ment to a new basket

Post by AbleMods » Thu Oct 01, 2009 9:47 am

adamh wrote:Can you please explain why the first basketshipment will add the basketitem fine, but the second basketshipment does not.
I'm afraid I can't explain it.

I've seen similar stuff happen when trying to do shipping estimates on any non-token basket. Certain able functions and methods simply don't seem to work on anything other than Token.Instance.User.Basket :(

Without full source, I haven't been able to dig deeper into it. Someday I will because it bugs the daylights out of me not knowing why......
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

adamh
Ensign (ENS)
Ensign (ENS)
Posts: 17
Joined: Fri Nov 14, 2008 11:14 am

Re: Cant add more than 1 ship ment to a new basket

Post by adamh » Thu Oct 01, 2009 10:20 am

Whoops, sorry Joe. Assumed you were part of the staff here. Thanks for giving it a shot.

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Cant add more than 1 ship ment to a new basket

Post by mazhar » Fri Oct 02, 2009 7:52 am

During the checkout process AbleCommerce recalculates shipments etc and if something doesn't fit on rules it will enforce the defaults. For multiple shipments make sure that destination address for each shipment should be different or if you want to send multiple shipments on same address then then products must be shipped form different warehouses other wise when AbleCommererce will recalculate basket upon checkout it will remove extra shipments.

dlynton
Ensign (ENS)
Ensign (ENS)
Posts: 1
Joined: Sat Oct 03, 2009 8:30 am

Re: Cant add more than 1 ship ment to a new basket

Post by dlynton » Sat Oct 03, 2009 8:46 am

Mazhar,

Thank you for your reply, however I don't think you understand the entire situation. We are trying to programmatically create a new basket from a previous order so we can change the quantities/items in the order. Per Adam's initial post:
"This works fine if the order only contains 1 shipment. However if the order has multiple shipments, the basketitems will not be added to the basketshipment's item collection.

the basketitem collection will remain unchanged with no shippable items and no basketitems in the collection.

The basketitems are inserted in to the database and appear fine there, but refuse to show up in the basketshipment's basketitem collection.

This also leads to an invalid shipMethod error because it does not believe there are items in the shipment, which there are."
Our multiple shipments do have unique addresses. The first checkout process works just fine, with the orders split into shipments as expected. Please re-read Adam's original post and his code. I believe he has supplied all the information you need to investigate this problem further, as it appears to be an issue with the AbleCommerce API.

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Cant add more than 1 ship ment to a new basket

Post by mazhar » Mon Oct 05, 2009 8:18 am

Here is the code working for me. Create a new file under ConLib/Custom folder, name it Sample.ascx and then make use of it on store default page. It will bring up a order number text box and a button. Enter order number you want to duplicate and then click save order button. In this code I assumed that shipping items are not having options or kits so for testing place order for simple products. Secondly for payments I made use of Pay By Phone call method and its id is being used when creating payment (7 in my case). You can update payment code as desired.

Code: Select all

<%@ Control Language="C#" ClassName="Sample" %>

<script runat="server">

    protected void SaveNewOrder_Click(Object sender,EventArgs e) 
    {
        int orderId = OrderDataSource.LookupOrderId(AlwaysConvert.ToInt(OrderNumber.Text));
        if (orderId < 1)
            return;
        Order order = OrderDataSource.Load(orderId);
        
        Basket basket = new Basket();
        basket.UserId = order.UserId;
        basket.Save();

        foreach (OrderItem orderItem in order.Items)
        {
            if (orderItem.OrderItemType == OrderItemType.Product)
            {
                BasketItem basketItem = BasketItemDataSource.CreateForProduct(orderItem.ProductId, orderItem.Quantity);
                basket.Items.Add(basketItem);
            }
        }
        basket.Save();
        foreach (BasketItem basketItem in basket.Items)
        {
            basketItem.BasketShipmentId = 0;
            basketItem.Save();
        }
        basket.Shipments.DeleteAll();
        
        foreach (OrderShipment orderShipment in order.Shipments)
        {
            
            BasketShipment basketShipment = new BasketShipment();
            basketShipment.BasketId = basket.BasketId;
            basketShipment.WarehouseId = orderShipment.WarehouseId;
            basketShipment.AddressId = orderShipment.Address.AddressId;
            basketShipment.ApplyShipMethod(orderShipment.ShipMethodId);
            basket.Shipments.Add(basketShipment);
            
            basketShipment.Save();
            
            foreach (OrderItem orderItem in orderShipment.OrderItems)
            {
                foreach (BasketItem basketItem in basket.Items)
                {
                    if (basketItem.ProductId == orderItem.ProductId)
                    {
                        basketItem.BasketShipmentId = basketShipment.BasketShipmentId;
                        basketItem.Quantity = orderItem.Quantity;
                        basketItem.Save();
                        basketShipment.Items.Add(basketItem);
                    }
                }
            }
            basketShipment.Save();
            
            basketShipment.Save();
            basket.Save();
        }
        
        
        Payment payment = new Payment();
        
        //Pay By Phone call for test purpose
        payment.PaymentMethodId = 7;
        payment.Amount = basket.Items.TotalPrice();
        payment.Amount = basket.Items.TotalPrice();
        
        CheckoutRequest checkoutRequest = new CheckoutRequest(payment);
        CheckoutResponse checkoutResponse = basket.Checkout(checkoutRequest);
        if (checkoutResponse.Success)
        {
            Response.Write("OK");
        }
        else
        {
            foreach(string msg in checkoutResponse.WarningMessages)
            Response.Write(msg);
        }
        
    }
</script>
<asp:TextBox ID="OrderNumber" runat="server"></asp:TextBox>
<asp:Button ID="SaveNewOrder" runat="server" Text="Save Order" OnClick="SaveNewOrder_Click" />

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Cant add more than 1 ship ment to a new basket

Post by mazhar » Mon Oct 05, 2009 8:32 am

I think the main point is that first drop all shipments from basket then manually create shipments and finally don't call recalculate/package after manually creating shipments otherwise AbleCommerce will recalculate shipments and will delete your manual multiple shipments created.

Post Reply