
Originally Posted by
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.
Code:
// 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;
}

Originally Posted by
mc12345678
Darnit, forgot a right parenthesis on the if statement line. Just happened to be looking over the code and see the imbalance.
Code:
$attributes = null;
if (isset($product['attributes'])) {
$attributes = $product['attributes'];
}
$quantity = zen_get_products_stock($products_id, $attributes);

Originally Posted by
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).
Code:
// 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.
Bookmarks