Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14
  1. #11
    Join Date
    Jul 2012
    Posts
    16,734
    Plugin Contributions
    17

    Default Re: How to write a piece of code for a product id total?

    Quote Originally Posted by marcopolo View Post
    Is it possible for you to do the code for the stock way, I'm stuck like I said it's returning 0.00 so it's not working or giving me any errors to see why.

    The fundraiser has started and I would like to have it display the amount. Below is the way I think it would work:

    Check current stock of products_id that = 1200
    then subtract that output from 1000
    Then multiple that output by 12
    Then populate var currentAmount below with the number:

    <script type="text/javascript">
    var currentAmount = 1367;
    </script>
    Well, looking over my code from before, I do see that there were two "obvious" issues with it. The while loop doesn't actually step through the process (doesn't increment if it is/was executed, and also there was no variable really to check as part of the beginning).. I was going to ask that some troubleshooting data be added, which I show below; however, it seems that there were some bugs in the code.
    Code:
    $start_datetime = '2020-03-20 05:00:00";
    $end_datetime = date(DATE_TIME_FORMAT, time()); // Is an example of checking up until now although that portion could be left off if a running total was wanted and just need to return all values greater than the start date. Else, put in the end date as desired.
    $quantity_sold_query = "SELECT op.products_quantity, op.products_price, op.products_tax, op.final_price, o.currency, o.currency_value FROM " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o WHERE op.products_id = :products_id: AND op.orders_id = o.orders_id AND o.date_purchased > STR_TO_DATE(:start_datetime:, :datetimeformat:) AND o.date_purchased < STR_TO_DATE(:end_datetime:, :datetimeformat:) ORDER BY o.date_purchased ASC"; 
    // ORDER BY ASC provides list with oldest purchase (by date) at the top and newest at the bottom. 
    
    $quantity_sold_query = $db->bindVars($quantity_sold_query, ":products_id:", 1200, 'integer');
    $quantity_sold_query = $db->bindVars($quantity_sold_query, ":start_datetime:", $start_datetime, 'string');
    $quantity_sold_query = $db->bindVars($quantity_sold_query, ":end_datetime:", $end_datetime, 'string');
    $quantity_sold_query = $db->bindVars($quantity_sold_query, ":datetimeformat:", DATE_TIME_FORMAT, 'string');
    
    $quantity_sold = $db->Execute($quantity_sold_query);
    
    //Since ZC version is not identified, a generic loop is offered instead of the options available as of ZC 1.5.5 where one can 
    //  loop on foreach instead of while (!$quantity_sold->EOF)
    
    $money = 0;
    $subtract_amount = 10;
    while (!$quantity_sold->EOF) {
      //For troubleshooting uncomment the below:
      // trigger_error('Sold Data: ' . print_r($quantity_sold, true), E_USER_WARNING);
      // For troubleshooting uncomment the above:. To comment it out again add two forward slashes at the start of the line(s): I.e. //
      $amount_sale = $currencies->value($quantity_sold->fields['final_price'] * $quantity_sold->fields['products_quantity'], true, $quantity_sold->fields['currency'], $quantity_sold->fields['currency_value']);
      $amount_deducted = $subtract_amount * $quantity_sold->fields['products_quantity'];
      $amount_sale -= $currencies->value($amount_deducted, true, DEFAULT_CURRENCY);
      // If item was free or some other characteristic caused it to be free, then do not remove more from the funds collected.
      if ($amount_sale < 0) {
        $amount_sale = 0;
      }
      $money += $amount_sale;
      $quantity_sold->MoveNext();
    }
    
    // To display just the number(s) use the below:
    echo $money;
    // To display as a currency:
    // echo $currencies->format($money);
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  2. #12
    Join Date
    May 2008
    Posts
    442
    Plugin Contributions
    1

    Default Re: How to write a piece of code for a product id total?

    It is returning 0
    no errors even after removing // from the troubleshooting line. I did have to change:
    PHP Code:
    $start_datetime '2020-03-20 05:00:00"; 
    to

    PHP Code:
    $start_datetime '2020-03-20 05:00:00'

  3. #13
    Join Date
    Jul 2012
    Posts
    16,734
    Plugin Contributions
    17

    Default Re: How to write a piece of code for a product id total?

    So then, I would suggest removing portions of the WHERE clause for starters... if the trigger_error portion is not executing at all, then that means that the query returns no values to process... I would just start by removing the portions associated with before and after dates in the first query line. Everything else can stay.

    So this:
    Code:
    $quantity_sold_query = "SELECT op.products_quantity, op.products_price, op.products_tax, op.final_price, o.currency, o.currency_value FROM " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o WHERE op.products_id = :products_id: AND op.orders_id = o.orders_id AND o.date_purchased > STR_TO_DATE(:start_datetime:, :datetimeformat:) AND o.date_purchased < STR_TO_DATE(:end_datetime:, :datetimeformat:) ORDER BY o.date_purchased ASC";
    Could be changed to this:
    Code:
    $quantity_sold_query = "SELECT op.products_quantity, op.products_price, op.products_tax, op.final_price, o.currency, o.currency_value FROM " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_ORDERS . " o WHERE op.products_id = :products_id: AND op.orders_id = o.orders_id ORDER BY o.date_purchased ASC";
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  4. #14
    Join Date
    May 2008
    Posts
    442
    Plugin Contributions
    1

    Default Re: How to write a piece of code for a product id total?

    Ok that made it work, which is fine since this is a new item there is no sales history so I do not need the date portion of the code anyways. Thanks again for taking the time to help me complete this and getting it to work I appreciate it very much!

 

 
Page 2 of 2 FirstFirst 12

Similar Threads

  1. v155 This piece of code works with php5.6 but crashes site with all versions of php7
    By patrioticflags in forum Templates, Stylesheets, Page Layout
    Replies: 10
    Last Post: 4 Mar 2020, 09:22 PM
  2. Help with code piece
    By yaseent in forum Code Collaboration
    Replies: 16
    Last Post: 18 Feb 2018, 03:44 AM
  3. Help a newbie install a piece of javascript with product vars on a product page
    By jhkaplan in forum Templates, Stylesheets, Page Layout
    Replies: 18
    Last Post: 1 May 2012, 05:35 PM
  4. How to write error trapping code on create account?
    By 0be1 in forum General Questions
    Replies: 0
    Last Post: 29 Oct 2007, 01:36 PM
  5. Can anyone write a simple code for me?
    By eaglewu in forum Templates, Stylesheets, Page Layout
    Replies: 0
    Last Post: 16 Mar 2007, 11:57 PM

Bookmarks

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
Zen-Cart, Internet Selling Services, Klamath Falls, OR