public function setInitialVariation() { if ($this->hasVariations()) { $optionGroups = $this->getProductOptionGroups(); $optionItems = $this->getProductOptionItems(); $optionkeys = array(); foreach ($optionGroups as $optionGroup) { foreach ($optionItems as $option) { if ($option->getProductOptionGroupID() == $optionGroup->getID()) { $optionkeys[] = $option->getID(); break; } } } $this->setVariation(StoreProductVariation::getByOptionItemIDs($optionkeys)); } }
public function add($data) { $product = StoreProduct::getByID((int) $data['pID']); if (!$product) { return false; } if ($product->isExclusive()) { self::clear(); } //now, build a nicer "cart item" $cartItem = array(); $cartItem['product'] = array("pID" => (int) $data['pID'], "qty" => (int) $data['quantity']); unset($data['pID']); unset($data['quantity']); //since we removed the ID/qty, we're left with just the attributes $cartItem['productAttributes'] = $data; $removeexistingexclusive = false; foreach (self::getCart() as $k => $cart) { $cartproduct = StoreProduct::getByID((int) $cart['product']['pID']); if ($cartproduct && $cartproduct->isExclusive()) { self::remove($k); $removeexistingexclusive = true; } } $optionItemIds = array(); // search for product options, if found, collect the id foreach ($cartItem['productAttributes'] as $name => $value) { if (substr($name, 0, 3) == 'pog') { $optionItemIds[] = $value; } } if (!empty($optionItemIds)) { // find the variation via the ids of the options $variation = StoreProductVariation::getByOptionItemIDs($optionItemIds); // association the variation with the product if ($variation) { $options = $variation->getOptions(); if (count($options) == count($optionItemIds)) { // check if we've matched to a variation with the correct number of options $product->setVariation($variation); $cartItem['product']['variation'] = $variation->getID(); } else { return false; } } else { return false; // variation not matched } } elseif ($product->hasVariations()) { return false; // if we have a product with variations, but no variation data was submitted, it's a broken add-to-cart form } $cart = self::getCart(); $exists = self::checkForExistingCartItem($cartItem); if ($exists['exists'] === true) { $existingproductcount = $cart[$exists['cartItemKey']]['product']['qty']; //we have a match, update the qty if ($product->allowQuantity()) { $newquantity = $cart[$exists['cartItemKey']]['product']['qty'] + $cartItem['product']['qty']; if (!$product->isUnlimited() && !$product->allowBackOrders() && $product->getProductQty() < max($newquantity, $existingproductcount)) { $newquantity = $product->getProductQty(); } $added = $newquantity - $existingproductcount; } else { $added = 1; $newquantity = 1; } $cart[$exists['cartItemKey']]['product']['qty'] = $newquantity; } else { $newquantity = $cartItem['product']['qty']; if (!$product->isUnlimited() && !$product->allowBackOrders() && $product->getProductQty() < $newquantity) { $newquantity = $product->getProductQty(); } $cartItem['product']['qty'] = $newquantity; if ($product->isExclusive()) { $cart = array($cartItem); } else { $cart[] = $cartItem; } $added = $newquantity; } Session::set('vividstore.cart', $cart); return array('added' => $added, 'exclusive' => $product->isExclusive(), 'removeexistingexclusive' => $removeexistingexclusive); }