/** * Prepare selected options for configurable product * * @param Mage_Catalog_Model_Product $product * @param Varien_Object $buyRequest * @return array */ public function processBuyRequest($product, $buyRequest) { $superAttribute = $buyRequest->getSuperAttribute(); $superAttribute = is_array($superAttribute) ? array_filter($superAttribute, 'intval') : array(); $options = array('super_attribute' => $superAttribute); return $options; }
/** * Initialize product(s) for add to cart process * * @param Varien_Object $buyRequest * @param Mage_Catalog_Model_Product $product * @return mixed */ public function prepareForCart(Varien_Object $buyRequest, $product = null) { if ($attributes = $buyRequest->getSuperAttribute()) { $result = parent::prepareForCart($buyRequest, $product); if (is_array($result)) { $product = $this->getProduct($product); /** * $attributes = array($attributeId=>$attributeValue) */ if ($subProduct = $this->getProductByAttributes($attributes, $product)) { $product->addCustomOption('attributes', serialize($attributes)); $product->addCustomOption('product_qty_' . $subProduct->getId(), 1, $subProduct); $product->addCustomOption('simple_product', $subProduct->getId(), $subProduct); $_result = $subProduct->getTypeInstance(true)->prepareForCart($buyRequest, $subProduct); if (is_string($_result) && !is_array($_result)) { return $_result; } if (!isset($_result[0])) { return AO::helper('checkout')->__('Can not add item to shopping cart'); } /** * Adding parent product custom options to child product * to be sure that it will be unique as its parent */ if ($optionIds = $product->getCustomOption('option_ids')) { $optionIds = explode(',', $optionIds->getValue()); foreach ($optionIds as $optionId) { if ($option = $product->getCustomOption('option_' . $optionId)) { $_result[0]->addCustomOption('option_' . $optionId, $option->getValue()); } } } $_result[0]->setParentProductId($product->getId())->setCartQty(1); $result[] = $_result[0]; return $result; } } } return $this->getSpecifyOptionMessage(); }
/** * Copied from Peets_Catalog_Model_Product_Type_Configurable * */ protected function _prepareProduct(Varien_Object $buyRequest, $product, $processMode) { $attributes = $buyRequest->getSuperAttribute(); $isConfigurable = $product->getTypeId() == parent::TYPE_CODE; if ($attributes || !$this->_isStrictProcessMode($processMode) || $isConfigurable) { if (!$this->_isStrictProcessMode($processMode)) { if (is_array($attributes)) { foreach ($attributes as $key => $val) { if (empty($val)) { unset($attributes[$key]); } } } else { $attributes = array(); } } $result = Mage_Catalog_Model_Product_Type_Abstract::_prepareProduct($buyRequest, $product, $processMode); if (is_array($result)) { $product = $this->getProduct($product); /** * $attributes = array($attributeId=>$attributeValue) */ $subProduct = true; if ($this->_isStrictProcessMode($processMode)) { foreach ($this->getConfigurableAttributes($product) as $attributeItem) { /* @var $attributeItem Varien_Object */ $attrId = $attributeItem->getData('attribute_id'); if (!isset($attributes[$attrId]) || empty($attributes[$attrId])) { $subProduct = null; break; } } } if ($subProduct) { $subProduct = $this->getProductByAttributes($attributes, $product); } if ($subProduct) { $product->addCustomOption('attributes', serialize($attributes)); $product->addCustomOption('product_qty_' . $subProduct->getId(), 1, $subProduct); $product->addCustomOption('simple_product', $subProduct->getId(), $subProduct); $_result = $subProduct->getTypeInstance(true)->_prepareProduct($buyRequest, $subProduct, $processMode); if (is_string($_result) && !is_array($_result)) { return $_result; } if (!isset($_result[0])) { return Mage::helper('checkout')->__('Cannot add the item to shopping cart'); } /** * Adding parent product custom options to child product * to be sure that it will be unique as its parent */ if ($optionIds = $product->getCustomOption('option_ids')) { $optionIds = explode(',', $optionIds->getValue()); foreach ($optionIds as $optionId) { if ($option = $product->getCustomOption('option_' . $optionId)) { $_result[0]->addCustomOption('option_' . $optionId, $option->getValue()); } } } $_result[0]->setParentProductId($product->getId())->addCustomOption('parent_product_id', $product->getId()); if ($this->_isStrictProcessMode($processMode)) { $_result[0]->setCartQty(1); } $result[] = $_result[0]; return $result; } else { if (!$this->_isStrictProcessMode($processMode) || $isConfigurable) { return $result; } } } } return $this->getSpecifyOptionMessage(); }
/** * Implement for compatibility with Magento pre 1.5.0 CE. * * @param Varien_Object $buyRequest * @param null $product * @return array|string */ public function prepareForCart(Varien_Object $buyRequest, $product = null) { $product = $this->getProduct($product); $productsInfo = $buyRequest->getSuperGroup(); if (!empty($productsInfo) && is_array($productsInfo)) { $products = array(); $associatedProducts = $this->getAssociatedProducts($product); if ($associatedProducts) { foreach ($associatedProducts as $subProduct) { if (isset($productsInfo[$subProduct->getId()])) { $qty = $productsInfo[$subProduct->getId()]; if (!empty($qty) && is_numeric($qty)) { /* BEGIN Brim Grouped-Options Customizations */ if ($subProduct->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) { $isConfigurable = true; $configBuyRequest = clone $buyRequest; $superAttr = $buyRequest->getSuperAttribute(); $configBuyRequest->setSuperAttribute($superAttr[$subProduct->getId()]); $configBuyRequest->setQty($qty); $_result = $subProduct->getTypeInstance(true)->prepareForCart($configBuyRequest, $subProduct); } else { /* END Brim Grouped-Options Customizations */ $isConfigurable = false; $clonedBuyRequest = clone $buyRequest; $superOptions = $buyRequest->getSuperOptions(); if ($superOptions && isset($superOptions[$subProduct->getId()])) { $clonedBuyRequest->setOptions($superOptions[$subProduct->getId()]); } if ($subProduct->getHasOptions() && count($subProduct->getOptions()) == 0) { foreach ($subProduct->getProductOptionsCollection() as $_option) { $subProduct->addOption($_option); } } $_result = $subProduct->getTypeInstance(true)->prepareForCart($clonedBuyRequest, $subProduct); } if (is_string($_result) && !is_array($_result)) { return $_result; } if (!isset($_result[0])) { return Mage::helper('checkout')->__('Cannot add the item to shopping cart.'); } /* BEGIN Brim Grouped-Options Customizations */ if ($isConfigurable) { foreach ($_result as $item) { $products[] = $item; } } else { /* END Brim Grouped-Options Customizations */ $_result[0]->setCartQty($qty); $_result[0]->addCustomOption('product_type', self::TYPE_CODE, $product); $newBuyRequest = array('super_product_config' => array('product_type' => self::TYPE_CODE, 'product_id' => $product->getId())); if (isset($clonedBuyRequest)) { if ($clonedBuyRequest->getOptions()) { $newBuyRequest['options'] = $clonedBuyRequest->getOptions(); } if ($clonedBuyRequest->getQty()) { $newBuyRequest['qty'] = $clonedBuyRequest->getQty(); } } $_result[0]->addCustomOption('info_buyRequest', serialize($newBuyRequest)); $products[] = $_result[0]; } } } } } if (count($products)) { return $products; } } return Mage::helper('catalog')->__('Please specify the quantity of product(s).'); }
/** * Initialize product(s) for add to cart process * * @param Varien_Object $buyRequest * @return unknown */ public function prepareForCart(Varien_Object $buyRequest) { if ($attributes = $buyRequest->getSuperAttribute()) { $result = parent::prepareForCart($buyRequest); if (is_array($result)) { $product = $this->getProduct(); /** * $attributes = array($attributeId=>$attributeValue) */ if ($subProduct = $this->getProductByAttributes($attributes)) { $product->addCustomOption('attributes', serialize($attributes)); $product->addCustomOption('product_qty_' . $subProduct->getId(), 1, $subProduct); $product->addCustomOption('simple_product', $subProduct->getId(), $subProduct); $_result = $subProduct->getTypeInstance()->prepareForCart($buyRequest); if (is_string($_result) && !is_array($_result)) { return $_result; } if (!isset($_result[0])) { return Mage::helper('checkout')->__('Can not add item to shopping cart'); } $_result[0]->setParentProductId($product->getId())->setCartQty(1); $result[] = $_result[0]; return $result; } } } return Mage::helper('catalog')->__('Please specify the product option(s)'); }