Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2006
    Posts
    7
    Plugin Contributions
    0

    Default Using Numeric Values And Dependant Attributes

    Here's to save some pain (but still requires a text editor).

    For the ones out there, with absolutely no php skills and stuck with those out-of-this-world products, such as a cartboard box, that requires a price calculaded from any value in lenght, height, and width: there is hope. (The following modifications have been tested with zencart 1.3.0, it is assumed that backward compatibility is not a myth.)


    First thing to do, is BACKUP, then open the file located at:

    /includes/functions/functions_prices.php

    locate the comment/line that says: "// calculate letters" (about line 1179)
    Just under that comment, there should be a function definition that should look like:

    function zen_get_letters_count($string, $free=0) {

    while (strstr($string, ' ')) $string = str_replace(' ', ' ', $string);

    $string = trim($string);

    if (TEXT_SPACES_FREE == '1') {

    $letters_count = strlen(str_replace(' ', '', $string));

    } else {

    $letters_count = strlen($string);

    }

    if ($letters_count - $free >= 1) {

    return ($letters_count - $free);

    } else {

    return 0;

    }

    }


    In order for your text input attributes to process numeric values instead of series of letters we have to purge some things up there.

    IMPORTANT NOTE: if you do modify this function, know that every "text per letter" attribute, for now on and ever, will use the direct data input by the user. If you still wish to use

    So, the modification is (with very few error handling):

    function zen_get_letters_count($string, $free=0) {

    if ($string > 0)
    {
    return ($string);
    } else {
    return 0;
    }
    }


    Just below, there's another function that should look like this one:

    function zen_get_letters_count_price($string, $free=0, $price) {

    $letters_price = zen_get_letters_count($string, $free) * $price;

    if (&#036;letters_price <= 0) {

    return 0;

    } else {

    return &#036;letters_price;

    }

    }


    It determines a price based on the sum of letters, we have to make it spit out a price simply based on the input times the price hence, simply replace it with:

    function zen_get_letters_count_price(&#036;string, &#036;free=0, &#036;price) {
    &#036;letters_price = &#036;string * &#036;price;
    if (&#036;letters_price <= 0) {
    return 0;
    } else {
    return &#036;letters_price;
    }
    }


    Now to the scotch tape part.

    If you need to calculate a price relatively to the product of your text attributes values in the form of "price = price x attribute1 x attribute2 x attribute3 ..." then follow the next modifications.

    IMPORTANT NOTE: if you do apply the following modifications, know that every "text per letter" attribute, for now on and ever, will multiply itself with the other "text per letter" attributes assigned for a specific product. If you still wish to use text attributes that do not multiply themselves together, you can always sacrifice the "text per words attributes"; we&#39;ll cover the necessary modifications in the so called: annex A.

    First, open the file located at: /includes/classes/shopping_cart.php


    Here we need to replace two peices of code. At about line 561, you should a line that look like this:

    &#036;this->total += &#036;qty * zen_add_tax(&#036;text_letters, &#036;products_tax);


    It&#39;s used for calculating the products sub-total with taxes applied. We replace it with:


    if(&#036;this->total == 0)
    {
    &#036;this->total += (&#036;qty * &#036;text_letters);

    if(count(&#036;this->contents[&#036;products_id][&#39;attributes&#39;]) == 1)
    {
    &#036;this->total += &#036;this->total * 0.15;
    }
    }
    else
    {
    &#036;this->total *= &#036;qty * &#036;text_letters;
    &#036;this->total += &#036;this->total * 0.15;
    }

    What we do here is simply modify the += operator for using multiplication instead of addition. Note that since we want
    to calculate our products prices ONLY from their parametric attributes and that, indeed, the initial price should be set to 0, I calculate the taxe in another manner then the one used by default. This is what the 0.15 stands for, a tax rate which you can replace with your own.

    Next, you need to replace the lined used to calculate the raw prices total. Look around line 694, where you should find the
    following peice of code:

    &#036;attributes_price += &#036;text_letters;


    Replace it with:

    if(&#036;attributes_price == 0)
    {
    &#036;attributes_price = &#036;text_letters;
    }
    else
    {
    &#036;attributes_price *= &#036;text_letters;
    }


    You should now be set to process parametric products with text attributes that multiply each other. Other attributes such
    as check boxes, radio buttons, well be added or substracted in the usual way. If you wish to multiply EVERY attributes against each other, you should look at line 961 where there is the line:

    &#39;final_price&#39; => (&#036;products_price + &#036;this->attributes_price(&#036;products_id)),

    simply replace it with:

    &#39;final_price&#39; => (&#036;products_price * &#036;this->attributes_price(&#036;products_id)),


    And that&#39;s it.

    PLEASE. Don&#39;t email me for comments, bugs and all. Use this forum.


    ANNEX A.

    IMPORTANT NOTE: I didn&#39;t tested if these modifications works as I say. Experiment at your own risk.

    To use text atrributes that won&#39;t multiply with each others, (by using the per word attribute)
    open the file located at:

    /includes/functions/functions_prices.php

    locate the comment/line that says: "// calculate words" (about line 1179)
    Just under that comment, there should be a function definition that should look like:

    function zen_get_word_count(&#036;string, &#036;free=0) {
    if (&#036;string &#33;= &#39;&#39;) {
    while (strstr(&#036;string, &#39; &#39;)) &#036;string = str_replace(&#39; &#39;, &#39; &#39;, &#036;string);
    &#036;string = trim(&#036;string);
    &#036;word_count = substr_count(&#036;string, &#39; &#39;);
    return ((&#036;word_count+1) - &#036;free);
    } else {
    // nothing to count
    return 0;
    }
    }


    Replace it with:


    function zen_get_word_count(&#036;string, &#036;free=0) {

    if (&#036;string > 0)
    {
    return (&#036;string);
    } else {
    return 0;
    }
    }

    And replace the function just below with:


    function zen_get_words_count_price(&#036;string, &#036;free=0, &#036;price) {
    &#036;words_price = &#036;string * &#036;price;
    if (&#036;letters_price <= 0) {
    return 0;
    } else {
    return &#036;words_price;
    }
    }

  2. #2
    Join Date
    Apr 2006
    Posts
    7
    Plugin Contributions
    0

    Default

    ok, I didn&#39;t tested much the modification and just found one bug.

    the following modification in includes/classes/shopping_cart.php

    if(&#036;this->total == 0)
    {
    &#036;this->total += (&#036;qty * &#036;text_letters);

    if(count(&#036;this->contents[&#036;products_id][&#39;attributes&#39;]) == 1)
    {
    &#036;this->total += &#036;this->total * 0.15;
    }
    }
    else
    {
    &#036;this->total *= &#036;qty * &#036;text_letters;
    &#036;this->total += &#036;this->total * 0.15;
    }

    shoud be replaced with:


    if(&#036;this->total == 0)
    {
    &#036;this->total += (&#036;qty * &#036;text_letters);

    if(count(&#036;this->contents[&#036;products_id][&#39;attributes&#39;]) == 1)
    {
    &#036;this->total += &#036;this->total * 0.15;
    }
    }
    else
    {
    &#036;this->total *= &#036;text_letters;
    &#036;this->total += &#036;this->total * 0.15;
    }

    Bug was that quantity of object was multiplied twice

    other reports should follow....

  3. #3
    Join Date
    May 2006
    Posts
    2
    Plugin Contributions
    0

    Default

    I&#39;m trying to implement this on my website and am running into a few snags.

    I have a page where I have customers use 2 text fields to input length and width for a piece priced by square inch.

    After updating the code as described above, it works fine so far...

    Then I add another text field to allow the customer to input instructions for customization. Now the price shows up in the shopping cart as 0.

    I need the third "instructions" text box to be disregarded in computing the price. How do I get this to happen? Some guidance on how to use the price per word workaround would be helpful, I think.

    Any help would be appreciated.

  4. #4
    Join Date
    Apr 2006
    Posts
    7
    Plugin Contributions
    0

    Default

    nissiana,

    The reason why you get a zero total appears to be because the third attribute you use has a value of 0 and is multiplied with the preceding attribute&#39;s values. Here&#39;s a little work around: set the last attribute&#39;s value to price per letter = 1. The final price result shall be unchanged since it&#39;s multiplied by one and you&#39;ll get to keep the user&#39;s data.


    As I said above, the modifications I suggested are merely a scotch tape patch, the code in itself is far from clean. If I ever get enough time to write a proper module, be assured I&#39;ll do so.

    Hope it would help.

  5. #5
    Join Date
    Apr 2006
    Posts
    7
    Plugin Contributions
    0

    Default

    nissiana,

    The reason why you get a zero total appears to be because the third attribute you use has a value of 0 and is multiplied with the preceding attribute&#39;s values. Here&#39;s a little work around: change the function zen_get_letters_count:

    function zen_get_letters_count(&#036;string, &#036;free=0) {

    if (&#036;string > 0)
    {
    return (&#036;string);
    } else {
    return 0;
    }
    }

    to something like:


    function zen_get_letters_count(&#036;string, &#036;free=0)
    {
    if (is_numeric(&#036;string))
    {
    return (&#036;string);
    }
    else
    {
    return 1;
    }
    }

    Then go to your third attribute in admin pannel and set it&#39;s price per letter to 1. The changes we&#39;ve made to the function will now multiply the preceding attributes by 1 (thus leaving the result unchanged) if the user enter&#39;s something that&#39;s not plainly a numeric value in the third attribute box.

    As I said above, the modifications I suggested are merely a scotch tape patch, the code in itself is far from clean. If I ever get enough time to write a proper module, be assured I&#39;ll do so.

    Hope it would help.

  6. #6
    Join Date
    Apr 2006
    Posts
    7
    Plugin Contributions
    0

    Default

    nissiana,

    One last thing (wish I could edit my posts&#33;)

    There is a chance that since we&#39;ve made out function return a value of one for if text is entered, you might only get "1" as the user input&#39;s values in the cart. If it&#39;s the case, let me know, the simplest solution would be to use a custom text per word attribute that won&#39;t mess with our text per letters ones. I&#39;ll tell you how.

  7. #7
    Join Date
    May 2006
    Posts
    2
    Plugin Contributions
    0

    Default

    strav,

    Thanks for the assistance&#33; I&#39;ve given the new function several tries on the test page, setting just the price per letter to 1 and also with price per word to 1 just to cover that possibility. The logic of the function makes sense to me, but it&#39;s still registering a price of 0 in the shopping cart. What next?

  8. #8
    Join Date
    Jul 2008
    Posts
    9
    Plugin Contributions
    0

    Default Re: Using Numeric Values And Dependant Attributes

    Hi I'm looking into doing something somewhat similar and can't find a solution yet.

    I need to set up a quantity box next to 3 attributes (Small, Medium, Large), and then do a total of what's in there...which would multiply the total cost of the order by the price of the product.
    S: text box
    M: text box
    L: text box
    ------
    total X item price

    Should be easy no??

    Can anyone help with this??

    Thanks in advance

    Jonathan

  9. #9
    Join Date
    Nov 2008
    Posts
    1
    Plugin Contributions
    0

    Default Re: Using Numeric Values And Dependant Attributes

    I'm not sure if anyone is still following this thread but I need a little help.
    I have followed the instructions and the solution works a treat for a store that sells blinds however when I add more than one blind to the cart it multiplies the two subtotals together.

    i.e.
    blind 1 - $200 ($20*$10)
    blind 2 - $200 ($20*$10)

    subtotal should be $400 ($200+$200)

    subtotal output is $40,000 ($200*$200)

    I cannot find the line that tells the attributes to multiply so I can change it to add. Thank you for your help in advance

  10. #10
    Join Date
    Dec 2009
    Location
    Amersfoort, The Netherlands
    Posts
    2,845
    Plugin Contributions
    25

    Default Re: Using Numeric Values And Dependant Attributes

    I installed this md, but am encountering the same problem.

    In the shopping cart multiple items are multiplied. The strange thing is, when i am continuing the check out the correct amount is used.

    Is there anyone with a solution?

 

 

Similar Threads

  1. v151 Best way to display multiple dependant attributes with stock? - eg Colour AND Size
    By kyates in forum Setting Up Categories, Products, Attributes
    Replies: 2
    Last Post: 28 Mar 2013, 02:16 PM
  2. Using Numeric Values And Dependant Attributes
    By Ropelocker in forum Setting Up Categories, Products, Attributes
    Replies: 5
    Last Post: 26 Nov 2007, 08:17 PM
  3. Dependant Attributes
    By Wrongsauce in forum Setting Up Categories, Products, Attributes
    Replies: 1
    Last Post: 26 Jul 2007, 08:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg