Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2011
    Posts
    9
    Plugin Contributions
    0

    Default "Subtract stock" individually

    I wonder if there is any way to select individually for each product if stock should be substracted or not when a customer makes a purchase?

    I know there is a "Subtract stock" setting in admin, which can be set to true or false, but this affects all products.

    (What I really want is ta be able to handle compound products, since I sell both assembled products and their parts, so when someone buys an assembled product (assembled from i e 5 parts), then all parts it is made of should also have the stock substracted. But I have already understood that no e-commerce systems can handle that, it is not specific for zencart.)

    My main problem is that I sell a lot sometimes of the parts, and perhaps during the night all my stock is sold but the shop still show that I have a stock, so people might keep on ordering, and get disappointed when I tell them it has actually just gone out of stock (for a while). So an acceptable way for me to handle this would be to have the "Subtract stock" function for the parts I sell a lot of.

    Another option is to hide stock and set stock value to "infinity" for items I don't want to be checked for stock amount. A drawback with this is that then customers have no idea if my stock is 1 or 1000.

    If I was able to set minimum stock level individually, that would have the same effect.

    I suppose I could try to hack the php code but since it is individual I would have to add a variable for each product (which says if stock shall be fixed or not) and that goes a bit beyond my knowledge of php (I've only done minor tweaking).

    I fear this is not the most common problem in the world so perhaps there is no solution except hacking? If so, does anyone have a link that describes for dummies how to add a variable?

  2. #2
    Join Date
    Jan 2004
    Posts
    66,364
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: "Subtract stock" individually

    Here's an idea.

    Three steps here:

    1. /includes/classes/order.php
    around line 715 or so, find this line
    Code:
            $this->notify('NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_BEGIN');
    and replace it with the following:
    Code:
            $this->stock_query = $stock_values;
            $this->notify('NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_BEGIN', array('i'=>$i));
            $stock_values = $this->stock_query;
    2. Add a new file: /includes/auto_loaders/config.observer_bypass_stock_decrementing.php
    Code:
    <?php
    /**
     *
     * @package plugins
     * @copyright Copyright 2003-2012 Zen Cart Development Team
     * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
     */
    /**
     * Designed for v1.3.9 and v1.5.0
     */
    if (!defined('IS_ADMIN_FLAG')) {
     die('Illegal Access');
    }
    $autoLoadConfig[190][] = array('autoType'=>'class',
                                  'loadFile'=>'observers/class.observer_bypass_stock_decrementing.php');
    $autoLoadConfig[190][] = array('autoType'=>'classInstantiate',
                                  'className'=>'observer_bypass_stock_decrementing',
                                  'objectName'=>'observer_bypass_stock_decrementing');

    3. Add a new file: /includes/classes/observers/class.observer_bypass_stock_decrementing.php
    PHP Code:
    <?php
    /**
     * @package plugins
     * @copyright Copyright 2003-2012 Zen Cart Development Team
     * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
     *
     * Designed for v1.5.0, compatible with v1.3.9
     */

    class observer_bypass_stock_decrementing extends base {

      function 
    __construct() {
        
    $this->attach($this, array('NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_BEGIN'));

        
    /**
         * INSTRUCTIONS:
         * The following array needs a list of product ID numbers to bypass when handling inventory reductions. Values are separated by commas:
         * MAKE EDITS INSIDE THE array() below:
         */
        
    $this->arrayOfProductsToByPass = array(3,44,18,10,1);
      }

      
    /**
       * The following is fired on every product that is added to the order, just before the stock-reduction is run.
       */
      
    function update(&$class$eventID$paramsArray = array())
      {
        global 
    $db// this is needed later
        
    $i $paramsArray['i']; // this is needed in order for the following code to know which product is being processed at the moment.
        /**
         * The following line checks to see if the product ID being added is in the list of products to bypass.
         */
        
    if (in_array($class->products[$i]['id'], $this->arrayOfProductsToByPass)) {
          
    /**
           * If the above test passes, then the following line will basically tell the normal stock-reduction code that there's nothing to do.
           * (technically it fudges the code so that the product ID for which it's expecting to decrease stock is not found, which therefore triggers a normal bypass)
           */
          
    $class->stock_query $db->Execute("SELECT * from " TABLE_PRODUCTS " where products_id = 0");
        }
        
    /**
         * Many other things could be done here as well, including a completely different stock-handling routine.
         * One could do more detailed checks for attributes present, reduce inventory on various related items, etc.
         * All the data is readable from the $class->products[$i] array.
         */
    //     uncomment the following line to dump a list of available data to the screen for testing (not on a live site of course)
    //     die('<pre>Current product data: ' . print_r($class->products[$i], TRUE));
      
    }
    }
    If this is helpful for you, donations welcome: www.zen-cart.com/donate
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  3. #3
    Join Date
    Apr 2011
    Posts
    9
    Plugin Contributions
    0

    Default Re: "Subtract stock" individually

    Wow that was fast! Doesn't work though, but perhaps I did something wrong?

    I copied the 2 new files to the locations you instructed, exchanged the line in order.php, changed my store's "substract stock" from false to true, changed array to include IDs for products for which I want stock to be fixed, and then made an order in the shop on various products, both things that should be decremented and not. But all were decremented.

    By "product ID" I suppose you mean the value I see e g at the end of the line as an ordinary customer (after "products_id="), so that in this example the ID is 45:
    http://mysite.se/webshop/index.php?m...products_id=45

    So I just replaced "array(3,44,18,10,1)" with "array(58,59,60,61,54,55,56,57,87,50,47,49,48,53,51,52,95,103,111,112,74,46,72)" . I assume the numers don't have to come in order since yours don't.

    Is there something I have to "refresh" or some cahce to empty or something like that?

    And I couldn't think of a way to test without doing it for real so I put a real order in the store (and then tidied up the mess it made afterwards). Is there some way to test that doesn't require a real order to be made?

    Of course I will make a donation if this works. I didn't imagine I could get so detailed help and so fast!

  4. #4
    Join Date
    Jan 2004
    Posts
    66,364
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: "Subtract stock" individually

    Hmmm ...

    I did several tests which worked correctly ... on my v1.5.0 demo site. And core code dependencies for this bit of code are unchanged between 1.3.9 and 1.5.0.
    You are correct about the array being product ID numbers and in no particular order. And yes unfortunately to test requires placing actual orders.
    I just did some more tests with different product numbers, and it works here for both products with attributes and without.

    Quote Originally Posted by DavidGGG View Post
    I copied the 2 new files to the locations you instructed, exchanged the line in order.php
    Did you add the 2 lines as well, one above and one below the original $this->notify that also needed editing? All the stuff in red needed to be added.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  5. #5
    Join Date
    Apr 2011
    Posts
    9
    Plugin Contributions
    0

    Default Re: "Subtract stock" individually

    Yep, I replaced

    $this->notify('NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_BEGIN');

    with

    $this->stock_query = $stock_values;
    $this->notify('NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_BEGIN', array('i'=>$i));
    $stock_values = $this->stock_query;


    I use 1.3.9h. (I was considering upgrading to 1.5.0 but I have a list of ~50 php changes I made and afaik I would have to do most of that all over again so I will stick with 1.3.9 for a long time.)

    Is there any way to do "fault finding" that you can suggest? E g you wrote "uncomment the following line..." in a comment, but I don't understand exactly when the array will be visible and what I should look for. I don't have a demo site and I imagine it must be live for testing but I can do it at night when the customers are sleeping..

    I think for both phpbb and Concrete5 it was sometimes necessary to empty some cache or refresh or delete some files in some situations (after upgrades and other changes) or else it would have no effect. But there's no such voodoo with zc?

  6. #6
    Join Date
    Jan 2004
    Posts
    66,364
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: "Subtract stock" individually

    No, there's no cache to reset for this.

    The file in the autoloaders folder hooks into the bootstrap system and causes the corresponding observer class file to be loaded and the class instantiated. Both of those files must be in place for it to work.
    The code edits in the order.php file intercept the normal inventory handling by allowing a reset of the normal SQL list of products whose stock are to be decremented.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  7. #7
    Join Date
    Apr 2011
    Posts
    9
    Plugin Contributions
    0

    Idea or Suggestion Re: "Subtract stock" individually

    Whoa sorry, my mistake(s)!

    Yesterday I first I forgot to edit the array before I made the test order. And now I realize I also forgot to upload my local order.php.. So now it works!!! Or - it worked all along, just not in my hands.. Still have to get used to having all files both locally and uploaded I guess..

    Another thing that confused me is that stock level doesn't stop at 0. If someone orders more than I have in stock then they get a warning but can place the order, that's the way I want it, but then I'd like stock to be 0, not negative. Doesn't seem to be a setting for that. Well maybe that's within my capabilities as php programmer to fix. Not very important either, just confusing for customers I think with minus signs in stock numbers.

    Thanks a whole bunch! I'll check out that donate button now.

  8. #8
    Join Date
    Jan 2004
    Posts
    66,364
    Blog Entries
    7
    Plugin Contributions
    274

    Default Re: "Subtract stock" individually

    Haha, been there. Gotta remember to upload the files ;)


    As for not showing negative stock, there are a few ways to handle this:
    a) If it's less than 0, display 0 instead
    b) If it's less than 0, skip displaying stock level at all
    c) If it's less than 0, replace the number with the phrase "out of stock", or maybe an image instead

    You can do all of those by editing this line in your custom tpl_product_info_display.php template file, with custom code depending on your need:
    Code:
      <?php echo (($flag_show_product_info_quantity == 1) ? '<li>' . $products_quantity . TEXT_PRODUCT_QUANTITY . '</li>'  : '') . "\n"; ?>
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  9. #9
    Join Date
    Apr 2011
    Posts
    9
    Plugin Contributions
    0

    Default Re: "Subtract stock" individually

    Thanks. I want option "a" and I fixed it by replacing $products_quantity by (($products_quantity < 0 ) ? 0 : $products_quantity)

    (I'm not too bad at C programming but a newbie at php..)

    Thought I'd post it in case some other poor dude wants it this way.

  10. #10
    Join Date
    Jun 2006
    Posts
    298
    Plugin Contributions
    0

    Default Re: "Subtract stock" individually

    This is exactly what I'm looking for, except I need it for categories instead of individual products. Could you be so kind as to tell me what I would change to set it by category instead of by individual product?

    Thank you!

    Quote Originally Posted by DrByte View Post
    Here's an idea.

    Three steps here:

    1. /includes/classes/order.php
    around line 715 or so, find this line
    Code:
            $this->notify('NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_BEGIN');
    and replace it with the following:
    Code:
            $this->stock_query = $stock_values;
            $this->notify('NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_BEGIN', array('i'=>$i));
            $stock_values = $this->stock_query;
    2. Add a new file: /includes/auto_loaders/config.observer_bypass_stock_decrementing.php
    Code:
    <?php
    /**
     *
     * @package plugins
     * @copyright Copyright 2003-2012 Zen Cart Development Team
     * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
     */
    /**
     * Designed for v1.3.9 and v1.5.0
     */
    if (!defined('IS_ADMIN_FLAG')) {
     die('Illegal Access');
    }
    $autoLoadConfig[190][] = array('autoType'=>'class',
                                  'loadFile'=>'observers/class.observer_bypass_stock_decrementing.php');
    $autoLoadConfig[190][] = array('autoType'=>'classInstantiate',
                                  'className'=>'observer_bypass_stock_decrementing',
                                  'objectName'=>'observer_bypass_stock_decrementing');

    3. Add a new file: /includes/classes/observers/class.observer_bypass_stock_decrementing.php
    PHP Code:
    <?php
    /**
     * @package plugins
     * @copyright Copyright 2003-2012 Zen Cart Development Team
     * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
     *
     * Designed for v1.5.0, compatible with v1.3.9
     */

    class observer_bypass_stock_decrementing extends base {

      function 
    __construct() {
        
    $this->attach($this, array('NOTIFY_ORDER_PROCESSING_STOCK_DECREMENT_BEGIN'));

        
    /**
         * INSTRUCTIONS:
         * The following array needs a list of product ID numbers to bypass when handling inventory reductions. Values are separated by commas:
         * MAKE EDITS INSIDE THE array() below:
         */
        
    $this->arrayOfProductsToByPass = array(3,44,18,10,1);
      }

      
    /**
       * The following is fired on every product that is added to the order, just before the stock-reduction is run.
       */
      
    function update(&$class$eventID$paramsArray = array())
      {
        global 
    $db// this is needed later
        
    $i $paramsArray['i']; // this is needed in order for the following code to know which product is being processed at the moment.
        /**
         * The following line checks to see if the product ID being added is in the list of products to bypass.
         */
        
    if (in_array($class->products[$i]['id'], $this->arrayOfProductsToByPass)) {
          
    /**
           * If the above test passes, then the following line will basically tell the normal stock-reduction code that there's nothing to do.
           * (technically it fudges the code so that the product ID for which it's expecting to decrease stock is not found, which therefore triggers a normal bypass)
           */
          
    $class->stock_query $db->Execute("SELECT * from " TABLE_PRODUCTS " where products_id = 0");
        }
        
    /**
         * Many other things could be done here as well, including a completely different stock-handling routine.
         * One could do more detailed checks for attributes present, reduce inventory on various related items, etc.
         * All the data is readable from the $class->products[$i] array.
         */
    //     uncomment the following line to dump a list of available data to the screen for testing (not on a live site of course)
    //     die('<pre>Current product data: ' . print_r($class->products[$i], TRUE));
      
    }
    }
    If this is helpful for you, donations welcome: www.zen-cart.com/donate

 

 

Similar Threads

  1. v152 Segregating "In Stock" and "Out of Stock" items
    By Peter M Dodge in forum Templates, Stylesheets, Page Layout
    Replies: 1
    Last Post: 3 Dec 2013, 12:42 PM
  2. Replies: 0
    Last Post: 27 Nov 2011, 06:04 PM
  3. Replies: 20
    Last Post: 26 Oct 2011, 12:21 AM
  4. "Always" in stock & "one-off" stock items
    By Fidz in forum Setting Up Categories, Products, Attributes
    Replies: 3
    Last Post: 13 Feb 2010, 12:16 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