Change a product category in the cart

timconsidinetimconsidine Member
in Help edited May 2014
Hi.

I am seeking to go live, but have one final issue to resolve.
I have a product which has normally a download category, but I am using the Perch CMS to allow the user to select an option for CD copy before adding to cart.
Currently the cart / checkout only picks up the default downloads category and so obviously doesn't calculate shipping even though there is CD option requested.

So I am thinking to add some custom code to the cart to check for an option of CD, and if so, change the product category to "cddvd".

But the following doesn't work, probably due to some syntax error, as I don't understand JSON ?
<script type="text/javascript" charset="utf-8">
jQuery(document).ready(function(){

for (var p = 0; p < fc_json.products.length; p++) {
  if (fc_json.products[p].options[0,0] == "CD (audio)") {
   fc_json.products[p].category = "cddvd";
  }
 }
});

Can you suggest a fix ?

[ I am trying to avoid removing/adding products to avoid multiple products with different categories so as to reduce admin/config ]

[ Sample draft page at http://saladseminars.com/nlp-belief-busting-sleight-of-mouth-mastery.php ]
Comments
  • fc_adamfc_adam FoxyCart Team
    @timconsidine,

    The JSON cart object is read-only, so you can't make changes to it unfortunately. What I'd suggest doing is changing the category using our product option modifiers. You're already making use of them to change the price - you can also use them to change the category by using the "y:" prefix. For example: "{p+3|y:NewCode}" would increase the price by $3 and change the category to the one with a code of "NewCode". More details on the bottom of this page: http://wiki.foxycart.com/static/redirect/add_to_cart
  • timconsidinetimconsidine Member
    edited May 2014
    Thanks @fc_adam

    Good point about the product price modification. However, the Perch CMS which I am using does not expose the add-to-cart url very easily, so I am not sure how to access this.

    I thought about approaching it in the other direction. Set the category to be shippable and remove the shipping in the checkout if it is MP3. However my test to read the options is failing.

    I tried the array approach as in the first post.
    I also tried a string read approach like :
    var str = fc_json.products.options;
        if (str.contains("CD")) {
          // remove shipping
        }
    
    But it also doesn't work.
    So my question is what is the options structure in the JSON product structure, and how would I read it to test its contents ?

    Thanks for your advice - will see if I can get access to the Perch add-to-cart call.
  • fc_adamfc_adam FoxyCart Team
    @timconsidine,

    The products node of the JSON is an array, so you need to loop through them like you were previously. The custom attributes you set for products are included within the options as an array too - so something like this would work:
    for (var p = 0; p < fc_json.products.length; p++) {
      if (fc_json.products[p].options["Format"] == "CD (audio)") {
        // Your code
      }
    });
    

    In terms of the product option modifiers - how are you currently setting the price modifier on that dropdown?
  • That's great, thank you @fc_adam
    That will be great if I test for contents in checkout and modify shipping there.

    I'm using the out-of-box Perch CMS foxycart addon.
    This defines the product in Perch CMS, as you probably know.
    It allows you to add multiple product option series, and multiple values for each series.
    But it only allows option name and price modifier (oh and stock quantity which it adjusts based on FC callback).
    It does not have a category modifier.
    I'm looking at the code to see if I can make it handle one, but it's looking a non-trivial task.
  • fc_adamfc_adam FoxyCart Team
    @timconsidine,

    Maybe try setting the price modifier with the category modifier present, like this: "3|y:yourcode". That might work.

    You may also want to reach out to their support and put in a request for them to support all of the product option modifiers we allow.
  • timconsidinetimconsidine Member
    edited May 2014
    Great thinking @fc_adam!
    Sadly they define the field type and field data is converted to number on saving.

    Yes, I will need to see if they can modify it, as I am not keen to keen to modify their add-on. A lot can get broken !

    I think I will have to make category shippable, test for download in checkout and modify shipping.
    But I also need to check if there are any other shippable products (eg one download and one cd).

    Or admit defeat (which I am not good at !) and create separate products :-(
  • timconsidinetimconsidine Member
    edited May 2014
    Thanks @fc_adam for all your help on this.

    I have decided that I have to change the category to being a shippable product, and then correct if the selected product option is only a download. This seems easier for the case where there are other shippable products in the cart.

    Just to close the loop in case it helps anyone else researching this type of issue :
    I have added the following at the end of my custom shipping logic :
      var shipitem = 0;
      for (var p = 0; p < fc_json.products.length; p++) {
        if (fc_json.products[p].category == "carddecks") {
          shipitem +=1;
        }
        if (fc_json.products[p].category == "cddvd") {
          shipitem +=1;
        }
        if (fc_json.products[p].category == "SaladHostedDownloads") {
          shipitem +=1;
        }
        if (fc_json.products[p].options["Format"] == "MP3 (audio)") {
          shipitem -=1;
        }
      };
      if (shipitem < 1) {
        shippingCost = 0;
      }
    

    Basically checks every product in cart, increments shipitem counter for each shippable category (have excluded regular download only items from the checks) but reduces by one if product option is set as MP3 download, and if the resulting count is less than 1 (i.e. no shippable products), then sets shippingCost to zero, otherwise leaves it as already calculated.

    My case is based on one ship cost per shipment. Will need change if others are doing per product.
Sign In or Register to comment.