예제 #1
0
 /**
  * This function is used to get all the quantites for a product.
  * So if the product is a bundle it will return an array with the associated products ids and their quantities in the product
  * For simple types it will just return the product id and qty 1 and for configurable will return the if of the selected configurable.
  * A configurable product has attributes
  * A bundle product is composed from bundle_options and quantities for every option.
  * A bundle product has an extra bundle_option_qty1 for qtys which are set as default and can't be changed by the user.
  *
  * @param      $product
  * @param      $qty
  * @param null $attributes
  * @param null $bundleOption
  * @param null $bundleOptionQty1
  * @param null $bundleOptionQty
  * @param bool $multiplyQty
  *
  * @return array
  */
 public static function getQuantityArrayForProduct($product, $qty, $attributes = null, $bundleOption = null, $bundleOptionQty1 = null, $bundleOptionQty = null, $multiplyQty = false)
 {
     $qtyArr = array();
     if ($qty == '' || $qty == 0) {
         $qty = 1;
     }
     if (is_object($product)) {
         $productObj = $product;
         $productId = $product->getId();
     } else {
         $productId = $product;
     }
     $typeId = ITwebexperts_Payperrentals_Helper_Data::getAttributeCodeForId($productId, 'type_id');
     if ($typeId == ITwebexperts_Payperrentals_Helper_Data::PRODUCT_TYPE_CONFIGURABLE) {
         if (!isset($productObj)) {
             $productObj = ITwebexperts_Payperrentals_Helper_Data::initProduct($productId);
         }
         $childProduct = Mage::getModel('catalog/product_type_configurable')->getProductByAttributes($attributes, $productObj);
         if (is_object($childProduct) && $childProduct->getTypeId() != 'simple') {
             $qtyArr[$childProduct->getId()] = $qty;
         } else {
             $qtyArr[$productId] = $qty;
         }
     } elseif ($typeId == ITwebexperts_Payperrentals_Helper_Data::PRODUCT_TYPE_GROUPED) {
         $assocArr = ITwebexperts_Payperrentals_Helper_Data::getAssociatedProductIds($productId);
         foreach ($assocArr as $iAssoc) {
             $iTypeId = ITwebexperts_Payperrentals_Helper_Data::getAttributeCodeForId($iAssoc, 'type_id');
             if ($iTypeId == ITwebexperts_Payperrentals_Helper_Data::PRODUCT_TYPE) {
                 $qtyArr[$iAssoc] = $qty;
                 break;
                 //we only allow grouped products with 1 reservation type product
             }
         }
     } elseif ($typeId != ITwebexperts_Payperrentals_Helper_Data::PRODUCT_TYPE_BUNDLE) {
         $qtyArr[$productId] = $qty;
     } elseif (!is_null($bundleOption)) {
         $selectionIds = $bundleOption;
         list($source['bundle_option_qty1'], $source['bundle_option_qty']) = array($bundleOptionQty1, $bundleOptionQty);
         ITwebexperts_Payperrentals_Helper_Data::createQuantities($source, $qty, $multiplyQty);
         if (!isset($productObj)) {
             $productObj = ITwebexperts_Payperrentals_Helper_Data::initProduct($productId);
         }
         $selections = $productObj->getTypeInstance(true)->getSelectionsByIds($selectionIds, $productObj);
         foreach ($selections->getItems() as $selection) {
             $bProductId = $selection->getProductId();
             $bTypeId = ITwebexperts_Payperrentals_Helper_Data::getAttributeCodeForId($bProductId, 'type_id');
             //if ($bTypeId == ITwebexperts_Payperrentals_Helper_Data::PRODUCT_TYPE) {
             $qty = ITwebexperts_Payperrentals_Helper_Data::getQuantityForSelectionProduct($selection, $qty);
             if (!isset($qtyArr[$bProductId])) {
                 $qtyArr[$bProductId] = $qty;
             } else {
                 $qtyArr[$bProductId] += $qty;
             }
             //}
         }
     }
     return $qtyArr;
 }
예제 #2
0
 /**
  * When product is added to shopping cart add an entry to reservationquotes with reservation data
  * @param Varien_Event_Observer $event
  *
  * @return $this
  */
 public function updateCartReservation(Varien_Event_Observer $event)
 {
     /** @var $quoteItem Mage_Sales_Model_Quote_Item */
     $quoteItem = $event->getItem();
     /** @var $product Mage_Catalog_Model_Product */
     $product = $quoteItem->getProduct();
     $source = unserialize($product->getCustomOption('info_buyRequest')->getValue());
     if ($this->excludedFromUpdate($quoteItem, $source)) {
         return $this;
     }
     list($startDateArr, $endDateArr) = ITwebexperts_Payperrentals_Helper_Data::getStartEndDates($source);
     foreach ($startDateArr as $count => $startDate) {
         $endDate = $endDateArr[$count];
         if ($quoteItem->getProductType() != ITwebexperts_Payperrentals_Helper_Data::PRODUCT_TYPE_BUNDLE) {
             $qty = $this->getSelectedQtyByTheCustomerForTheProduct($product, $quoteItem);
             $product = $this->getProduct($product, $source);
             if ($quoteItem->getProductType() == ITwebexperts_Payperrentals_Helper_Data::PRODUCT_TYPE) {
                 Mage::getResourceModel('payperrentals/reservationquotes')->deleteByQuoteItemAndDates($quoteItem, $startDate, $endDate);
                 $this->saveSimpleReservationProduct($quoteItem, $product, $source, $qty, $startDate, $endDate);
             }
             $this->saveConfigurableReservationProduct($quoteItem, $product, $source, $qty, $startDate, $endDate);
         } else {
             $qty = $this->getSelectedQtyByTheCustomerForTheProduct($product, $quoteItem);
             ITwebexperts_Payperrentals_Helper_Data::createQuantities($source, $qty, true);
             $selectionIds = $source['bundle_option'];
             $selections = $product->getTypeInstance(true)->getSelectionsByIds($selectionIds, $product);
             Mage::getResourceModel('payperrentals/reservationquotes')->deleteByQuoteItemAndDates($quoteItem, $startDate, $endDate);
             foreach ($selections->getItems() as $selection) {
                 if ($selection->getTypeId() == ITwebexperts_Payperrentals_Helper_Data::PRODUCT_TYPE) {
                     $qty = ITwebexperts_Payperrentals_Helper_Data::getQuantityForSelectionProduct($selection, $qty);
                     $this->saveSimpleReservationProduct($quoteItem, $selection, $source, $qty, $startDate, $endDate);
                 }
             }
         }
     }
     return $this;
 }