/** * Return a stdClass object with the price difference and a CSV list of options. * $optionResult->priceDiff * $optionResult->options * @return object */ protected function _processOptionInfo($product, $optionInfo) { $valid_options = array(); if ($product->isGravityProduct()) { $valid_options = Cart66GravityReader::getFormValuesArray($product->gravity_form_id); } else { if (strlen($product->options_1) > 1) { $valid_options[] = explode(',', str_replace(' ', '', $product->options_1)); } if (strlen($product->options_2) > 1) { $valid_options[] = explode(',', str_replace(' ', '', $product->options_2)); } } $optionInfo = trim($optionInfo); $priceDiff = 0; $options = explode('~', $optionInfo); $optionList = array(); foreach ($options as $opt) { if (strlen($opt) >= 1) { Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Working with option: {$opt}\n" . print_r($valid_options, true)); // make sure product option is vallid $is_gravity_form = false; $check_option = str_replace(' ', '', $opt); if ($is_gravity_form = $product->isGravityProduct()) { $check_option = explode('|', $check_option); $check_option = $check_option[0]; } if ($this->_validate_option($valid_options, $check_option, $is_gravity_form)) { if (strpos($opt, '$')) { if (preg_match('/\\+\\s*\\$/', $opt)) { $opt = preg_replace('/\\+\\s*\\$/', '+' . CART66_CURRENCY_SYMBOL_TEXT, $opt); list($opt, $pd) = explode('+' . CART66_CURRENCY_SYMBOL_TEXT, $opt); $optionList[] = trim($opt); $priceDiff += $pd; } elseif (preg_match('/-\\s*\\$/', $opt)) { $opt = preg_replace('/-\\s*\\$/', '-' . CART66_CURRENCY_SYMBOL_TEXT, $opt); list($opt, $pd) = explode('-' . CART66_CURRENCY_SYMBOL_TEXT, $opt); $optionList[] = trim($opt); $pd = trim($pd); $priceDiff -= $pd; } else { $optionList[] = trim($opt); } } else { if (preg_match('/\\+\\s*\\' . CART66_CURRENCY_SYMBOL_TEXT . '/', $opt)) { $opt = preg_replace('/\\+\\s*\\' . CART66_CURRENCY_SYMBOL_TEXT . '/', '+' . CART66_CURRENCY_SYMBOL_TEXT, $opt); list($opt, $pd) = explode('+' . CART66_CURRENCY_SYMBOL_TEXT, $opt); $optionList[] = trim($opt); $priceDiff += $pd; } elseif (preg_match('/-\\s*\\' . CART66_CURRENCY_SYMBOL_TEXT . '/', $opt)) { $opt = preg_replace('/-\\s*\\' . CART66_CURRENCY_SYMBOL_TEXT . '/', '-' . CART66_CURRENCY_SYMBOL_TEXT, $opt); list($opt, $pd) = explode('-' . CART66_CURRENCY_SYMBOL_TEXT, $opt); $optionList[] = trim($opt); $pd = trim($pd); $priceDiff -= $pd; } else { $optionList[] = trim($opt); } } } else { throw new Exception("Invalid product option: {$opt}"); } } else { if (count($valid_options) > 0 && !$product->isGravityProduct()) { Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] no option"); throw new Exception("Product option is required"); } } } $optionResult = new stdClass(); $optionResult->priceDiff = $priceDiff; $optionResult->options = implode(', ', $optionList); return $optionResult; }
public static function increaseInventory($id, $variation = '', $qty = 1) { Cart66Common::log("Increasing Inventory: line " . __LINE__); // Build varation ikey string component if (!empty($variation)) { $variation = self::scrubVaritationsForIkey(str_replace(', ', '~', $variation)); } $p = new Cart66Product($id); $is_gravity_form = false; $valid_options = array(); if ($p->isGravityProduct()) { $valid_options = Cart66GravityReader::getFormValuesArray($p->gravity_form_id); $is_gravity_form = true; } else { if (strlen($p->options_1) > 1) { $valid_options[] = explode(',', str_replace(' ', '', $p->options_1)); } if (strlen($p->options_2) > 1) { $valid_options[] = explode(',', str_replace(' ', '', $p->options_2)); } } $newVariation = ''; $options = explode(',', $variation); foreach ($options as $option) { if ($p->validate_option($valid_options, $option, $is_gravity_form)) { $newVariation .= $option; } } $ikey = $p->getInventoryKey($newVariation); $count = $p->getInventoryCount($ikey); $newCount = $count + $qty; $p->setInventoryLevel($ikey, $newCount); }