/**
  * @param OrderCore $order
  *
  * @return array
  * @throws PrestaShopDatabaseException
  */
 protected function _getCartRules($order)
 {
     $result = array();
     foreach ($order->getDiscounts() as $item) {
         if (array_key_exists('id_order_cart_rule', $item)) {
             /** @var OrderCartRuleCore $cartRuleItem */
             $cartRuleItem = new OrderCartRule($item['id_order_cart_rule']);
         } else {
             /** @var OrderDiscountCore $cartRuleItem */
             $cartRuleItem = new OrderDiscount($item['id_order_discount']);
         }
         $resultItem = new ShopgateExternalCoupon();
         $resultItem->setCode($cartRuleItem->name);
         $resultItem->setAmountNet($cartRuleItem->value_tax_excl);
         $resultItem->setAmountGross($cartRuleItem->value);
         $resultItem->setIsFreeShipping($cartRuleItem->free_shipping);
         $result[] = $resultItem;
     }
     return $result;
 }
Exemple #2
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;
 }