How to change categories of products in cart during checkout?

jlahijanijlahijani Member
in Help edited September 2014
Hello,

A store I'm building (using FoxyCart 2.0) has 2 payment methods:
1. PayPal
2. Purchase Order

It also has 2 shipping methods:
1. Flat Shipping Per Product ($75 per product); FoxyCart handles this via Product Categories
2. Customer Pickup (Free); FoxyCart handles this as a "Custom" shipping option

My store requires a relationship between Payment and Shipping methods:
1. if PayPal is chosen, then Flat Shipping Per Product must apply
2. if Purchase Order is chosen, then Custom Pickup must apply

The question I have is, during checkout, when a customer chooses 1 of the 2 payment methods, how do I apply (or remove) the custom Product Category which sets the flat shipping per product price of each product in the cart?

Some sample code would be great, or even an explanation of a starting point.

Thank you,

Jonathan
Tagged:
Comments
  • fc_adamfc_adam FoxyCart Team
    @jlahijani,

    Unfortunately it's actually not a simple process to alter the product category of a product - although it's not super hard at the same time. You need to remove the existing product from the cart and re-add them with a different product category using JSONP (http://wiki.foxycart.com/static/redirect/jsonp).

    Perhaps a simpler approach would be to use a javascript snippet to apply dynamic flat rates, dictated by what products are in the cart. We're almost finished porting over the existing flat rate shipping snippet to 2.0, at which point you'll be able to plug that in.

    If you wanted to stay using the categories though - you will need to add a change event handler to the payment gateway radio inputs with jQuery, and then using JSONP remove the relevant product and re-add it with the correct category.
  • OK... how about the following approach...

    If PayPal is chosen, then a Product is added to the cart which represents the Shipping. So, the value of this Shipping "product" / line item would be the quantity of all the products in the cart * 75.

    If Purchase Order is chosen, then the shipping line item just described is removed.

    Please advise if this is possible, easier and possible approach.

  • fc_adamfc_adam FoxyCart Team
    @jlahijani,

    That approach is certainly possible as well - and would be simpler than adding and removing in that it would just be a single JSONP call rather than two. Similar to the other approach you'd apply the logic in a change event handler on the payments radio inputs.
  • I'm rusty with the JSONP approach. Would it be possible to provide a starting point with some initial code so I can build off of it? Just need a little kickstart.
  • fc_adamfc_adam FoxyCart Team
    @jlahijani,

    The following code will add a shipping product to the cart if the customer selects PayPal, and will remove it if they select a purchase order. One thing it won't do though is remove an existing shipping product and re-add it if the customer changes their products, but hopefully this will give you a starting point for it.
    jQuery("[name='fc_payment_method']").change(function() {
      var shippingProduct = -1;
      for (var i = 0; i < FC.json.items.length; i++) {
        if (FC.json.items[i].name == "Shipping") {
          shippingProduct = i;
        }
      }
      var cartAction = 'https://'+FC.settings.storedomain+'/cart?';
      if (this.value == "paypal") {
        if (shippingProduct == -1) {
          cartAction += 'name=Shipping&price=' + (FC.json.item_count * 75);
        }
      } else {
        if (shippingProduct > -1) {
          cartAction += 'cart=update&quantity=0&id=' + FC.json.item[i].id;
        }
      }
    
      FC.client.request(cartAction).done(function(dataJSON) { FC.checkout.render(); });
    });
    
Sign In or Register to comment.