Beispiel #1
0
 /**
  * Validates missing Quote_Items which got removed because of insufficient qty available
  *
  * @param ShopgateCart           $cart
  * @param Mage_Sales_Model_Quote $quote
  *
  * @return array $result
  */
 public function fetchMissingQuoteItems($cart, $quote)
 {
     $result = array();
     foreach ($cart->getItems() as $_item) {
         $itemNumbers = explode("-", $_item->getItemNumber());
         $item = $quote->getItemsCollection()->getItemsByColumnValue('product_id', $itemNumbers[0]);
         if (empty($item) && !empty($itemNumbers[1])) {
             // grouped child
             $item = $quote->getItemsCollection()->getItemsByColumnValue('product_id', $itemNumbers[1]);
         }
         if (!count($item)) {
             $product = Mage::getModel('catalog/product')->setStoreId(Mage::helper('shopgate')->getConfig()->getStoreViewId())->load($_item->getItemNumber())->setShopgateItemNumber($_item->getItemNumber())->setShopgateOptions($_item->getOptions())->setShopgateInputs($_item->getInputs())->setShopgateAttributes($_item->getAttributes());
             $model = Mage::getModel('sales/quote_item');
             $model->setProduct($product);
             $result[] = $model;
         }
     }
     return $result;
 }
Beispiel #2
0
 /**
  * Represents the "check_stock" action.
  *
  * @throws ShopgateLibraryException
  * @see http://wiki.shopgate.com/Shopgate_Plugin_API_check_stock
  */
 protected function checkStock()
 {
     if (!isset($this->params['items'])) {
         throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_NO_ITEMS);
     }
     if (empty($this->response)) {
         $this->response = new ShopgatePluginApiResponseAppJson($this->trace_id);
     }
     $cart = new ShopgateCart();
     $cart->setItems($this->params['items']);
     $items = $this->plugin->checkStock($cart);
     $responseData = array();
     if (!is_array($items)) {
         throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_WRONG_RESPONSE_FORMAT, '$cartData Is type of : ' . is_object($items) ? get_class($items) : gettype($items));
     }
     $cartItems = array();
     if (!empty($items)) {
         foreach ($items as $cartItem) {
             /** @var ShopgateCartItem $cartItem */
             if (!is_object($cartItem) || !$cartItem instanceof ShopgateCartItem) {
                 throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_WRONG_RESPONSE_FORMAT, '$cartItem Is type of : ' . is_object($cartItem) ? get_class($cartItem) : gettype($cartItem));
             }
             $item = $cartItem->toArray();
             $notNeededArrayKeys = array('qty_buyable', 'unit_amount', 'unit_amount_with_tax');
             foreach ($notNeededArrayKeys as $key) {
                 if (array_key_exists($key, $item)) {
                     unset($item[$key]);
                 }
             }
             $cartItems[] = $item;
         }
     }
     $responseData["items"] = $cartItems;
     $this->responseData = $responseData;
 }
Beispiel #3
0
 public function visitCart(ShopgateCart $c)
 {
     // get properties
     $properties = $c->buildProperties();
     // iterate the simple variables and arrays with simple variables recursively
     $this->iterateSimpleProperties($properties);
     // visit delivery_address
     if (!empty($properties['delivery_address']) && $properties['delivery_address'] instanceof ShopgateAddress) {
         $properties['delivery_address']->accept($this);
         $properties['delivery_address'] = $this->array;
     }
     // visit invoice_address
     if (!empty($properties['invoice_address']) && $properties['invoice_address'] instanceof ShopgateAddress) {
         $properties['invoice_address']->accept($this);
         $properties['invoice_address'] = $this->array;
     }
     // visit shipping info
     if (!empty($properties['shipping_infos']) && $properties['shipping_infos'] instanceof ShopgateShippingInfo) {
         $properties['shipping_infos']->accept($this);
         $properties['shipping_infos'] = $this->array;
     }
     // iterate lists of referred objects
     $properties['external_coupons'] = $this->iterateObjectList($properties['external_coupons']);
     $properties['shopgate_coupons'] = $this->iterateObjectList($properties['shopgate_coupons']);
     $properties['items'] = $this->iterateObjectList($properties['items']);
     $this->array = $properties;
 }
