Update all shopping cart items with single button

Eagle Y asked on November 25, 2015 13:09

Hi,

I wan to create a single update button on shopping cart.

I will use code found in: CMSModules\Ecommerce\Controls\Checkout\CartItemUnits.ascx.cs public void Update(object sender, EventArgs e){ ... }

I know I need to look though shopping cart items and update them.

I'll use: foreach (ShoppingCartItemInfo item in ECommerceContext.CurrentShoppingCart.CartItems) { ... }

Question is, since the UNITS input code is placed in the transformation which displays products in shopping cart, how can I get those values?

It seems to me that Page.FinControl("myID") and then get the value will not work since there could be multiple products in shopping cart and that means multiple UNIT inputs.

Any idea how I can get the UNITS values per shoppingCartItem?

This is last piece the puzzle :)

Recent Answers


Dawid Jachnik answered on November 25, 2015 13:22

Hello,

You should have the input units text box in the control where is placed your Update function. It will be simple solution.

BTW:

foreach (ShoppingCartItemInfo item in ECommerceContext.CurrentShoppingCart.CartItems) { 
    int units = item.CartItemUnits; // units of item in your shopping cart
}
0 votesVote for this answer Mark as a Correct answer

Eagle Y answered on November 25, 2015 19:13 (last edited on November 26, 2015 09:09)

"You should have the input units text box in the control where is placed your Update function"

the units textbox are in a control that is aded in the transformation.

That is where the default update button is. problem is that each item (product) in shopping cart has its own update button.

I want to use one update button for he whole cart.

What you suggested:

foreach (ShoppingCartItemInfo item in ECommerceContext.CurrentShoppingCart.CartItems) { 
    int units = item.CartItemUnits; // units of item in your shopping cart
}

Only get the current update units per item and NOT what user enters into units textbox per item.

0 votesVote for this answer Mark as a Correct answer

Dawid Jachnik answered on November 26, 2015 10:36

So I think that without little help of javascript there's no way. Make one hidden field and put there SKUID and units count in pairs for each item in shopping cart. Do it on button click: example:

$('#updateBtn').click(function() { 
    var result = '';
    $('#shoppingCart .row').each(function () {
        result +=';'+ $(this).attr('skuid');+'|'+$(this).find('.UnitCountControl input[type=text]').val();
    });
    if (result.length>0)
        result = result.substring(1);
    $('#hiddenField').val(result);
});

for example

14215|1;13245|3;1245|1

Then in your code make function that will loop through the sku with Update function, example:

private void updateBtn_click(object sender, EventArgs e)
{
    if (!String.IsNullOrEmpty(hiddenField.Value))
    {
        string[] skus = hiddenField.Value.Split(';');
        foreach(string pair in skus)
        {
            string[] sku = pair.Split('|');
            Update(ValidationHelper.GetInteger(sku[0], 0), ValidationHelper.GetInteger(sku[1], 0)); // first parameters is SKUID, second one is units count
        }
    }
}

You have here almost full customization. You just need to modify your function Update for meet your needs.

Hope it was helpful :)

0 votesVote for this answer Mark as a Correct answer

Eagle Y answered on November 26, 2015 11:54

Thanks for your amazing answer.

In the mean time I did the following, which is kinda tricking the system.

\CMSModules\Ecommerce\Controls\Checkout\CartItemUnits.ascx

I added a asp:TextBox and am using it for units instead of cms:FormControl runat="server" ID="unitCountFormControl" FormControlName="TextBoxControl" .

So thi is my units input: <asp:TextBox runat="server" ID="unitCount" OnTextChanged="unitCount_TextChanged" AutoPostBack="true" Width="20" ></asp:TextBox>

In addition I added <asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="btnUpdate_Click" />

Above the shopping cart. btnUpdate_Click function has no code in it.

It is used to trigger the OnTextChanged="unitCount_TextChanged" which saves the UNIT values.

Anyone know how I can trigger "SaveStepData" in all webparts on this STEP?

I want to trigger it when btnUpdate_Click runs.

0 votesVote for this answer Mark as a Correct answer

Dawid Jachnik answered on November 26, 2015 15:26

Simply, just go to next step :D or try to use

ComponentEvents.RequestEvents.RaiseComponentEvent(this, WizardManager.StepEventArgs, "PageWizardManager", ComponentEvents.SAVE);

If this doesn't help try to change to another event in ComponentEvents

0 votesVote for this answer Mark as a Correct answer

Eagle Y answered on December 9, 2015 11:28

used part of last answer:

ComponentEvents.RequestEvents.RaiseComponentEvent(this, WizardManager.StepEventArgs, "PageWizardManager", ComponentEvents.NEXT);

Used ".NEXT" becasue ".SAVE" did not work.

using .NEXT I then needed to cancel the move to next step and just use the saving part.

This works great.

Only issue i have now is that the following code that is used in the shopping cart item remove and in units causes my new control to disappear from shopping cart:

ComponentEvents.RequestEvents.RaiseEvent(sender, e, SHOPPING_CART_CHANGED);

When I comment it out all is good.

How terrible is it to leave ti commented out?

Or maybe someone knows why causes my custom controls to disappear and is there ea way to fix it?

THANKS :)

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.