When I just add normal items to the basket, i.e. LineItemType.Product, I can successfully progress right through to the Review stage where I can review the Billing and Delivery addresses entered at the Billing & Shipping page.
However, when I add products along with a LineItem of Type Discount at the Basket stage, the "preparation.GetBillToAddress()" is null at the Review stage. Have you any idea how this could be null just because I add a Discount item? See below my code:
private void ValidateAndSetShipping()
{
//Customer context
var customerContext = new CustomerContext(UmbracoContext.Current);
var basket = customerContext.CurrentCustomer.Basket();
var preparation = customerContext.CurrentCustomer.Basket().SalePreparation();
IAddress shippingAddressToUse = null;
//***************************************************//
//****************** Addresses **********************//
//***************************************************//
//Get the Billing Address
var billingAddress = new Address();
var deliveryAddress = new Address();
//Contact details
billingAddress.Email = txtEmail.Text;
billingAddress.Phone = txtTelephone.Text;
deliveryAddress.Email = txtEmail.Text;
deliveryAddress.Phone = txtTelephone.Text;
//Append to the existing billing address
billingAddress.Name = txtFirstNameBilling.Text + " " + txtLastNameBilling.Text;
billingAddress.Address1 = txtAddress1Billing.Text;
billingAddress.Address2 = txtAddress2Billing.Text;
billingAddress.Locality = txtTownCityBilling.Text;
billingAddress.Region = txtCountyStateProvinceBilling.Text;
billingAddress.PostalCode = txtPostcodeZipBilling.Text;
billingAddress.CountryCode = ddlCountryBilling.SelectedValue;
//if the checkbox is checked then use the billing address details as the delivery
if (chkDeliverySameAsBilling.Checked)
{
//Append to the existing billing address
deliveryAddress.Name = txtFirstNameBilling.Text + " " + txtLastNameBilling.Text;
deliveryAddress.Address1 = txtAddress1Billing.Text;
deliveryAddress.Address2 = txtAddress2Billing.Text;
deliveryAddress.Locality = txtTownCityBilling.Text;
deliveryAddress.Region = txtCountyStateProvinceBilling.Text;
deliveryAddress.PostalCode = txtPostcodeZipBilling.Text;
deliveryAddress.CountryCode = ddlCountryBilling.SelectedValue;
//Set the address to use for shipping
shippingAddressToUse = billingAddress;
}
else
{
//Append to the existing delivery address
deliveryAddress.Name = txtFirstNameDelivery.Text + " " + txtLastNameDelivery.Text;
deliveryAddress.Address1 = txtAddress1Delivery.Text;
deliveryAddress.Address2 = txtAddress2Delivery.Text;
deliveryAddress.Locality = txtTownCityDelivery.Text;
deliveryAddress.Region = txtCountyStateProvinceDelivery.Text;
deliveryAddress.PostalCode = txtPostcodeZipDelivery.Text;
deliveryAddress.CountryCode = ddlCountryDelivery.SelectedValue;
//Set the address to use for shipping
shippingAddressToUse = deliveryAddress;
}
try
{
//Save the Addresses
preparation.SaveBillToAddress(billingAddress);
preparation.SaveShipToAddress(deliveryAddress);
//***************************************************//
//******************* Shipping **********************//
//***************************************************//
//Associate shipment with the basket's line items
var shipment = basket.PackageBasket(shippingAddressToUse).FirstOrDefault();
//Clear any previous saved quotes
basket.SalePreparation().ClearShipmentRateQuotes();
// get the quote using the "approved shipping method"
var shipRateQuotes = shipment.ShipmentRateQuotes().ToArray();
//Get the cheapest rate
var approvedShipRateQuote = shipRateQuotes.FirstOrDefault();
//Apply the shipping charge to the basket
if (approvedShipRateQuote != null)
{
//Add the quote to the dropdown and make its container visible
ddlShippingOptions.Items.Clear();
ddlShippingOptions.Items.Add(new ListItem(approvedShipRateQuote.ShipMethod.Name + " (£" + approvedShipRateQuote.Rate.ToString("F") + ")"));
ddlShippingOptions.Items.Insert(0, new ListItem("(Please select Shipping Method...)", ""));
pnlShipping.Visible = true;
//Save
preparation.SaveShipmentRateQuote(approvedShipRateQuote);
}
else
{
//add an error to the log and display an error to the user stating that the shopping detail si incorrect,
//please contact admin
ltStatus.Text = "<p class='error-message'>There was an error with calculating the shipping. Please contact us through the <a href='/contact'>contact form</a></p>";
}
}
catch (Exception ex)
{
Merchello.Core.Services.AuditLogService auditLog = new Merchello.Core.Services.AuditLogService();
auditLog.CreateAuditLogWithKey("PFB-ERROR:- CheckoutBilling.ascx - " + ex.Message, true, true);
}
}
Review:
var customerContext = new CustomerContext(UmbracoContext.Current);
var basket = customerContext.CurrentCustomer.Basket();
var preparation = customerContext.CurrentCustomer.Basket().SalePreparation();
var billingAddress = preparation.GetBillToAddress();
I am using WebForms, Umbraco 7.2.4 and Merchello 1.8.2.
Every time something is added, updated or removed from the basket the "VersionKey" (GUID) is changed. This version key is used to verify that the SalePreparation is the same "version" as that of the basket.
This is mainly for Shipping and Tax computations - e.g. if quantities change or the number of items, those values would likely be different.
If the version is different, the SalePreparation internally resets itself clearing information (including the billing address). So what is likely happening, is you are adding the discount to the basket between where you save the billing address to the SalePreparation and when you present the review.
Try adding the discount to the SalePreparation object rather than to the basket ...
On the Review page I was pulling a discount item from the database and updating the basket. I wanted to check, at the Review stage, if the discount coupon still existed. I have removed the update and now I just pull the details from the basket line item row.
Coupons were actually added in Merchello 1.9.0. The way they work is to store the coupon discount code in the Merchello cookie (if the code passes all the configured constraints) and then every time the invoice is generated, the discount is added in the invoice builder.
This is done so that we can enforce constraints on a discount, in cases where multiple coupons can be added to a single checkout.
GetBillToAddress is null when a LineItem of Type Discount is added to the basket
Hi Rusty,
I have the following page walkthrough (in order) for my Merchello checkout:
Basket > Sign In/Register > Billing & Shipping > Review > Payment.
When I just add normal items to the basket, i.e. LineItemType.Product, I can successfully progress right through to the Review stage where I can review the Billing and Delivery addresses entered at the Billing & Shipping page.
However, when I add products along with a LineItem of Type Discount at the Basket stage, the "preparation.GetBillToAddress()" is null at the Review stage. Have you any idea how this could be null just because I add a Discount item? See below my code:
Basket (Add Discount LineItem):
Billing & Shipping:
Review:
I am using WebForms, Umbraco 7.2.4 and Merchello 1.8.2.
Thanks in advance!
Hey Paul,
Every time something is added, updated or removed from the basket the "VersionKey" (GUID) is changed. This version key is used to verify that the SalePreparation is the same "version" as that of the basket.
This is mainly for Shipping and Tax computations - e.g. if quantities change or the number of items, those values would likely be different.
If the version is different, the SalePreparation internally resets itself clearing information (including the billing address). So what is likely happening, is you are adding the discount to the basket between where you save the billing address to the SalePreparation and when you present the review.
Try adding the discount to the SalePreparation object rather than to the basket ...
Hi Rusty,
On the Review page I was pulling a discount item from the database and updating the basket. I wanted to check, at the Review stage, if the discount coupon still existed. I have removed the update and now I just pull the details from the basket line item row.
Thanks alot again.
Paul
Hey Paul,
Coupons were actually added in Merchello 1.9.0. The way they work is to store the coupon discount code in the Merchello cookie (if the code passes all the configured constraints) and then every time the invoice is generated, the discount is added in the invoice builder.
This is done so that we can enforce constraints on a discount, in cases where multiple coupons can be added to a single checkout.
Hey Rusty,
Yes I realised the coupons were part of Merchello after I wrote quite a bit of custom code :-).
For future builds I will use the latest version of Merchello.
Ok cool. Nice to know the mechanics of it!
Thanks again
is working on a reply...