Пример #1
0
 public function addItem(\Model_Cart $cart, \Model_Product $product, array $data)
 {
     $event_params = array_merge($data, array('cart_id' => $cart->id, 'product_id' => $product->id));
     $this->di['events_manager']->fire(array('event' => 'onBeforeProductAddedToCart', 'params' => $event_params));
     $productService = $product->getService();
     if ($this->isRecurrentPricing($product)) {
         $required = array('period' => 'Period parameter not passed');
         $this->di['validator']->checkRequiredParamsForArray($required, $data);
         if (!$this->isPeriodEnabledForProduct($product, $data['period'])) {
             throw new \Box_Exception('Selected billing period is not valid');
         }
     }
     $qty = $this->di['array_get']($data, 'quantity', 1);
     // check stock
     if (!$this->isStockAvailable($product, $qty)) {
         throw new \Box_Exception("I'm afraid we are out of stock.");
     }
     $addons = $this->di['array_get']($data, 'addons', array());
     unset($data['id']);
     unset($data['addons']);
     $list = array();
     $list[] = array('product' => $product, 'config' => $data);
     //check for required domain product
     if (method_exists($productService, 'getDomainProductFromConfig')) {
         $dc = $productService->getDomainProductFromConfig($product, $data);
         if (isset($dc['config']) && $dc['product'] && $dc['product'] instanceof \Model_Product) {
             $list[] = $dc;
         }
     }
     $productService = $this->di['mod_service']('Product');
     foreach ($addons as $id => $ac) {
         if (isset($ac['selected']) && (bool) $ac['selected']) {
             $addon = $productService->getAddonById($id);
             if ($addon instanceof \Model_Product) {
                 if ($this->isRecurrentPricing($addon)) {
                     $required = array('period' => 'Addon period parameter not passed');
                     $this->di['validator']->checkRequiredParamsForArray($required, $ac);
                     if (!$this->isPeriodEnabledForProduct($addon, $ac['period'])) {
                         throw new \Box_Exception('Selected billing period is not valid for addon');
                     }
                 }
                 $ac['parent_id'] = $product->id;
                 $list[] = array('product' => $addon, 'config' => $ac);
             } else {
                 error_log('Addon not found by id ' . $id);
             }
         }
     }
     foreach ($list as $c) {
         $productFromList = $c['product'];
         $productFromListConfig = $c['config'];
         $productServiceFromList = $productFromList->getService();
         //@deprecated logic
         if (method_exists($productServiceFromList, 'prependOrderConfig')) {
             $productFromListConfig = $productServiceFromList->prependOrderConfig($productFromList, $productFromListConfig);
         }
         if (method_exists($productServiceFromList, 'attachOrderConfig')) {
             $model = $this->di['db']->load('Product', $productFromList->id);
             $productFromListConfig = $productServiceFromList->attachOrderConfig($model, $productFromListConfig);
         }
         if (method_exists($productServiceFromList, 'validateOrderData')) {
             $productServiceFromList->validateOrderData($productFromListConfig);
         }
         if (method_exists($productServiceFromList, 'validateCustomForm')) {
             $productServiceFromList->validateCustomForm($productFromListConfig, $this->di['db']->toArray($productFromList));
         }
         $this->addProduct($cart, $productFromList, $productFromListConfig);
     }
     $this->di['logger']->info('Added "%s" to shopping cart', $product->title);
     $this->di['events_manager']->fire(array('event' => 'onAfterProductAddedToCart', 'params' => $event_params));
     return true;
 }