Zen Cart Logo
Forums / All Other Contributions/Addons / Stock by Attribute v4.0 addon for v1.3.5-1.3.9

Stock by Attribute v4.0 addon for v1.3.5-1.3.9

Views: 658,356

Results 3,021 to 3,040 of 3,609
18 Oct 2018, 22:53
#3021
cubmanky avatar

cubmanky

Zen Follower

Join Date:
Jan 2014
Posts:
224
Plugin Contributions:
0

Stock by Attribute v4.0 addon for v1.3.5-1.3.9

I ran the following sql files
Installation
Full/Upgrade DB Install
Full, makes all script changes to the database (DB) (i.e., adds new SBA table, adds entries into the Admin page, and new entries into the Configuration file).
Upgrade, updates Configuration file and the SBA table as needed. If run again, it will "Clean" table entries and reapply the settings, it will not affect current data in the "products_with_attributes_stock" table.

and Add all Product Attributes

19 Oct 2018, 03:28
#3022
cubmanky avatar

cubmanky

Zen Follower

Join Date:
Jan 2014
Posts:
224
Plugin Contributions:
0

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

So if for each item I set up the quantity for each color individual it will work. Attachment 18076
If I set just the pick color so it will run SBA Attachment 18077
it only shows pick color in stock and all others are out of stock.
Attachment 18078

19 Oct 2018, 16:55
#3023
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

cubmanky:

So I grabbed the files from https://github.com/mc12345678/Stock_By_Attributes_Combined and uploaded them to my site. I ran the above script and looking in and the attribute type is set to AV, Still no luck. On test product I have set the qty for each color individual. for test2 product I just made pick a color at 100 units I have in the out of stock that white is out. Still any color i click on will tell me out of stock when i try to click to add it to the cart.
Unfortunately I'm having to look into this as my tests being run in parallel with yours are also not working as expected. I've even gone into the class file and changed the setting that is supposed to determine the attribute stock presence by either first looking at the loaded stock or by looking at the non_stock table and ignore the SBA quantity variant.

I needed to look more at the code with the proposed setup in mind. I couldn't seem to recall if/how other settings came into play such as checking the stock level, reducing stock after purchase, allowance for overselling, etc... I knew that when it was first incorporated and publicized, it worked...

Mind you, when this was first incorporated the primary thought/concern was what was considered the "gift" option. A choice that could be made that would always be available when selecting other attributes that were related to the stock of the product... So, what that meant was that there would always be at least 2 attributes (2 option names) with every product except for some specific characteristic type attributes (Files and text boxes)... Well, development was considered for the single attribute product, but it was not completed. Further, I built in a sort of "safety" of overselling product by causing those such options to not support the non-stock characteristics. Could have left just a little more of a note to myself; however, had commented the area about what needed to be done not so much of how it behaved currently.

Anyways, if you go to includes/classes/class.products_with_attributes_class_stock.php and then modify line 1351 from:

        return false;

to:

        return true;

Then when the product has a single attribute and the option values/option names are identified as being non-stock, if the option value is not assigned to SBA, it will be possible to select it and add it to the cart. The total available will be identified as the quantity of product as entered in the product information page.

One thing about it though is that I had set aside some comments in that area of things I thought would be valuable to address to ensure that the data pushed would be validated. I'll get to it, but at least you can get through this issue now. There are a few things to consider as I realized when working with a single attribute that if the option value is not captured in SBA then a quantity is displayed, but when I tried to add the product to the cart, the cart notified that the option wasn't available, even though it appeared in the list. I've spent a great deal more on multiple attributes than single attributes, so need to go back and revisit that operation and functionality.

20 Oct 2018, 03:53
#3024
cubmanky avatar

cubmanky

Zen Follower

Join Date:
Jan 2014
Posts:
224
Plugin Contributions:
0

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

First I'm glad its not something on my end as you are having the issue.
I made the change and updated the file on my site
On my test2 product it shows all colors in stock. On white I have it still set as out of stock. I click white color and pick 5 then click add to cart It adds them but tells me 1 available.

