virtual product inventory function

/** * @param int $product_id * * @return int */ public function get_inventory_available($product_id = 0) { // use global if you must, but consider a private variable // referenced during instantiation set private local to the class global $class_product, $class_inventory_transaction, $class_virtual_product; $return = 0; $product_id = intval($product_id); $product = $class_product->get_by_id($product_id); if($product['is_virtual_product'] == 1) { // get all virtual product items and quantity required // verify how many times they can be added up to create a qty of 1 // add the 1 recursively and return the number $virtual_items = $class_virtual_product->get_by_parent_id($product_id); $qty_needed = array(); $items = array(); if(count($virtual_items) > 0) { foreach($virtual_items as $virtual_item) { $qty_needed[$virtual_item['product_id']] = $virtual_item['quantity']; } } if(count(($qty_needed)) > 0) { foreach($qty_needed as $product_needed_id => $product_needed_qty) { $product_needed_id = intval($product_needed_id); $transactions = $class_inventory_transaction->get_by_product_id($product_needed_id); $product_onhand = intval($transactions[0]['total_on_hand']); if($product_onhand >= $product_needed_qty) { $needed = $product_onhand / $product_needed_qty; $items[$product_needed_id] = floor($needed); } else { $items[$product_needed_id] = 0; } } } if(count($items) > 0) { $return = min($items); } } else { // find out how many items based on inventory transactions $return = $class_inventory_transaction->get_total_available($product_id); } $return = intval($return); return $return; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.