Ejemplo n.º 1
0
 /**
  * Check coupons for validation
  * Function will throw an ShopgateLibraryException if
  * * Count of coupons > 1
  * * Coupon cannot found
  * * Magento throws an exception
  *
  * @param              $mageCart
  * @param ShopgateCart $cart
  *
  * @return mixed|null|ShopgateExternalCoupon
  * @throws ShopgateLibraryException
  */
 public function checkCoupons($mageCart, ShopgateCart $cart)
 {
     /* @var $mageQuote Mage_Sales_Model_Quote */
     /* @var $mageCart Mage_Checkout_Model_Cart */
     /* @var $mageCoupon Mage_SalesRule_Model_Coupon */
     /* @var $mageRule Mage_SalesRule_Model_Rule */
     if (!$cart->getExternalCoupons()) {
         return null;
     }
     $externalCoupons = array();
     $mageQuote = $mageCart->getQuote();
     $validCouponsInCart = 0;
     foreach ($cart->getExternalCoupons() as $coupon) {
         /** @var ShopgateExternalCoupon $coupon */
         $externalCoupon = new ShopgateExternalCoupon();
         $externalCoupon->setIsValid(true);
         $externalCoupon->setCode($coupon->getCode());
         try {
             $mageQuote->setCouponCode($coupon->getCode());
             $mageQuote->setTotalsCollectedFlag(false)->collectTotals();
         } catch (Exception $e) {
             $externalCoupon->setIsValid(false);
             $externalCoupon->setNotValidMessage($e->getMessage());
         }
         if ($this->_getConfigHelper()->getIsMagentoVersionLower1410()) {
             $mageRule = Mage::getModel('salesrule/rule')->load($coupon->getCode(), 'coupon_code');
             $mageCoupon = $mageRule;
         } else {
             $mageCoupon = Mage::getModel('salesrule/coupon')->load($coupon->getCode(), 'code');
             $mageRule = Mage::getModel('salesrule/rule')->load($mageCoupon->getRuleId());
         }
         if ($mageRule->getId() && $mageQuote->getCouponCode()) {
             $couponInfo = array();
             $couponInfo["coupon_id"] = $mageCoupon->getId();
             $couponInfo["rule_id"] = $mageRule->getId();
             $amountCoupon = $mageQuote->getSubtotal() - $mageQuote->getSubtotalWithDiscount();
             $storeLabel = $mageRule->getStoreLabel(Mage::app()->getStore()->getId());
             $externalCoupon->setName($storeLabel ? $storeLabel : $mageRule->getName());
             $externalCoupon->setDescription($mageRule->getDescription());
             $externalCoupon->setIsFreeShipping((bool) $mageQuote->getShippingAddress()->getFreeShipping());
             $externalCoupon->setInternalInfo($this->jsonEncode($couponInfo));
             $externalCoupon->setAmountGross($amountCoupon);
             if (!$amountCoupon && !$externalCoupon->getIsFreeShipping()) {
                 $externalCoupon->setIsValid(false);
                 $externalCoupon->setNotValidMessage($this->_getHelper()->__('Coupon code "%s" is not valid.', Mage::helper('core')->htmlEscape($coupon->getCode())));
             }
             $externalCoupon->setTaxType('not_taxable');
         } else {
             $externalCoupon->setIsValid(false);
             $externalCoupon->setNotValidMessage($this->_getHelper()->__('Coupon code "%s" is not valid.', Mage::helper('core')->htmlEscape($coupon->getCode())));
         }
         if ($externalCoupon->getIsValid() && $validCouponsInCart >= 1) {
             $errorCode = ShopgateLibraryException::COUPON_TOO_MANY_COUPONS;
             $externalCoupon->setIsValid(false);
             $externalCoupon->setNotValidMessage(ShopgateLibraryException::getMessageFor($errorCode));
         }
         if ($externalCoupon->getIsValid()) {
             $validCouponsInCart++;
         }
         $externalCoupons[] = $externalCoupon;
     }
     return $externalCoupons;
 }
Ejemplo n.º 2
0
 /**
  * @param ShopgateCart $cart
  * @return array
  * @throws PrestaShopException
  */
 protected function _addCoupons(ShopgateCart $cart)
 {
     $results = array();
     $carrierId = null;
     if ($cart->getShippingInfos()) {
         $apiResponse = $cart->getShippingInfos()->getApiResponse();
         if (!empty($apiResponse)) {
             $apiResponse = unserialize($apiResponse);
             if (!empty($apiResponse['carrierId'])) {
                 $carrierId = $apiResponse['carrierId'];
             }
         }
     }
     $package = null;
     if (!empty($carrierId)) {
         $package = array('products' => null, 'id_carrier' => $carrierId);
     }
     foreach ($cart->getExternalCoupons() as $coupon) {
         $result = new ShopgateExternalCoupon();
         $result->setCode($coupon->getCode());
         $result->setCurrency($this->_getCurrency());
         $result->setIsValid(false);
         $result->setNotValidMessage(Tools::displayError('This voucher does not exists.'));
         if (version_compare(_PS_VERSION_, '1.5.0.0', '>=')) {
             /** @var CartRuleCore $cartRule */
             $cartRule = new CartRule(CartRule::getIdByCode($coupon->getCode()));
             if (Validate::isLoadedObject($cartRule)) {
                 $result->setName($cartRule->getFieldByLang('name'), $this->getPlugin()->getContext()->language->id);
                 $result->setDescription($cartRule->getFieldByLang('description', $this->getPlugin()->getContext()->language->id));
                 $result->setTaxType(Translate::getAdminTranslation('not_taxable'));
                 $result->setAmountGross($cartRule->getContextualValue(true, $this->getPlugin()->getContext(), null, $package));
                 /**
                  * validate coupon
                  */
                 if ($validateException = $cartRule->checkValidity($this->getPlugin()->getContext(), false, true)) {
                     $result->setIsValid(false);
                     $result->setNotValidMessage($validateException);
                 } else {
                     $result->setIsValid(true);
                     $result->setNotValidMessage(null);
                     $this->getPlugin()->getContext()->cart->addCartRule($cartRule->id);
                     $this->getPlugin()->getContext()->cart->save();
                 }
             }
         }
         $results[] = $result;
     }
     return $results;
 }