public static function checkInventoryOnAddToCart()
 {
     $result = array(true);
     $itemId = Cart66Common::postVal('cart66ItemId');
     $options = '';
     $optionsMsg = '';
     $opt1 = Cart66Common::postVal('options_1');
     $opt2 = Cart66Common::postVal('options_2');
     if (!empty($opt1)) {
         $options = $opt1;
         $optionsMsg = trim(preg_replace('/\\s*([+-])[^$]*\\$.*$/', '', $opt1));
     }
     if (!empty($opt2)) {
         $options .= '~' . $opt2;
         $optionsMsg .= ', ' . trim(preg_replace('/\\s*([+-])[^$]*\\$.*$/', '', $opt2));
     }
     $scrubbedOptions = Cart66Product::scrubVaritationsForIkey($options);
     if (!Cart66Product::confirmInventory($itemId, $scrubbedOptions)) {
         $result[0] = false;
         $p = new Cart66Product($itemId);
         $counts = $p->getInventoryNamesAndCounts();
         $out = '';
         if (count($counts)) {
             $out = '<table class="inventoryCountTableModal">';
             $out .= '<tr><td colspan="2"><strong>' . __('Currently In Stock', 'cart66') . '</strong></td></tr>';
             foreach ($counts as $name => $qty) {
                 $out .= '<tr>';
                 $out .= "<td>{$name}</td><td>{$qty}</td>";
                 $out .= '</tr>';
             }
             $out .= '</table>';
         }
         $soldOutLabel = Cart66Setting::getValue('label_out_of_stock') ? strtolower(Cart66Setting::getValue('label_out_of_stock')) : __('out of stock', 'cart66');
         $result[1] = $p->name . " " . $optionsMsg . " is {$soldOutLabel} {$out}";
     }
     $result = json_encode($result);
     echo $result;
     die;
 }
 protected function _updateQuantitiesFromPost()
 {
     $qtys = Cart66Common::postVal('quantity');
     if (is_array($qtys)) {
         foreach ($qtys as $itemIndex => $qty) {
             $item = $this->getItem($itemIndex);
             if (!is_null($item) && is_object($item) && get_class($item) == 'Cart66CartItem') {
                 if ($qty == 0) {
                     Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Customer specified quantity of 0 - remove item.");
                     $this->removeItem($itemIndex);
                 }
                 if (Cart66Product::confirmInventory($item->getProductId(), $item->getOptionInfo(), $qty)) {
                     $this->setItemQuantity($itemIndex, $qty);
                 } else {
                     $qtyAvailable = Cart66Product::checkInventoryLevelForProduct($item->getProductId(), $item->getOptionInfo());
                     $this->setItemQuantity($itemIndex, $qtyAvailable);
                     if (!Cart66Session::get('Cart66InventoryWarning')) {
                         Cart66Session::set('Cart66InventoryWarning', '');
                     }
                     $inventoryWarning = Cart66Session::get('Cart66InventoryWarning');
                     $inventoryWarning .= '<div class="alert-message alert-error  Cart66Unavailable">' . __("The quantity for", "cart66") . ' ' . $item->getFullDisplayName() . " " . __("could not be changed to", "cart66") . " {$qty} " . __("because we only have", "cart66") . " {$qtyAvailable} " . __("in stock", "cart66") . ".</div>";
                     Cart66Session::set('Cart66InventoryWarning', $inventoryWarning);
                     Cart66Common::log("Quantity available ({$qtyAvailable}) cannot meet desired quantity ({$qty}) for product id: " . $item->getProductId());
                 }
             }
         }
     }
 }