20 Oct 2018, 04:03
#3025
cubmanky avatar

cubmanky

Zen Follower

Join Date:
Jan 2014
Posts:
224
Plugin Contributions:
0

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

SO I went in a deleted all variants for test2 then went back and added 100 to pick a color and I get the error Product 23 update failed: IT still shows 100 for each color in the dropdown window

20 Oct 2018, 18:24
#3026
jimmie avatar

jimmie

Totally Zenned

Join Date:
Jan 2013
Location:
New Port Richey, Florida
Posts:
969
Plugin Contributions:
0

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

when a product is out of stock, mark_product_out_of_stock it looks at products_quantity to determine this, A splash screen in red shows up and informs the customer, and the products are marked out of stock(Special Ordered) how can i also get it to look at the attribute stock, and show that same splash screen. some of the attributes are instock and some are not but the products quantity has quantity from the ones that do.
this is when no product quantity Attachment 18079
and this is no attribute quantity Attachment 18080

20 Oct 2018, 18:26
#3027
jimmie avatar

jimmie

Totally Zenned

Join Date:
Jan 2013
Location:
New Port Richey, Florida
Posts:
969
Plugin Contributions:
0

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

i modified a shipping module to detect out of stock to ship another way so having a problem looking at attribute stock to determine
ex. ```
// Variable holds information about the products in the order
$this->_products = $_SESSION['cart']->get_products();
$_SESSION['cart']->shopping_stock = 1;
$inStock = 0;
$outStock = 0;
$_SESSION['cart']->in_stock_products = [];
$_SESSION['cart']->out_stock_products = [];
foreach ($this->_products as $product) {
$pid = $product["id"];
$qty = $product["quantity"];
$product_query = "select products_quantity
from " . TABLE_PRODUCTS . "
where products_id = '" . (int)$pid . "'";
$product = $db->Execute($product_query);
$quantity = $product->fields["products_quantity"];

        if ($quantity < $qty){
            $_SESSION['cart']->shopping_stock = 2;
            array_push($_SESSION['cart']->out_stock_products,(int)$pid);
            $outStock = 1;

        }else{
            $_SESSION['cart']->shopping_stock = 1;
            array_push($_SESSION['cart']->in_stock_products,(int)$pid);
            $inStock = 1;
        }
	}
    if($outStock == 1 && $inStock == 1 && count($this->_products) > 0){
        $_SESSION['cart']->shopping_stock=3;
    }
21 Oct 2018, 01:17
#3028
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

jimmie:

i modified a shipping module to detect out of stock to ship another way so having a problem looking at attribute stock to determine
ex. ```
// Variable holds information about the products in the order
$this->_products = $_SESSION['cart']->get_products();
$_SESSION['cart']->shopping_stock = 1;
$inStock = 0;
$outStock = 0;
$_SESSION['cart']->in_stock_products = [];
$_SESSION['cart']->out_stock_products = [];
foreach ($this->_products as $product) {
$pid = $product["id"];
$qty = $product["quantity"];
$product_query = "select products_quantity
from " . TABLE_PRODUCTS . "
where products_id = '" . (int)$pid . "'";
$product = $db->Execute($product_query);
$quantity = $product->fields["products_quantity"];

        if ($quantity < $qty){
            $_SESSION['cart']->shopping_stock = 2;
            array_push($_SESSION['cart']->out_stock_products,(int)$pid);
            $outStock = 1;

        }else{
            $_SESSION['cart']->shopping_stock = 1;
            array_push($_SESSION['cart']->in_stock_products,(int)$pid);
            $inStock = 1;
        }
	}
    if($outStock == 1 && $inStock == 1 && count($this->_products) > 0){
        $_SESSION['cart']->shopping_stock=3;
    }

There's already a ZC function that determines the stock availability which SBA "hooks" into (reason the includes/functions/functions_lookups.php file requires modification in ZC 1.5.5 and below. In ZC 1.5.6 there is expected to be code that will be in place and won't require modification of that file). There is also a function in the includes/classes file added for SBA that also supports, but I would recommend using the modified SBA function if for any reason SBA is removed. 

