1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Post Price 0.1

Discussion in 'Plugins' started by objective, Jul 23, 2010.

  1. objective

    objective New Member Donor

    Post Price
    Version: 0.1
    Author: Objective
    Created on: 2010/07/23
    Last Updated: 2010/07/23
    Tested on: (Hotaru 1.3)
    Plugins Required: Submit
    Plug & Play: Yes
    Supported: No

    This plugin adds a price field to Submit Step 2, filters it to ensure it is a valid number and displays it in the post extras.

    1. Upload the "post_price" folder to your plugins folder. Install it from Plugin Management in Admin.
    2. Modify the language file if you need to change from ? to a different currency symbol.

    Revision History
    Version 0.1 2010/07/23 - Objective - Released first version

    Please click "Rate Thread" and give this plugin a rating. Thank you.


    Attached Files:

  2. epoch

    epoch New Member

    nice one objective. I am working on a coupon site this will come in handy.
  3. edwardlobo

    edwardlobo New Member

    expiry date

    I am also working on a coupon site. It would be good to have an expiry date as well after which the post expires
  4. zduniacz

    zduniacz New Member

    This is my first post in the forums, so hello everyone!

    Would you know if there is a way to make the plugin accept other price digits separator than a dot?

    What I mean is that my users commonly type the price in this manner: 2,99 instead of 2.99, which the plugin expects. As a result, the price shown in the post is equal to 0.00

    Here is the code from the plugin that I believe would need to be amended somehow:
            if ($h->cage->post->keyExists('post_price')) {
                $h->post->vars['price'] = $h->cage->post->testFloat('post_price');
            } else {
                // use existing setting unless blank, in which case set default:
                if (!$h->post->vars['price']) { $h->post->vars['price'] = '0.00'; } // default
    Any ideas ? :)
  5. PuckRobin

    PuckRobin New Member

    Try replacing this line:
    $h->post->vars['price'] = $h->cage->post->testFloat('post_price');
    With this one:
    $h->post->vars['price'] = $h->cage->post->testRegex( 'post_price', '/(^\d*\,?\d*[0-9]+\d*$)|(^[0-9]+\d*\,\d*$)/' );
    This change will also fix negative price issue, which is missing in original code I guess.

    Also replace 0.00 with 0,00 of course.

    Note: I didn't analyse the plugin completely, since I did not use it at all. I just suggest for the codes you wrote here. If there are other checks in the plugin, you should correct them as well.
  6. zduniacz

    zduniacz New Member

    Thank you for replying and suggesting a fix, PuckRobin.

    I tested your code. It does half the job - it respects the "," as a separator, however, it converts all digits after the "," to "00". For example, when I put 14,25 I get 14,00...

    I will have a look at the rest of the code to see if there are perhaps other places where the price gets affected..
  7. PuckRobin

    PuckRobin New Member

    The fix only checks if the format of the input is acceptable. It does not change anything. The code that displays it is the guilty one :) Find this line:

    printf ("%.2f", $price);
    Change it as:

    echo number_format( str_replace( ",", ".", $price ), 2, ",", "" );
    You can see other number formatting options here:
    Last edited: Jun 23, 2011
  8. zduniacz

    zduniacz New Member

    Still no luck with this code :/

    Do you think the reason might be that the value of $price is stored in the db field 'post_price' which is of a FLOAT type?

    Having this line in my script:
    $h->post->vars['price'] = $h->cage->post->testRegex( 'post_price', '/(^\d*\,?\d*[0-9]+\d*$)|(^[0-9]+\d*\,\d*$)/' );
    I looked at data in the post_price field in database and the values I see there are only plain numbers(no decimals), i.e. if I type 14,49 the vaule stored in the db is shrinked to plain 14.

    This would explain why the price displayed on the website is "14,00" - because it takes '14' from the db, right? :)
  9. PuckRobin

    PuckRobin New Member

    You are right. It expects float format (xx.yy) to save it to the database. Find the line:
    $h->vars['submitted_data']['submit_price'] = $h->post->vars['price'];
    and replace it with:
    $h->vars['submitted_data']['submit_price'] = str_replace( ",", ".", $h->post->vars['price'] );
    The other changes stay.
  10. zduniacz

    zduniacz New Member

    Hi PuckRobin,

    Sorry for late reply, I was on vacation and didnt have time to test it sooner. I did it yesterday and it works perfect! Thanks for your helpful advice.
  11. UncleDanger

    UncleDanger New Member

    Is there a way to move the price field from the bottom of submit form 2 to second from the top? As in just under the title field?
  12. UncleDanger

    UncleDanger New Member

    Okay, I solved it. I have no idea how to explain this in a technical way, but here goes.

    So post-price adds its price field to submit_2_fields, so I went into phpmyadmin and changed it to submit_3_fields, then changed the corresponding submit_2_fields line in post_price.php to submit_3_fields.

    Then at line 49 I inserted:

    <div class="form-group">
    <label for='submitPrice' >Price <br></label>
    <?php $h->pluginHook('submit_3_fields'); ?>

    And it worked!
  13. valMETNG

    valMETNG Administrator Staff Member Admin

    I'm not familiar with this plugin but looking at the code suggests that you'd have to customize the submit plugin to do so. The function submit_2_fields in post_price.php is the code that adds this field to the submit 2 page, and is run by line 80 of \content\plugins\submit\templates\submit2.php which looks like:
    <?php $h->pluginHook('submit_2_fields'); ?>
    Thus, if you move that code (line 80) earlier (e.g., to line 48), it will run that function earlier. Just be aware that by doing this, if you run other plugins that also add additional fields to the submit 2 page, those added fields will also then come right after title too (i.e., you'd need to customize the code more to move additional fields to different places).
  14. UncleDanger

    UncleDanger New Member

    As always, when one problem is solved another emerges.

    If a post doesn't have a price, the plugin gives it a price of 0.00 - is there a way to have this not happen at all, for it just to stay blank?
  15. valMETNG

    valMETNG Administrator Staff Member Admin

    Looking at the code, I believe if you add the following at line 155, you should get the desired result:
    if (!$h->post->vars['post_row']->post_price) { return; }
  16. UncleDanger

    UncleDanger New Member

    Okay, that works great, thank you!

    However, when I edit an item, to change the price from an existing price to no price, it only ever retains the existing price. I can change from no price to an existing price without any problem.
  17. valMETNG

    valMETNG Administrator Staff Member Admin

    I'm guessing the problem is with line 92 (because if the price = 0, the update doesn't occur). Try commenting out line 92 in post_price.php by changing it to:
    // if (!isset($h->vars['submitted_data']['submit_price'])) { return false; }
    ...and then changing line 94 to:
    $h->post->vars['price'] = (!isset($h->vars['submitted_data']['submit_price']) ? 0 : $h->vars['submitted_data']['submit_price'];
  18. UncleDanger

    UncleDanger New Member

    That causes an HTTP 500 error?
  19. valMETNG

    valMETNG Administrator Staff Member Admin

    My bad. Forgot a close parenthesis:
    $h->post->vars['price'] = (!isset($h->vars['submitted_data']['submit_price'])) ? 0 : $h->vars['submitted_data']['submit_price'];
  20. UncleDanger

    UncleDanger New Member

    Okay, the site's up again, but it's just doing the same thing.

    I have slightly edited my submit_edit.php file, might that have something to do with it?

    I added this at line 47:

    <?php $h->pluginHook('submit_3_fields'); ?>

Share This Page