/** * Setup product for quote item * * @param Mage_Catalog_Model_Product $product * @return Mage_Sales_Model_Quote_Item */ public function setProduct($product) { if ($this->getQuote()) { $product->setStoreId($this->getQuote()->getStoreId()); $product->setCustomerGroupId($this->getQuote()->getCustomerGroupId()); } $this->setData('product', $product)->setProductId($product->getId())->setProductType($product->getTypeId())->setWeight($this->getProduct()->getWeight())->setTaxClassId($product->getTaxClassId())->setBaseCost($product->getCost())->setIsRecurring($product->getIsRecurring())->setIsQtyDecimal(false); if (!Vikont_ARIOEM_Helper_Cart::isOemProduct($product) || !$this->hasData('sku')) { $this->setSku($this->getProduct()->getSku())->setName($product->getName()); } Mage::dispatchEvent('sales_quote_item_set_product', array('product' => $product, 'quote_item' => $this)); return $this; }
public function addAction() { $coreSession = Mage::getSingleton('core/session'); $data = $this->getRequest()->getPost(); //vd($data); $oemPageURL = $data['pageURL']; $oemBrands = Mage::getModel('arioem/source_oembrand'); $oemHelper = Mage::helper('arioem/OEM'); $commonHelper = Mage::helper('arioem'); $gainPercent = $commonHelper->getCustomerCostPercent(); $response = new Varien_Object(); $itemsAddedTotal = 0; $skusAdded = array(); $items = array(); foreach ($data['parts'] as $part) { $price = (double) trim(str_replace(',', '', $part['price']), ' $'); if ($gainPercent) { $cost = (double) $oemHelper->getOEMCost($part['brand'], $part['sku']); if ($cost) { $price = $cost * (100 + $gainPercent) / 100; } else { $coreSession->addWarning($this->__('No cost to apply custom price for Part #%s %s has been found, retail price used instead. Please confirm this with our customer support.', $part['sku'], $part['name'])); Vikont_ARIOEM_Model_Log::logError(sprintf('No cost for OEM product found: Brand=%s PartNumber=%s Name=%s Price=%s Qty=%d IP=%s CustomerId=%d URL=%s. Retail price used instead', $part['brand'], $part['sku'], $part['name'], $part['price'], $part['qty'], $_SERVER['REMOTE_ADDR'], Mage::getSingleton('customer/session')->getCustomerId(), $oemPageURL)); // continue; } } $items[] = array('brand' => $part['brand'], 'sku' => $part['sku'], 'name' => $part['name'], 'qty' => $part['qty'], 'price' => $price); } $checkoutSession = Mage::getSingleton('checkout/session'); $cart = Mage::getSingleton('checkout/cart'); $cart->getQuote()->setIsSuperMode(true); // TODO try { $productId = Vikont_ARIOEM_Helper_Cart::getOemProductId(); if (!$productId) { Mage::throwException($this->__('Cannot find OEM parent product')); } $customOptionId = Vikont_ARIOEM_Helper_Db::getTableValue('catalog/product_option', 'option_id', 'product_id=' . $productId); foreach ($items as $itemData) { $product = Mage::getModel('catalog/product')->load($productId); if (!$product->getId()) { Mage::log('ERROR: ARIOEM: No dummy OEM product found'); Mage::throwException($this->__('Cannot add product(s) to Cart')); break; } $addToCartParams = array('product' => $productId, 'options' => array($customOptionId => $itemData['sku']), 'qty' => $itemData['qty']); $itemCountBefore = count($cart->getItems()); $cart->addProduct($product, $addToCartParams); // $brandName = $oemBrands->getOptionText($itemData['brand']); $brandName = Vikont_ARIOEM_Model_Oem_Part::getBrandNameByShortname($itemData['brand']); if (!count($cart->getItems())) { $cart->save(); } // if the item was really added, but not just increased the qty of its copy that's been added before if ($itemCountBefore < count($cart->getItems())) { $cart->getItems()->getLastItem()->setSku($itemData['sku'])->setName($this->__('OEM %s | %s', $brandName, $itemData['name']))->setOriginalCustomPrice($itemData['price'])->setRedirectUrl($oemPageURL)->save(); } $itemsAddedTotal += $itemData['qty']; $skusAdded[] = $itemData['sku']; } $cart->getQuote()->setTotalsCollectedFlag(false); $cart->save(); $checkoutSession->setCartWasUpdated(true); Mage::dispatchEvent('checkout_cart_add_product_complete', array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())); if (!$cart->getQuote()->getHasError()) { $response->setError(0); $response->setProductsAdded($skusAdded); $response->setQty((string) (int) $cart->getSummaryQty()); $message = count($items) == 1 ? $this->__('Item %s was added to your shopping cart', $items[0]['name']) : $this->__('%d items were added to your shopping cart', $itemsAddedTotal); $response->setMessage($message); // $cartTopBlock = $this->getLayout()->createBlock('checkout/cart_sidebar'); // $topCartHtml = $cartTopBlock // ? $cartTopBlock // ->setTemplate('checkout/cart/cartheader.phtml') // ->setIsAjax(true) // ->toHtml() // : ''; // $response->setCartTop($topCartHtml); } else { $response->setError(1); $response->setMessage($this->__('Some error has occurred while adding to Cart')); } } catch (Exception $e) { Mage::logException($e); $response->setError(1); $response->setMessage($this->__('Cannot add the item to shopping cart, reason: %s', $e->getMessage())); } $this->getResponse()->setBody($response->toJson()); }
public function addToCart($data) { // $oemPageURL = $data['pageURL']; $brandCode = $data['brand']; $vehicleName = @$data['vehicle']; $yearName = @$data['year']; $modelName = @$data['model']; $assemblyName = @$data['assembly']; $response = new Varien_Object(); $oemBrands = Mage::getModel('arioem/source_oembrand'); if ('SLN' == $brandCode) { $brandCode = 'POL'; } $brandName = $oemBrands->getOptionText($brandCode); if (!$brandName) { $brandName = Vikont_ARIOEM_Model_Oem_Part::getBrandNameByShortname($brandCode); } $oemHelper = Mage::helper('arioem/OEM'); $commonHelper = Mage::helper('arioem'); $gainPercent = $commonHelper->getCustomerCostPercent(); $itemsAddedTotal = 0; $skusAdded = array(); $items = array(); $warnings = array(); foreach ($data['parts'] as $part) { $partData = $oemHelper->getOEMData($brandCode, $part['sku']); if ($partData) { if ($gainPercent) { $price = $partData['cost'] * (100 + $gainPercent) / 100; } else { $origPrice = floatval($partData['price']); $price = $origPrice ? $origPrice : ($price = (double) trim(str_replace(',', '', $part['price']), ' $')); } } else { $warnings[] = $this->__('No record for %s, part #%s was found in DB. Please contact our support.', $part['name'], $part['sku']); Vikont_ARIOEM_Model_Log::logError(sprintf('No record for OEM product found: Brand=%s PartNumber=%s Name=%s Price=%s Qty=%d IP=%s CustomerId=%d URL=%s', $brandCode, $part['sku'], $part['name'], $part['price'], $part['qty'], $_SERVER['REMOTE_ADDR'], Mage::getSingleton('customer/session')->getCustomerId(), $oemPageURL)); continue; } $qty = (int) $part['qty'] ? (int) $part['qty'] : 1; $items[] = array('sku' => $part['sku'], 'name' => $partData['part_name'], 'qty' => $qty, 'price' => $price, 'pageURL' => @$part['pageURL']); } $checkoutSession = Mage::getSingleton('checkout/session'); $cart = Mage::getSingleton('checkout/cart'); $cart->getQuote()->setIsSuperMode(true); // TODO $productId = Vikont_ARIOEM_Helper_Cart::getOemProductId(); if (!$productId) { Mage::throwException($this->__('Cannot find OEM parent product')); } $brandOptionId = Mage::getStoreConfig('arioem/add_to_cart/dummy_product_brand_option_id'); $yearOptionId = Mage::getStoreConfig('arioem/add_to_cart/dummy_product_year_option_id'); $modelOptionId = Mage::getStoreConfig('arioem/add_to_cart/dummy_product_model_option_id'); $partNumberOptionId = Mage::getStoreConfig('arioem/add_to_cart/dummy_product_partNo_option_id'); $productNameTemplate = Mage::getStoreConfig('arioem/add_to_cart/dummy_product_name_template'); foreach ($items as $itemData) { $product = Mage::getModel('catalog/product')->load($productId); if (!$product->getId()) { Mage::log('ERROR: ARIOEM: No dummy OEM product found'); Mage::throwException($this->__('Cannot add product(s) to Cart')); break; } $addToCartOptions = array($brandOptionId => $brandName); if ($yearName) { $addToCartOptions[$yearOptionId] = $yearName; } if ($modelName) { $addToCartOptions[$modelOptionId] = $modelName; } $addToCartOptions[$partNumberOptionId] = $itemData['sku']; $addToCartParams = array('product' => $productId, 'options' => $addToCartOptions, 'qty' => $itemData['qty']); $itemCountBefore = count($cart->getItems()); $cart->addProduct($product, $addToCartParams); if (!count($cart->getItems())) { $cart->save(); } $productName = str_replace('%BRAND%', $brandName, $productNameTemplate); $productName = str_replace('%VEHICLE%', $vehicleName, $productName); $productName = str_replace('%YEAR%', $yearName, $productName); $productName = str_replace('%MODEL%', $modelName, $productName); $productName = str_replace('%ASSEMBLY%', $assemblyName, $productName); $productName = str_replace('%PART_NUMBER%', $itemData['sku'], $productName); $productName = str_replace('%PART_NAME%', $itemData['name'], $productName); // if the item was really added, but not just increased the qty of its copy that's been added before if ($itemCountBefore < count($cart->getItems())) { $cart->getItems()->getLastItem()->setSku($itemData['sku'])->setName($productName)->setOriginalCustomPrice($itemData['price'])->setRedirectUrl($itemData['pageURL'])->save(); } $itemsAddedTotal += $itemData['qty']; $skusAdded[] = $itemData['sku']; } $cart->getQuote()->setTotalsCollectedFlag(false); $cart->save(); $checkoutSession->setCartWasUpdated(true); Mage::dispatchEvent('checkout_cart_add_product_complete', array('product' => $product, 'request' => Mage::app()->getRequest(), 'response' => Mage::app()->getResponse())); if (count($warnings)) { $response->setWarnings($warnings); } $response->setCartSummaryQty((int) $cart->getSummaryQty())->setItemsAddedTotal($itemsAddedTotal)->setSkusAdded($skusAdded)->setError($cart->getQuote()->getHasError()); return $response; }