The modification made to the function is to obtain/use the attributes of the product and from there determine if it is tracked by SBA and what the associated stock is. That function is:

zen_get_products_stock($products_id, $attributes = null, $dupTest = null)


You only need to pass the first two parameters. I'm not where I can easily identify (and at the moment don't remember) the makeup of the attributes portion off of a cart's entry to say what it takes to pull the attributes off of the product information. I'd have to get back to you if you don't know/find it.
21 Oct 2018, 03:13
#3029
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

Found it:

$_SESSION['cart']->contents[$products_id]['attributes']
or as used above:
$product['attributes']

So something like:

 $attributes = null;
if (isset($product['attributes']) {
  $attributes = $product['attributes'];
}

$quantity = zen_get_products_stock($products_id, $attributes);


21 Oct 2018, 13:02
#3030
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

mc12345678:

Found it:

$_SESSION['cart']->contents[$products_id]['attributes']
or as used above:
$product['attributes']

So something like:

$attributes = null;
if (isset($product['attributes'])) {
$attributes = $product['attributes'];
}

$quantity = zen_get_products_stock($products_id, $attributes);

> ***
> ***

Darnit, forgot a right parenthesis on the if statement line. Just happened to be looking over the code and see the imbalance.

$attributes = null;
if (isset($product['attributes'])) {
$attributes = $product['attributes'];
}

$quantity = zen_get_products_stock($products_id, $attributes);

22 Oct 2018, 01:44
#3031
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

jimmie:

when a product is out of stock, mark_product_out_of_stock it looks at products_quantity to determine this, A splash screen in red shows up and informs the customer, and the products are marked out of stock(Special Ordered) how can i also get it to look at the attribute stock, and show that same splash screen. some of the attributes are instock and some are not but the products quantity has quantity from the ones that do.
this is when no product quantity Attachment 18079
and this is no attribute quantity Attachment 18080
Finally being able to zoom in on these two images, I see that they are as later reported related to the shipping module, so hopefully the other assistance provided clarifies (or closely clarifies) this issue.

22 Oct 2018, 11:15
#3032
jimmie avatar

jimmie

Totally Zenned

Join Date:
Jan 2013
Location:
New Port Richey, Florida
Posts:
969
Plugin Contributions:
0

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

i will come back to this, very busy

22 Oct 2018, 21:59
#3033
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

cubmanky:

SO I went in a deleted all variants for test2 then went back and added 100 to pick a color and I get the error Product 23 update failed: IT still shows 100 for each color in the dropdown window
Couple of things.

SBA works off of two stock quantities, 1 for the specific attribute (attribute combination when more than one attribute is involved) and the quantity of product identified overall for the product.
Deleting the attribute(s) from the product and reassigning them does not change the non_stock table. This means that if an action hasn't been taken to remove the entries from the non-stock table, then the ones previously assigned remain in place and will cause the single dropdown option to appear to have the quantity available that is entered for the overall product once those attributes are again assigned to the product and the product assigned to SBA.
The recommendation I made a few days ago to change return false to return true will cause the shopping_cart page to only support a quantity of 1 existing in the cart, even though on the product page it will appear that there are more available. The "value" returned should be the quantity of product that are purchasable at that time (either some sort of total quantity of all available product or perhaps some sort of maximum per purchase. This portion of the code as previously said had not been flushed out. Even now with this new concept of using it there are additional caveats to consider.
If configuration->stock->subtract stock is true, then with each purchase, the total available quantity will decrease for both the attribute and the overall product quantity. So, recommend considering how the store's stock is used when trying to flush out operation. At the moment there is no individual setting against SBA that would prevent this decrease for either/both values other than the above described stock setting.

So I hope that with any combination of the above information at least there is some more understanding. Of course I'd like to finish off the section of code in question.

24 Oct 2018, 04:31
#3034
cubmanky avatar

cubmanky

Zen Follower

Join Date:
Jan 2014
Posts:
224
Plugin Contributions:
0

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

So I still have this changed to true

Anyways, if you go to includes/classes/class.products_with_attributes_class_stock.php and then modify line 1351 from:
Code:
return false;
to:
Code:
return true;
For test2 I have now added two attributes. the first one is pick type of vinyl, the second one is to pick color. It works, but the colors I have shown in zc_products_with_attributes_stock_attributes_non_stock is what it shows as in stock.

25 Oct 2018, 19:19
#3035
jimmie avatar

jimmie

Totally Zenned

Join Date:
Jan 2013
Location:
New Port Richey, Florida
Posts:
969
Plugin Contributions:
0

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

mc12345678:

Found it:

$_SESSION['cart']->contents[$products_id]['attributes']
or as used above:
$product['attributes']

So something like:

$attributes = null;
if (isset($product['attributes']) {
$attributes = $product['attributes'];
}

$quantity = zen_get_products_stock($products_id, $attributes);

> ***
> ***

How do i incorporate this into code above. if products qty 0 and products attribute stock 0 this item is nonstock.
if products qty 1 and products attribute stock 0 this item is nonstock,
if products qty 0 and products attribute stock 1 this item is instock, etc.
26 Oct 2018, 20:06
#3036
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

jimmie:

i modified a shipping module to detect out of stock to ship another way so having a problem looking at attribute stock to determine
ex. ```
// Variable holds information about the products in the order
$this->_products = $_SESSION['cart']->get_products();
$_SESSION['cart']->shopping_stock = 1;
$inStock = 0;
$outStock = 0;
$_SESSION['cart']->in_stock_products = [];
$_SESSION['cart']->out_stock_products = [];
foreach ($this->_products as $product) {
$pid = $product["id"];
$qty = $product["quantity"];
$product_query = "select products_quantity
from " . TABLE_PRODUCTS . "
where products_id = '" . (int)$pid . "'";
$product = $db->Execute($product_query);
$quantity = $product->fields["products_quantity"];

        if ($quantity < $qty){
            $_SESSION['cart']->shopping_stock = 2;
            array_push($_SESSION['cart']->out_stock_products,(int)$pid);
            $outStock = 1;

        }else{
            $_SESSION['cart']->shopping_stock = 1;
            array_push($_SESSION['cart']->in_stock_products,(int)$pid);
            $inStock = 1;
        }
    }
    if($outStock == 1 && $inStock == 1 && count($this->_products) > 0){
        $_SESSION['cart']->shopping_stock=3;
    }

> **mc12345678:**
>
> Darnit, forgot a right parenthesis on the if statement line. Just happened to be looking over the code and see the imbalance.
> ```
 $attributes = null;
if (isset($product['attributes'])) {
  $attributes = $product['attributes'];
}

$quantity = zen_get_products_stock($products_id, $attributes);

jimmie:

How do i incorporate this into code above. if products qty 0 and products attribute stock 0 this item is nonstock.
if products qty 1 and products attribute stock 0 this item is nonstock,
if products qty 0 and products attribute stock 1 this item is instock, etc.

Mind you, there is also a function that has been incorporated/modified for SBA called zen_check_stock, which returns a string of text if the product is out-of-stock (less than zero would remain if the provided quantity to be removed from the stock would cause the quantity to be less than zero).

The modified version of this call is: zen_check_stock($products_id, $quantity_to_try_to_obtain, $attributes);
In your case, the second parameter would be $qty.

While on the surface of things I would approach this differently, I have used the code I previously provided in the below since you have indicated that you want to know the actual numbers involved and not just whether the item would be in stock or not based on the program/code written to provide that information (zen_check_stock).

// Variable holds information about the products in the order
        $this->_products = $_SESSION['cart']->get_products();
        $_SESSION['cart']->shopping_stock = 1;
        $inStock = 0;
        $outStock = 0;
        $_SESSION['cart']->in_stock_products = [];
        $_SESSION['cart']->out_stock_products = [];
        foreach ($this->_products as $product) {
            $pid = $product["id"];
            $qty = $product["quantity"];
            /*$product_query = "select products_quantity
                          from " . TABLE_PRODUCTS . "
                          where products_id = '" . (int)$pid . "'";
                  $product = $db->Execute($product_query);
            $quantity = $product->fields["products_quantity"]; */

            $attributes = null;

            if (isset($product['attributes'])) {
              $attributes = $product['attributes'];
            }

            $quantityAttrib = zen_get_products_stock($products_id, $attributes);
            $quantityProd = zen_get_products_stock($products_id);

            /* Do whatever is necessary to mark product as in stock here in place of this comment block and then below take action if it is identified as out-of-stock  */
            $quantity = $qty;

            if (isset($attributes) && $quantityAttrib <= 0) {
             /* out-of-stock */
             /* This if block says that if there are attributes associated with the product (assuming all of the appropriate attributes have been provided)
                and those attributes have made it such that there are no attribute related product then the product is considered out-of-stock
                regardless of the quantity of the original product.
             */
             $quantity = $quantityAttrib;
            } else if (!isset($attributes) && $quantityProd <= 0) {
              /* out-of-stock */
              /* This block identifies that the product is out-of-stock based on not having attributes and the quantity available associated is out-of-stock.*/
              $quantity = $quantityProd;
            }
 
            if ($quantity < $qty){
                $_SESSION['cart']->shopping_stock = 2;
                array_push($_SESSION['cart']->out_stock_products,(int)$pid);
                $outStock = 1;

            }else{
                $_SESSION['cart']->shopping_stock = 1;
                array_push($_SESSION['cart']->in_stock_products,(int)$pid);
                $inStock = 1;
            }
        }
        if($outStock == 1 && $inStock == 1 && count($this->_products) > 0){
            $_SESSION['cart']->shopping_stock=3;
        }

Now, I note that here in the "shipping" page you are trying to assign/capture the current stock status. I don't know if the above is a one time only check or will be called again just before closing out the order during the order generation process. But there is a stock check evolution performed in checkout_confirmation processing to try to prevent an individual from checking out with product that have "just" been purchased by someone else. Further though, the session variables set here could become out-of-date if the purchase is not pushed through "quickly" and someone else purchases those items.

26 Oct 2018, 21:34
#3037
jimmie avatar

jimmie

Totally Zenned

Join Date:
Jan 2013
Location:
New Port Richey, Florida
Posts:
969
Plugin Contributions:
0

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

Thank you, u already know you are a coding genius, LOL
one more thing, when products are not in stock a banner in reds tells the customer this, but banner doesnt show with attributes out of stock

26 Oct 2018, 21:51
#3038
jimmie avatar

jimmie

Totally Zenned

Join Date:
Jan 2013
Location:
New Port Richey, Florida
Posts:
969
Plugin Contributions:
0

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

im sorry code did not work, i have a product attribute in cart with 0 Quantity, but product has 10000 quantity and it istrying to ship it as stock, should ship as a special order,(from Vendor)

26 Oct 2018, 22:05
#3039
jimmie avatar

jimmie

Totally Zenned

Join Date:
Jan 2013
Location:
New Port Richey, Florida
Posts:
969
Plugin Contributions:
0

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

Product in question https://jnsflooringandsupplies.com/index.php?main_page=product_info&products_id=13273:6695efab09dc5eb6cb860adc5a9a58d1
i disabled nonstock method from showing in shopping cart, i put a #1 in front of stock method title to know the different methods, there is a third shipping called stock an nonstock, when stock and nonstock are in cart.

26 Oct 2018, 23:24
#3040
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Stock by Attribute v4.0 addon for v1.3.5-1.3.9

I just was able to add a "special order" product to the cart and was provided a notification that there was special ordering required (when viewing shipping options from the shopping cart page), I added an instock product and had the same options overall, then removed the special order product and was provided a different series of shipping options. Don't recall which "special order" product I selected, but from my perspective and limited knowledge of the site, it seemed like something was handled correctly.