remove from cart when local pickup is chosen

I have box items that are automatically added to my cart based on other items in the cart, but I want to remove them if the customer chooses the local pickup option at checkout. What's the best way to accomplish this? All box items are in a category called "BOX."
Comments
  • fc_adamfc_adam FoxyCart Team
    @puremoxie,

    For performing custom logic based on a customer selecting a shipping rate, that would be best handled with javascript. If you're currently automatically adding products to the cart based on what the customer has already added, I'm guessing you may already be dynamically adding products with javascript - is that right?

    For removing them, you could hook into our customer-shipping-option-select.done javascript event. Within that you can check what shipping event has been selected, and if it's the customer pickup, remove any "BOX" products present in the cart.

    The following code, while untested, should give you an idea of how you can approach this with javascript:
    <script>
    FC.client.on("customer-shipping-option-select.done", function(params) {
    if (params.shipping_service_id == 1234) {
    var remove = "";
    for (var i = 0; i < FC.json.items.length; i++) {
    if (FC.json.items[i].category == "BOX") {
    remove += "&" + i + ":quantity=0&" + i + ":id=" FC.json.items[i].id;
    }
    }

    if (remove != "") {
    FC.client.request("https://" + FC.json.config.store_domain + "/cart?cart=update" + remove).done(function() {
    FC[FC.json.context].render();
    });
    }
    }
    });
    </script>
    I hope that helps! Let us know if you have any further questions
  • Thanks, just getting back to this, and your example did get me on the right track. The modified version below does remove the items in the BOX category when the local pickup option is selected on the checkout page, but after selecting local pickup and then another shipping option (in case the customer did not mean to choose local pickup), the shipping rates section disappears and I get a "TypeError: address is undefined" error from jquery.

    The renderShippingRates() is there as my attempt to make sure the shipping rates section doesn't disappear regardless of user action.

    FC.client.on("customer-shipping-option-select.done", function(params) {
    if (params.shipping_service_id == 59) {
    var remove = "";
    for (var i = 0; i < FC.json.items.length; i++) {
    console.log(FC.json.items[i].id)
    if (FC.json.items[i].category == "BOX") {
    remove = "&" + "quantity=0&" + "id=" + FC.json.items[i].id;
    FC.client.request("https://" + FC.settings.storedomain + "/cart?cart=update" + remove).done(function() {
    FC.cart.render();
    FC.checkout.renderShippingRates();
    });
    }
    }
    }else{
    FC.cart.render();
    FC.checkout.renderShippingRates();
    }
    });
  • fc_adamfc_adam FoxyCart Team
    @puremoxie,

    I think I know what's causing that. Could you give this version a try?
    FC.client.on("customer-shipping-option-select.done", function(params) {
    if (params.shipping_service_id == 59) {
    var shipping_results = FC.json.shipping_address.shipping_results,
    shipping_service_description = FC.json.shipping_address.shipping_service_description,
    shipping_service_id = FC.json.shipping_address.shipping_service_id;
    var remove = "";
    for (var i = 0; i < FC.json.items.length; i++) {
    if (FC.json.items[i].category == "BOX") {
    remove += "&" + i + ":quantity=0&" + i + ":id=" + FC.json.items[i].id;
    }
    }

    if (remove != "") {
    FC.client.request("https://" + FC.json.config.store_domain + "/cart?cart=update" + remove).done(function() {
    FC.json.shipping_address.shipping_results = shipping_results;
    FC.json.shipping_address.shipping_service_description = shipping_service_description;
    FC.json.shipping_address.shipping_service_id = shipping_service_id;
    FC[FC.json.context].render();
    FC[FC.json.context].getShippingOptions({address: FC.json.shipping_address});
    });
    }
    }
    });
Sign In or Register to comment.