fc_cart_item_price_each

dasharddashard Member
in Bugs & Feature Requests edited October 2010
I'm looking to pass back to the cart a modified 'each' price. How to do?

I have products sold by the yard. My script is taking the user-selected lengths and multiplying that by the cost/per and passing that back fine. What I'm getting for the 'each' price is the total, and I want the 'each' price to be the cost/per price.

Hence, 12 yards of $10/yard product will show a total of $120.00, and a price of $10/each.

How do I pass back the latter from my script? What do I talk to?

Thanks in advance.
Comments
  • brettbrett FoxyCart Team
    I could be missing it here, but I think it could be as simple as just doing something like:
    <form ...>
    <input type="text" name="quantity" />
    <input type="hidden" name="price" value="10" />
    <input type="hidden" name="name" value="Fabric (1 yard)" />
    </form>
    

    So your product is 1 yard at $10. Quantity of 12 is 12 yards. Yeah?

    This has actually come up before where it made a difference in terms of how the fabric was cut. So you might actually want quantity 2 of 5 yard lengths. That gets a little more involved with some javascript, but if that's what you need we could try to find that thread.
  • dasharddashard Member
    edited October 2010
    No no no… you're missing me (or I was unclear). the code all works. It passes to the cart beautifully. What I want to customize is the "price each" that displays under the total in the cart (hence the title of the thread.)

    See image…
    5192451_7b1f.png
  • fc_adamfc_adam FoxyCart Team
    @dashard, hows your handle on javascript? You could pass the actual price per as a product attribute, and using javascript put that value into the price each area on the right on cart load. Note that you'd need to do it on the checkout too
  • what would be the attribute? if you take a look at one of the pages in question, say here, you can take a look at the page's linked javascript by viewing the source. It's messy right now and still a work in progress, but you can see what's happening in there.

    Yes, I can calculate and then pass back an attribute, just need to know what the attribute is and where to do what. Thanks.
  • ahh… would that include JS in the template? pass the attribute from my script file to the cart, and then add JS to the template to dupe the value into 'fc_cart_item_price_each' ?? Please advise.
  • fc_adamfc_adam FoxyCart Team
    Ok, so basically you'd add a hidden input to each form, just like your price, but give it a name of something like "price_per" (don't use "price_each", thats used by FoxyCart already). Then when you do your calculations update that input's value as needed to match the actual price per for the ordered product.

    Then in your cart template, you'd loop through the fc_json.products, grab the fc_json.products[ i ].options["price_per"] and replace the html in the ".fc_cart_item_price_each" element.

    Does that make sense?
  • Intellectually, yes, but I've never dipped my toe into the json waters. Can you provide a sample of code to do the looping? What am I looping through?
    I could use some code direction here. Thanks in advance.
  • fc_adamfc_adam FoxyCart Team
    edited October 2010
    So on your cart page, you'd do something like this:
    for (var p = 0; p < fc_json.products.length; p++) {
    // check that price_per exists for this product
    if (typeof fc_json.products[p].options["price_per"] != "undefined") {
    // price_per exists, so replace the corresponding price_each span
    $(".fc_cart_item_price_each")[p].html("($"+fc_json.products[p].options["price_per"]+" each)");
    }
    }
    

    Note that I haven't tested this, but it should work.
Sign In or Register to comment.