public function prepareForCart(Varien_Object $buyRequest, $product = null) { $product = $this->getProduct($product); $data = $buyRequest->getData(); $product->addCustomOption('src_option', $data['src_option']); return parent::prepareForCart($buyRequest, $product); }
/** * @magentoDataFixture Mage/Catalog/_files/product_simple.php */ public function testPrepareForCartOptionsException() { $product = new Mage_Catalog_Model_Product(); $product->load(1); // fixture $this->assertEquals('Please specify the product required option(s).', $this->_model->prepareForCart(new Varien_Object(), $product)); }
public function prepareForCart(Varien_Object $buyRequest, $product = null) { if (!$product) { $product = $this->getProduct(); } return parent::prepareForCart($buyRequest, $product); }
public function prepareForCart(Varien_Object $buyRequest, $product = null) { if (!method_exists(get_parent_class($this), 'prepareForCartAdvanced')) { $result = parent::prepareForCart($buyRequest, $product); if (is_string($result)) { return $result; } $this->_prepareProduct($buyRequest, $product, null); return $result; } return parent::prepareForCart($buyRequest, $product); }
public function prepareForCart(Varien_Object $buyRequest, $product = null) { if (version_compare(Mage::getVersion(), '1.5.0', '>=')) { return parent::prepareForCart($buyRequest, $product); } if (is_null($product)) { $product = $this->getProduct(); } $result = parent::prepareForCart($buyRequest, $product); if (is_string($result)) { return $result; } reset($result); $product = current($result); $result = $this->_prepareWebPOSProduct($buyRequest, $product); return $result; }
/** * 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(); }
/** * Prepares product for cart according to buyRequest. * * @param Varien_Object $buyRequest * @param object $product [optional] * @return */ public function prepareForCart(Varien_Object $buyRequest, $product = null, $old = false) { if (!$product->getAwSarpEnabled()) { if (!$old) { return parent::prepareForCart($buyRequest, $product); } return; } Mage::getModel('sarp/product_type_default')->checkPeriod($product, $buyRequest); /* * For creating order from admin * If product is added to cart from admin, we doesn't add sart custom options to it. */ $Period = Mage::getModel('sarp/period'); if ($product->getAwSarpPeriod()) { if (count(explode(",", $product->getAwSarpPeriod())) === 1) { $date = Mage::getModel('sarp/period')->load($product->getAwSarpPeriod())->getNearestAvailableDay(); $product->setAwSarpSubscriptionType($product->getAwSarpPeriod()); $product->setAwSarpSubscriptionStart($date->toString(), Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT)); } } /* We should add custom options that doesnt exist */ if ($buyRequest->getAwSarpSubscriptionType()) { if ($Period->load($buyRequest->getAwSarpSubscriptionType())->getId()) { $product->addCustomOption('aw_sarp_subscription_type', $Period->getId()); } } else { if ($product->getAwSarpSubscriptionType()) { $buyRequest->setAwSarpSubscriptionType($product->getAwSarpSubscriptionType()); $product->addCustomOption('aw_sarp_subscription_type', $product->getAwSarpSubscriptionType()); $Period->setId($product->getAwSarpSubscriptionStart()); } } if ($this->requiresSubscriptionOptions($product) && !$Period->getId()) { $date = Mage::app()->getLocale()->date(); } $options = $buyRequest->getOptions(); if (isset($options['aw_sarp_subscription_start']) && is_array($options['aw_sarp_subscription_start']) && !empty($options['aw_sarp_subscription_start']['day']) && !empty($options['aw_sarp_subscription_start']['month']) && !empty($options['aw_sarp_subscription_start']['year'])) { $subscriptionStart = $options['aw_sarp_subscription_start']; $date = new Zend_Date(); $date->setMinute(0)->setHour(0)->setSecond(0)->setDay($subscriptionStart['day'])->setMonth($subscriptionStart['month'])->setYear($subscriptionStart['year']); $buyRequest->setAwSarpSubscriptionStart($date->toString(Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT))); } if (!is_null($buyRequest->getAwSarpSubscriptionStart()) && $Period->getId()) { $start = $buyRequest->getAwSarpSubscriptionStart(); if (!empty($start)) { $date = new Zend_Date($start, Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT)); } else { $date = $Period->getNearestAvailableDay(); } // Check date // Never check if start date //$performDateCompare = !!Mage::getSingleton('customer/session')->getCustomer()->getId(); $performDateCompare = !AW_Sarp_Model_Cron::$isCronSession; $today = new Zend_Date(); if (!$this->isVirtual($product)) { $today->addDayOfYear($Period->getPaymentOffset()); } if ($performDateCompare && ($date->compare($today, Zend_Date::DATE_SHORT) < 0 || !$Period->isAllowedDate($date, $product))) { $date = $Period->getNearestAvailableDay(); } } else { $date = Mage::app()->getLocale()->date(); } $product->addCustomOption('aw_sarp_subscription_start', $date->toString('Y-MM-dd')); if (!$old) { return parent::prepareForCart($buyRequest, $product); } }
/** * Initialize product(s) for add to cart process * * @param Varien_Object $buyRequest * @param Mage_Catalog_Model_Product $product * @return unknown */ public function prepareForCart(Varien_Object $buyRequest, $product = null) { $result = parent::prepareForCart($buyRequest, $product); if (is_string($result)) { return $result; } $product = $this->getProduct($product); $allowedAmounts = array(); foreach ($product->getGiftcardAmounts() as $value) { $allowedAmounts[] = Mage::app()->getStore()->roundPrice($value['website_value']); } $allowOpen = $product->getAllowOpenAmount(); $minAmount = $product->getOpenAmountMin(); $maxAmount = $product->getOpenAmountMax(); $selectedAmount = $buyRequest->getGiftcardAmount(); $customAmount = $buyRequest->getCustomGiftcardAmount(); $rate = Mage::app()->getStore()->getCurrentCurrencyRate(); if ($rate != 1) { if ($customAmount) { $customAmount = Mage::app()->getLocale()->getNumber($customAmount); if (is_numeric($customAmount) && $customAmount) { $customAmount = Mage::app()->getStore()->roundPrice($customAmount / $rate); } } } $emptyFields = 0; if (!$buyRequest->getGiftcardRecipientName()) { $emptyFields++; } if (!$buyRequest->getGiftcardSenderName()) { $emptyFields++; } if (!$this->isTypePhysical($product)) { if (!$buyRequest->getGiftcardRecipientEmail()) { $emptyFields++; } if (!$buyRequest->getGiftcardSenderEmail()) { $emptyFields++; } } if (($selectedAmount == 'custom' || !$selectedAmount) && $allowOpen && $customAmount <= 0) { $emptyFields++; } else { if (is_numeric($selectedAmount)) { if (!in_array($selectedAmount, $allowedAmounts)) { $emptyFields++; } } else { if (count($allowedAmounts) != 1) { $emptyFields++; } } } if ($emptyFields > 1) { return Mage::helper('enterprise_giftcard')->__('Please specify all the required information.'); } $amount = null; if (($selectedAmount == 'custom' || !$selectedAmount) && $allowOpen) { if ($customAmount <= 0) { return Mage::helper('enterprise_giftcard')->__('Please specify Gift Card amount.'); } if (!$minAmount || $minAmount && $customAmount >= $minAmount) { if (!$maxAmount || $maxAmount && $customAmount <= $maxAmount) { $amount = $customAmount; } else { if ($customAmount > $maxAmount) { $messageAmount = Mage::helper('core')->currency($maxAmount, true, false); return Mage::helper('enterprise_giftcard')->__('Gift Card max amount is %s', $messageAmount); } } } else { if ($customAmount < $minAmount) { $messageAmount = Mage::helper('core')->currency($minAmount, true, false); return Mage::helper('enterprise_giftcard')->__('Gift Card min amount is %s', $messageAmount); } } } else { if (is_numeric($selectedAmount)) { if (in_array($selectedAmount, $allowedAmounts)) { $amount = $selectedAmount; } } } if (is_null($amount)) { if (count($allowedAmounts) == 1) { $amount = array_shift($allowedAmounts); } } if (is_null($amount)) { return Mage::helper('enterprise_giftcard')->__('Please specify Gift Card amount.'); } $product->addCustomOption('giftcard_amount', $amount, $product); if (!$buyRequest->getGiftcardRecipientName()) { return Mage::helper('enterprise_giftcard')->__('Please specify recipient name.'); } if (!$buyRequest->getGiftcardSenderName()) { return Mage::helper('enterprise_giftcard')->__('Please specify sender name.'); } $product->addCustomOption('giftcard_sender_name', $buyRequest->getGiftcardSenderName(), $product); $product->addCustomOption('giftcard_recipient_name', $buyRequest->getGiftcardRecipientName(), $product); if (!$this->isTypePhysical($product)) { if (!$buyRequest->getGiftcardRecipientEmail()) { return Mage::helper('enterprise_giftcard')->__('Please specify recipient email.'); } if (!$buyRequest->getGiftcardSenderEmail()) { return Mage::helper('enterprise_giftcard')->__('Please specify sender email.'); } $product->addCustomOption('giftcard_sender_email', $buyRequest->getGiftcardSenderEmail(), $product); $product->addCustomOption('giftcard_recipient_email', $buyRequest->getGiftcardRecipientEmail(), $product); } $messageAllowed = false; if ($product->getUseConfigAllowMessage()) { $messageAllowed = Mage::getStoreConfigFlag(Enterprise_GiftCard_Model_Giftcard::XML_PATH_ALLOW_MESSAGE); } else { $messageAllowed = (int) $product->getAllowMessage(); } if ($messageAllowed) { $product->addCustomOption('giftcard_message', $buyRequest->getGiftcardMessage(), $product); } return $result; }
/** * Initialize product(s) for add to cart process * * @param Varien_Object $buyRequest * @param Mage_Catalog_Model_Product $product * @return unknown */ public function prepareForCart(Varien_Object $buyRequest, $product = null) { $result = parent::prepareForCart($buyRequest, $product); if (is_string($result)) { return $result; } $selections = array(); $product = $this->getProduct($product); $_appendAllSelections = false; if ($product->getSkipCheckRequiredOption()) { $_appendAllSelections = true; } if ($options = $buyRequest->getBundleOption()) { $qtys = $buyRequest->getBundleOptionQty(); foreach ($options as $_optionId => $_selections) { if (empty($_selections)) { unset($options[$_optionId]); } } $optionIds = array_keys($options); if (empty($optionIds)) { return Mage::helper('bundle')->__('Please select options for product.'); } //$optionsCollection = $this->getOptionsByIds($optionIds, $product); $product->getTypeInstance(true)->setStoreFilter($product->getStoreId(), $product); $optionsCollection = $this->getOptionsCollection($product); if (!$this->getProduct($product)->getSkipCheckRequiredOption()) { foreach ($optionsCollection->getItems() as $option) { if ($option->getRequired() && !isset($options[$option->getId()])) { return Mage::helper('bundle')->__('Required options not selected.'); } } } $selectionIds = array(); foreach ($options as $optionId => $selectionId) { if (!is_array($selectionId)) { if ($selectionId != '') { $selectionIds[] = $selectionId; } } else { foreach ($selectionId as $id) { if ($id != '') { $selectionIds[] = $id; } } } } $selections = $this->getSelectionsByIds($selectionIds, $product); /** * checking if selections that where added are still on sale */ foreach ($selections->getItems() as $key => $selection) { if (!$selection->isSalable()) { $_option = $optionsCollection->getItemById($selection->getOptionId()); if (is_array($options[$_option->getId()]) && count($options[$_option->getId()]) > 1) { $moreSelections = true; } else { $moreSelections = false; } if ($_option->getRequired() && (!$_option->isMultiSelection() || $_option->isMultiSelection() && !$moreSelections)) { return Mage::helper('bundle')->__('Selected required options not available.'); } } } $optionsCollection->appendSelections($selections, false, $_appendAllSelections); $selections = $selections->getItems(); } else { $product->getTypeInstance(true)->setStoreFilter($product->getStoreId(), $product); $optionCollection = $product->getTypeInstance(true)->getOptionsCollection($product); $optionIds = $product->getTypeInstance(true)->getOptionsIds($product); $selectionIds = array(); $selectionCollection = $product->getTypeInstance(true)->getSelectionsCollection($product->getTypeInstance(true)->getOptionsIds($product), $product); $options = $optionCollection->appendSelections($selectionCollection, false, $_appendAllSelections); foreach ($options as $option) { if ($option->getRequired() && count($option->getSelections()) == 1) { $selections = array_merge($selections, $option->getSelections()); } else { $selections = array(); break; } } } if (count($selections) > 0) { $uniqueKey = array($product->getId()); $selectionIds = array(); /* * shaking selection array :) by option position */ usort($selections, array($this, "shakeSelections")); foreach ($selections as $selection) { if ($selection->getSelectionCanChangeQty() && isset($qtys[$selection->getOptionId()])) { $qty = $qtys[$selection->getOptionId()] > 0 ? $qtys[$selection->getOptionId()] : 1; } else { $qty = $selection->getSelectionQty() ? $selection->getSelectionQty() : 1; } $product->addCustomOption('selection_qty_' . $selection->getSelectionId(), $qty, $selection); $selection->addCustomOption('selection_id', $selection->getSelectionId()); if ($customOption = $product->getCustomOption('product_qty_' . $selection->getId())) { $customOption->setValue($customOption->getValue() + $qty); } else { $product->addCustomOption('product_qty_' . $selection->getId(), $qty, $selection); } /* * creating extra attributes that will be converted * to product options in order item * for selection (not for all bundle) */ $price = $product->getPriceModel()->getSelectionPrice($product, $selection, $qty); $attributes = array('price' => Mage::app()->getStore()->convertPrice($price), 'qty' => $qty, 'option_label' => $selection->getOption()->getTitle(), 'option_id' => $selection->getOption()->getId()); //if (!$product->getPriceType()) { $_result = $selection->getTypeInstance(true)->prepareForCart($buyRequest, $selection); 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[] = $_result[0]->setParentProductId($product->getId())->addCustomOption('bundle_option_ids', serialize($optionIds))->addCustomOption('bundle_selection_attributes', serialize($attributes))->setCartQty($qty); //} $selectionIds[] = $_result[0]->getSelectionId(); $uniqueKey[] = $_result[0]->getSelectionId(); $uniqueKey[] = $qty; } /** * "unique" key for bundle selection and add it to selections and bundle for selections */ $uniqueKey = implode('_', $uniqueKey); foreach ($result as $item) { $item->addCustomOption('bundle_identity', $uniqueKey); } $product->addCustomOption('bundle_option_ids', serialize($optionIds)); $product->addCustomOption('bundle_selection_ids', serialize($selectionIds)); return $result; } return $this->getSpecifyOptionMessage(); }
/** * 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)'); }
/** * Prepares product for cart according to buyRequest. * * @param Varien_Object $buyRequest * @param object $product [optional] * @return */ public function prepareForCart(Varien_Object $buyRequest, $product = null) { Mage::getModel('sarp/product_type_default')->checkPeriod($product, $buyRequest); $Period = Mage::getModel('sarp/period'); /* We should add custom options that doesnt exist */ if ($buyRequest->getAwSarpSubscriptionType()) { if ($Period->load($buyRequest->getAwSarpSubscriptionType())->getId()) { $product->addCustomOption('aw_sarp_subscription_type', $Period->getId()); } } if ($buyRequest->getAwSarpSubscriptionStart() && $Period->getId()) { $date = new Zend_Date($buyRequest->getAwSarpSubscriptionStart(), Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT)); // Check date // Never check if start date //$performDateCompare = !!Mage::getSingleton('customer/session')->getCustomer()->getId(); $performDateCompare = !AW_Sarp_Model_Cron::$isCronSession; $today = new Zend_Date(); if (!$this->isVirtual($product)) { $today->addDayOfYear($Period->getPaymentOffset()); } if ($performDateCompare && ($date->compare($today, Zend_Date::DATE_SHORT) < 0 || !$Period->isAllowedDate($date, $product))) { throw new Mage_Core_Exception(Mage::helper('sarp')->__("Selected date is not valid for specified period")); } } else { $date = Mage::app()->getLocale()->date(); } $product->addCustomOption('aw_sarp_subscription_start', $date->toString('Y-MM-dd')); if ($attributes = $buyRequest->getSuperAttribute()) { $result = Mage_Catalog_Model_Product_Type_Abstract::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 Mage::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()); } } } if ($buyRequest->getAwSarpSubscriptionType()) { if ($Period->getId()) { $_result[0]->addCustomOption('aw_sarp_subscription_start', $date->toString('Y-MM-dd')); $_result[0]->addCustomOption('aw_sarp_subscription_type', $Period->getId()); } } $_result[0]->setParentProductId($product->getId())->addCustomOption('parent_product_id', $product->getId())->setCartQty(1); $result[] = $_result[0]; return $result; } } } return $this->getSpecifyOptionMessage(); }