Ejemplo n.º 1
0
 /**
  * @param Mage_Sales_Model_Order $order
  * @return array
  */
 protected function _getCouponsFormatted($order)
 {
     $result = array();
     if ($order->getCouponCode()) {
         if (Mage::helper("shopgate/config")->getIsMagentoVersionLower1410()) {
             $mageRule = Mage::getModel('salesrule/rule')->load($order->getCouponCode(), 'coupon_code');
             $mageCoupon = $mageRule;
         } else {
             $mageCoupon = Mage::getModel('salesrule/coupon')->load($order->getCouponCode(), 'code');
             $mageRule = Mage::getModel('salesrule/rule')->load($mageCoupon->getRuleId());
         }
         $externalCoupon = new ShopgateExternalCoupon();
         $couponInfo = array();
         $couponInfo["coupon_id"] = $mageCoupon->getId();
         $couponInfo["rule_id"] = $mageRule->getId();
         $externalCoupon->setCode($order->getCouponCode());
         $externalCoupon->setCurrency($order->getOrderCurrencyCode());
         $externalCoupon->setName($mageRule->getName());
         $externalCoupon->setDescription($mageRule->getDescription());
         $externalCoupon->setInternalInfo($this->_getConfig()->jsonEncode($couponInfo));
         $externalCoupon->setAmount($order->getDiscountAmount());
         array_push($result, $externalCoupon);
     }
     return $result;
 }
Ejemplo n.º 2
0
 /**
  * @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;
 }
Ejemplo n.º 3
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.º 4
0
 public function visitExternalCoupon(ShopgateExternalCoupon $c)
 {
     // get properties and iterate (no complex types in ShopgateExternalCoupon objects)
     $this->array = $this->iterateSimpleProperties($c->buildProperties());
 }
Ejemplo n.º 5
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;
 }