Esempio n. 1
0
    /**
     * Handle adding article
     *
     * @return void
     */
    public function handleAddArticle()
    {
        $hookObjectsArr = array();
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['commerce/pi2/class.tx_commerce_pi2.php']['artAddUid'])) {
            GeneralUtility::deprecationLog('
				hook
				$GLOBALS[\'TYPO3_CONF_VARS\'][\'EXTCONF\'][\'commerce/pi2/class.tx_commerce_pi2.php\'][\'artAddUid\']
				is deprecated since commerce 1.0.0, it will be removed in commerce 1.4.0, please use instead
				$GLOBALS[\'TYPO3_CONF_VARS\'][\'EXTCONF\'][\'commerce/Classes/Controller/BasketController.php\'][\'addArticleUid\']
			');
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['commerce/pi2/class.tx_commerce_pi2.php']['artAddUid'] as $classRef) {
                $hookObjectsArr[] =& GeneralUtility::getUserObj($classRef);
            }
        }
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['commerce/Classes/Controller/BasketController.php']['addArticleUid'])) {
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['commerce/Classes/Controller/BasketController.php']['addArticleUid'] as $classRef) {
                $hookObjectsArr[] =& GeneralUtility::getUserObj($classRef);
            }
        }
        // Hook to process basket before adding an article to basket
        foreach ($hookObjectsArr as $hookObj) {
            if (method_exists($hookObj, 'preartAddUid')) {
                $hookObj->preartAddUid($this->basket, $this);
            }
        }
        if (isset($this->piVars['artAddUid']) && is_array($this->piVars['artAddUid'])) {
            foreach ($this->piVars['artAddUid'] as $articleUid => $articleAddValues) {
                $articleUid = (int) $articleUid;
                /**
                 * Basket item
                 *
                 * @var Tx_Commerce_Domain_Model_BasketItem $basketItem
                 */
                $basketItem = $this->basket->getBasketItem($articleUid);
                // Safe old quantity for price limit
                if ($basketItem) {
                    $oldCountValue = $basketItem->getQuantity();
                } else {
                    $oldCountValue = 0;
                }
                if (!isset($articleAddValues['count']) || $articleAddValues['count'] < 0) {
                    $articleAddValues['count'] = 1;
                }
                if ((int) $articleAddValues['count'] === 0) {
                    if ($this->basket->getQuantity($articleUid) > 0) {
                        $this->basket->deleteArticle($articleUid);
                    }
                    foreach ($hookObjectsArr as $hookObj) {
                        if (method_exists($hookObj, 'postDeleteArtUidSingle')) {
                            $hookObj->postDeleteArtUidSingle($articleUid, $articleAddValues, $oldCountValue, $this->basket, $this);
                        }
                    }
                } else {
                    /**
                     * Article
                     *
                     * @var $articleObj Tx_Commerce_Domain_Model_Article
                     */
                    $articleObj = GeneralUtility::makeInstance('Tx_Commerce_Domain_Model_Article', $articleUid);
                    $articleObj->loadData('basket');
                    $productObj = $articleObj->getParentProduct();
                    $productObj->loadData('basket');
                    foreach ($hookObjectsArr as $hookObj) {
                        if (method_exists($hookObj, 'preartAddUidSingle')) {
                            $hookObj->preartAddUidSingle($articleUid, $articleAddValues, $productObj, $articleObj, $this->basket, $this);
                        }
                    }
                    if ($articleObj->isAccessible() && $productObj->isAccessible()) {
                        // Only if product and article are accessible
                        if ($this->conf['checkStock'] == 1) {
                            // Instance to calculate shipping costs
                            if ($articleObj->hasStock($articleAddValues['count'])) {
                                if ((int) $articleAddValues['price_id'] > 0) {
                                    $this->basket->addArticle($articleUid, $articleAddValues['count'], $articleAddValues['price_id']);
                                } else {
                                    $this->basket->addArticle($articleUid, $articleAddValues['count']);
                                }
                            } else {
                                $this->noStock = $this->pi_getLL('noStock');
                            }
                        } else {
                            // Add article by default
                            if ((int) $articleAddValues['price_id'] > 0) {
                                $this->basket->addArticle($articleUid, $articleAddValues['count'], $articleAddValues['price_id']);
                            } else {
                                $this->basket->addArticle($articleUid, $articleAddValues['count']);
                            }
                        }
                    }
                    foreach ($hookObjectsArr as $hookObj) {
                        if (method_exists($hookObj, 'postartAddUidSingle')) {
                            $hookObj->postartAddUidSingle($articleUid, $articleAddValues, $productObj, $articleObj, $this->basket, $this);
                        }
                    }
                    // Check for basket price limit
                    if ((int) $this->conf['priceLimitForBasket'] > 0 && $this->basket->getSumGross() > (int) $this->conf['priceLimitForBasket']) {
                        $this->basket->addArticle($articleUid, $oldCountValue);
                        $this->setPriceLimitForBasket(1);
                    }
                }
            }
            foreach ($hookObjectsArr as $hookObj) {
                if (method_exists($hookObj, 'postartAddUid')) {
                    $hookObj->postartAddUid($this->basket, $this);
                }
            }
        }
    }