Beispiel #4
0
 /**
  * Fetches any related customer_group to the given cart object
  *
  * @param ShopgateCart $cart
  * @param int          $websiteId
  *
  * @return array
  */
 protected function _getCustomerGroups(ShopgateCart $cart, $websiteId)
 {
     /** @var Mage_Customer_Model_Customer $customer */
     $customer = Mage::getModel('customer/customer');
     $externalCustomerId = $cart->getExternalCustomerId();
     if ($externalCustomerId) {
         $customer->load($externalCustomerId);
     } else {
         if ($cart->getDeliveryAddress() && $cart->getDeliveryAddress()->getMail()) {
             $customer->setWebsiteId($websiteId)->loadByEmail($cart->getDeliveryAddress()->getMail());
         } else {
             if ($cart->getInvoiceAddress() && $cart->getInvoiceAddress()->getMail()) {
                 $customer->setWebsiteId($websiteId)->loadByEmail($cart->getInvoiceAddress()->getMail());
             }
         }
     }
     if (!$externalCustomerId && $customer->getId()) {
         $cart->setExternalCustomerId($customer->getId());
     }
     return Mage::helper('shopgate/customer')->getShopgateCustomerGroups($customer);
 }
Beispiel #5
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;
 }
 /**
  * create dummy customer
  *
  * @param ShopgateCart $cart
  */
 protected function _createCustomer(ShopgateCart $cart)
 {
     /**
      * prepare customer group
      */
     if ($cart->getExternalCustomerId()) {
         /**
          * load exist customer
          */
         $this->getPlugin()->getContext()->customer = new Customer($cart->getExternalCustomerId());
         if (!Validate::isLoadedObject($this->getPlugin()->getContext()->customer)) {
             $this->_addException(ShopgateLibraryException::UNKNOWN_ERROR_CODE, sprintf('Customer with id #%s not found', $cart->getExternalCustomerId()));
         }
     } else {
         /**
          * create dummy customer
          */
         $customerGroup = $this->_getCustomerGroups($cart);
         $this->getPlugin()->getContext()->customer = new Customer();
         $this->getPlugin()->getContext()->customer->lastname = self::DEFAULT_CUSTOMER_LAST_NAME;
         $this->getPlugin()->getContext()->customer->firstname = self::DEFAULT_CUSTOMER_FIRST_NAME;
         $this->getPlugin()->getContext()->customer->email = self::DEFAULT_CUSTOMER_EMAIL;
         $this->getPlugin()->getContext()->customer->passwd = self::DEFAULT_CUSTOMER_PASSWD;
         $this->getPlugin()->getContext()->customer->id_default_group = current($customerGroup->getCustomerGroups())->getId();
         $this->getPlugin()->getContext()->customer->add();
         $this->_isDummyCustomer = true;
     }
     /**
      * add customer to cart
      */
     $this->getPlugin()->getContext()->cart->id_customer = $this->getPlugin()->getContext()->customer->id;
     /**
      * add carrier id
      */
     $shippingModel = new ShopgateShipping($this->getModule());
     $tmpOrder = new ShopgateOrder();
     $tmpOrder->setShippingType($cart->getShippingType() ? $cart->getShippingType() : ShopgateShipping::DEFAULT_PLUGIN_API_KEY);
     $tmpOrder->setShippingGroup($cart->getShippingGroup());
     $tmpOrder->setShippingInfos($cart->getShippingInfos());
     /** @var CarrierCore $carrierItem */
     $carrierItem = new Carrier($shippingModel->getCarrierIdByApiOrder($tmpOrder));
     if (!Validate::isLoadedObject($carrierItem)) {
         $this->_addException(ShopgateLibraryException::UNKNOWN_ERROR_CODE, sprintf('Invalid carrier ID #%s', $shippingModel->getCarrierIdByApiOrder($tmpOrder)));
     }
     $this->getPlugin()->getContext()->cart->id_carrier = $carrierItem->id;
     $this->getPlugin()->getContext()->cart->save();
 }
Beispiel #7
0
 /**
  * add customer to cart e.g to validate customer related price rules
  *
  * @param ShopgateCart $cart
  */
 public function addCustomerToCart(&$cart)
 {
     if ($cart->getMail()) {
         /** @var Mage_Customer_Model_Customer $magentoCustomer */
         $magentoCustomer = Mage::getModel("customer/customer");
         $magentoCustomer->setWebsiteId(Mage::app()->getWebsite()->getid());
         $magentoCustomer->loadByEmail($cart->getMail());
         if ($magentoCustomer->getId()) {
             $cart->setExternalCustomerId($magentoCustomer->getId());
         }
     }
 }