/** * Checks if the the basket weight is ok for the service provider. * * @param MShop_Order_Item_Base_Interface $basket Basket object * * @return boolean True if payment provider can be used, false if not */ public function isAvailable(MShop_Order_Item_Base_Interface $basket) { $context = $this->_getContext(); $basketWeight = 0; $basketItems = $basket->getProducts(); foreach ($basketItems as $basketItem) { $prodId = $basketItem->getProductId(); if (!isset($prodMap[$prodId])) { // basket can contain a product several times in different basket items $prodMap[$prodId] = 0.0; } $prodMap[$prodId] += $basketItem->getQuantity(); } $propertyManager = MShop_Factory::createManager($context, 'product/property'); $search = $propertyManager->createSearch(true); $expr = array($search->compare('==', 'product.property.productid', array_keys($prodMap)), $search->compare('==', 'product.property.type.code', 'package-weight'), $search->getConditions()); $search->setConditions($search->combine('&&', $expr)); $search->setSlice(0, 0x7fffffff); // if more than 100 products are in the basket foreach ($propertyManager->searchItems($search) as $property) { $basketWeight += (double) $property->getValue() * $prodMap[$property->getParentId()]; } if ($this->_checkWeightScale($basketWeight) === false) { return false; } return $this->_getProvider()->isAvailable($basket); }
/** * Checks for requirements. * * @param MShop_Order_Item_Base_Interface $base Basic order of the customer * @return boolean True if the requirements are met, false if not */ public function isAvailable(MShop_Order_Item_Base_Interface $base) { if (($prodcode = $this->_getConfigValue('required.productcode')) !== null) { foreach ($base->getProducts() as $product) { if ($product->getProductCode() == $prodcode) { return parent::isAvailable($base); } } return false; } return true; }
/** * Checks for the product limits when the configuration contains limits per currency. * * @param MShop_Order_Item_Base_Interface $order Basket object * @param MShop_Order_Item_Base_Product_Interface $value Order product item * @throws MShop_Plugin_Provider_Exception If one limit is exceeded */ protected function _checkWithCurrency(MShop_Order_Item_Base_Interface $order, MShop_Order_Item_Base_Product_Interface $value) { $config = $this->_getItem()->getConfig(); $currencyId = $value->getPrice()->getCurrencyId(); if (isset($config['single-value-max'][$currencyId]) && $value->getPrice()->getValue() * $value->getQuantity() > (double) $config['single-value-max'][$currencyId]) { $msg = sprintf('The maximum product value is %1$s', $config['single-value-max'][$currencyId]); throw new MShop_Plugin_Provider_Exception($msg); } if (isset($config['total-value-max'][$currencyId])) { $price = clone $value->getPrice(); $price->setValue($price->getValue() * $value->getQuantity()); foreach ($order->getProducts() as $product) { $price->addItem($product->getPrice(), $product->getQuantity()); } if ((double) $price->getValue() > (double) $config['total-value-max'][$currencyId]) { $msg = sprintf('The maximum value of all products is %1$s', $config['total-value-max'][$currencyId]); throw new MShop_Plugin_Provider_Exception($msg); } } }
/** * Saves the ordered products to the storage. * * @param MShop_Order_Item_Base_Interface $basket Basket containing ordered products or bundles */ protected function _storeProducts(MShop_Order_Item_Base_Interface $basket) { $position = 1; $manager = $this->getSubManager('product'); $attrManager = $manager->getSubManager('attribute'); foreach ($basket->getProducts() as $item) { $baseId = $basket->getId(); $item->setBaseId($baseId); if (($pos = $item->getPosition()) === null) { $item->setPosition($position++); } else { $position = ++$pos; } $manager->saveItem($item); $productId = $item->getId(); foreach ($item->getAttributes() as $attribute) { $attribute->setProductId($productId); $attrManager->saveItem($attribute); } // if the item is a bundle, it probably contains sub-products foreach ($item->getProducts() as $subProduct) { $subProduct->setBaseId($baseId); $subProduct->setOrderProductId($productId); if (($pos = $subProduct->getPosition()) === null) { $subProduct->setPosition($position++); } else { $position = ++$pos; } $manager->saveItem($subProduct); $subProductId = $subProduct->getId(); foreach ($subProduct->getAttributes() as $attribute) { $attribute->setProductId($subProductId); $attrManager->saveItem($attribute); } } } }
/** * Returns an list of order data required by PayPal. * * @param MShop_Order_Item_Base_Interface $orderBase Order base item * @return array Associative list of key/value pairs with order data required by PayPal */ protected function _getOrderDetails(MShop_Order_Item_Base_Interface $orderBase) { $values = $this->_getAuthParameter(); try { $orderAddressDelivery = $orderBase->getAddress(MShop_Order_Item_Base_Address_Abstract::TYPE_DELIVERY); /* setting up the shipping address details (ReviewOrder) */ $values['ADDROVERRIDE'] = 1; $values['PAYMENTREQUEST_0_SHIPTONAME'] = $orderAddressDelivery->getFirstName() . ' ' . $orderAddressDelivery->getLastName(); $values['PAYMENTREQUEST_0_SHIPTOSTREET'] = $orderAddressDelivery->getAddress1() . ' ' . $orderAddressDelivery->getAddress2() . ' ' . $orderAddressDelivery->getAddress3(); $values['PAYMENTREQUEST_0_SHIPTOCITY'] = $orderAddressDelivery->getCity(); $values['PAYMENTREQUEST_0_SHIPTOSTATE'] = $orderAddressDelivery->getState(); $values['PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE'] = $orderAddressDelivery->getCountryId(); $values['PAYMENTREQUEST_0_SHIPTOZIP'] = $orderAddressDelivery->getPostal(); } catch (Exception $e) { } // If no address is available $lastPos = 0; foreach ($orderBase->getProducts() as $product) { $lastPos = $product->getPosition() - 1; $values['L_PAYMENTREQUEST_0_NUMBER' . $lastPos] = $product->getId(); $values['L_PAYMENTREQUEST_0_NAME' . $lastPos] = $product->getName(); $values['L_PAYMENTREQUEST_0_QTY' . $lastPos] = $product->getQuantity(); $values['L_PAYMENTREQUEST_0_AMT' . $lastPos] = $product->getPrice()->getValue(); } foreach ($orderBase->getServices() as $service) { if (($val = $service->getPrice()->getValue()) > '0.00') { $lastPos++; $values['L_PAYMENTREQUEST_0_NAME' . $lastPos] = $service->getName(); $values['L_PAYMENTREQUEST_0_QTY' . $lastPos] = '1'; $values['L_PAYMENTREQUEST_0_AMT' . $lastPos] = $val; } } $paymentItem = $orderBase->getService('payment'); if (($paymentCosts = $paymentItem->getPrice()->getCosts()) > '0.00') { $lastPos++; $values['L_PAYMENTREQUEST_0_NAME' . $lastPos] = $this->_getContext()->getI18n()->dt('mshop', 'Payment costs'); $values['L_PAYMENTREQUEST_0_QTY' . $lastPos] = '1'; $values['L_PAYMENTREQUEST_0_AMT' . $lastPos] = $paymentCosts; } $price = $orderBase->getPrice(); $amount = $price->getValue() + $price->getCosts(); $values['MAXAMT'] = $amount + 0.01; // @todo rounding error? $values['PAYMENTREQUEST_0_AMT'] = number_format($amount, 2, '.', ''); $values['PAYMENTREQUEST_0_ITEMAMT'] = (string) ($price->getValue() + $paymentCosts); $values['PAYMENTREQUEST_0_SHIPPINGAMT'] = (string) ($price->getCosts() - $paymentCosts); $values['PAYMENTREQUEST_0_INSURANCEAMT'] = '0.00'; $values['PAYMENTREQUEST_0_INSURANCEOPTIONOFFERED'] = 'false'; $values['PAYMENTREQUEST_0_SHIPDISCAMT'] = '0.00'; $values['PAYMENTREQUEST_0_TAXAMT'] = $price->getTaxRate(); $values['PAYMENTREQUEST_0_CURRENCYCODE'] = $orderBase->getPrice()->getCurrencyId(); $values['PAYMENTREQUEST_0_PAYMENTACTION'] = $this->_getConfigValue(array('paypalexpress.PaymentAction'), 'sale'); try { $orderServiceDeliveryItem = $orderBase->getService('delivery'); $values['L_SHIPPINGOPTIONAMOUNT0'] = (string) ($price->getCosts() - $paymentCosts); $values['L_SHIPPINGOPTIONLABEL0'] = $orderServiceDeliveryItem->getName(); $values['L_SHIPPINGOPTIONNAME0'] = $orderServiceDeliveryItem->getCode(); $values['L_SHIPPINGOPTIONISDEFAULT0'] = 'true'; } catch (Exception $e) { } // If no delivery service is available return $values; }
protected function loadDownloadAttrList(MShop_Order_Item_Base_Interface $order, $context) { $downloads = array(); $downloadManager = MShop_Factory::createManager($context, 'download'); foreach ($order->getProducts() as $product) { $search = $downloadManager->createSearch(true); $expr = array($search->compare('==', 'download.prodid', $product->getId()), $search->compare('==', 'download.orderid', $order->getId()), $search->getConditions()); $search->setConditions($search->combine('&&', $expr)); $dlList = $downloadManager->searchItems($search); if (count($dlList) > 0) { $download = reset($dlList); $downloads[$product->getId()] = $download->getFileId(); } } return $downloads; }
/** * Returns a list of tax rates and their price items for the given basket. * * @param MShop_Order_Item_Base_Interface $basket Basket containing the products, services, etc. * @return array Associative list of tax rates as key and corresponding price items as value */ protected function _getPriceByTaxRate(MShop_Order_Item_Base_Interface $basket) { $taxrates = array(); foreach ($basket->getProducts() as $product) { $price = $product->getPrice(); $taxrate = $price->getTaxRate(); if (isset($taxrates[$taxrate])) { $taxrates[$taxrate]->addItem($price); } else { $taxrates[$taxrate] = $price; } } try { $price = $basket->getService('delivery')->getPrice(); $taxrate = $price->getTaxRate(); if (isset($taxrates[$taxrate])) { $taxrates[$taxrate]->addItem($price); } else { $taxrates[$taxrate] = $price; } } catch (Exception $e) { } // if delivery service isn't available try { $price = $basket->getService('payment')->getPrice(); $taxrate = $price->getTaxRate(); if (isset($taxrates[$taxrate])) { $taxrates[$taxrate]->addItem($price); } else { $taxrates[$taxrate] = $price; } } catch (Exception $e) { } // if payment service isn't available return $taxrates; }
/** * Adds the product list to the XML object * * @param MShop_Order_Item_Base_Interface $base Order base object * @param DOMDocument $dom DOM document object with contains the XML structure * @param DOMElement $orderitem DOM element which will be the parent of the new child * @throws DOMException If an error occures */ protected function _buildXMLProducts(MShop_Order_Item_Base_Interface $base, DOMDocument $dom, DOMElement $orderitem) { $productlist = $dom->createElement('productlist'); foreach ($base->getProducts() as $product) { $price = $product->getPrice(); $total = $price->getValue() + $price->getCosts(); $productitem = $dom->createElement('productitem'); $this->_appendChildCDATA('position', $product->getPosition(), $dom, $productitem); $this->_appendChildCDATA('code', $product->getProductCode(), $dom, $productitem); $this->_appendChildCDATA('name', $product->getName(), $dom, $productitem); $this->_appendChildCDATA('quantity', $product->getQuantity(), $dom, $productitem); $priceitem = $dom->createElement('priceitem'); $this->_appendChildCDATA('price', number_format($price->getValue(), 2, '.', ''), $dom, $priceitem); $this->_appendChildCDATA('shipping', number_format($price->getCosts(), 2, '.', ''), $dom, $priceitem); $this->_appendChildCDATA('discount', number_format(0.0, 2, '.', ''), $dom, $priceitem); $this->_appendChildCDATA('total', number_format($total, 2, '.', ''), $dom, $priceitem); $productitem->appendChild($priceitem); if ($product->getType() === 'bundle') { $this->_buildXMLChildList($product, $product->getProducts(), $dom, $productitem); } $productlist->appendChild($productitem); } $orderitem->appendChild($productlist); }
/** * Returns the IDs of the products in the current basket. * * @param MShop_Order_Item_Base_Interface $basket Basket object * @return string[] List of product IDs */ protected function _getProductIdsFromBasket(MShop_Order_Item_Base_Interface $basket) { $list = array(); foreach ($basket->getProducts() as $orderProduct) { $list[$orderProduct->getProductId()] = true; foreach ($orderProduct->getProducts() as $subProduct) { $list[$subProduct->getProductId()] = true; } } return array_keys($list); }
/** * Migrates the products from the old basket to the current one. * * @param MShop_Order_Item_Base_Interface $basket Basket object * @param array $errors Associative list of previous errors * @param string $localeKey Unique identifier of the site, language and currency * @return array Associative list of errors occured */ private function _copyProducts(MShop_Order_Item_Base_Interface $basket, array $errors, $localeKey) { foreach ($basket->getProducts() as $pos => $product) { if ($product->getFlags() & MShop_Order_Item_Base_Product_Abstract::FLAG_IMMUTABLE) { continue; } try { $attrIds = array(); foreach ($product->getAttributes() as $attrItem) { $attrIds[$attrItem->getType()][] = $attrItem->getAttributeId(); } $this->addProduct($product->getProductId(), $product->getQuantity(), array(), $this->_getValue($attrIds, 'variant', array()), $this->_getValue($attrIds, 'config', array()), $this->_getValue($attrIds, 'hidden', array()), $this->_getValue($attrIds, 'custom', array()), $product->getWarehouseCode()); $basket->deleteProduct($pos); } catch (Exception $e) { $code = $product->getProductCode(); $logger = $this->_getContext()->getLogger(); $str = 'Error migrating product with code "%1$s" in basket to locale "%2$s": %3$s'; $logger->log(sprintf($str, $code, $localeKey, $e->getMessage()), MW_Logger_Abstract::INFO); $errors['product'][$pos] = $e->getMessage(); } } return $errors; }
/** * Returns a list of tax rates and their price items for the given basket. * * @param MShop_Order_Item_Base_Interface $basket Basket containing the products, services, etc. * @return array Associative list of tax rates as key and corresponding price items as value */ protected function _getPriceByTaxRate(MShop_Order_Item_Base_Interface $basket) { $taxrates = array(); $manager = MShop_Factory::createManager($this->_getContext(), 'price'); foreach ($basket->getProducts() as $product) { $price = $product->getPrice(); $taxrate = $price->getTaxRate(); if (!isset($taxrates[$taxrate])) { $taxrates[$taxrate] = $manager->createItem(); } $taxrates[$taxrate]->addItem($price, $product->getQuantity()); } try { $price = $basket->getService('delivery')->getPrice(); $taxrate = $price->getTaxRate(); if (!isset($taxrates[$taxrate])) { $taxrates[$taxrate] = $manager->createItem(); } $taxrates[$taxrate]->addItem($price); } catch (Exception $e) { } // if delivery service isn't available try { $price = $basket->getService('payment')->getPrice(); $taxrate = $price->getTaxRate(); if (!isset($taxrates[$taxrate])) { $taxrates[$taxrate] = $manager->createItem(); } $taxrates[$taxrate]->addItem($price); } catch (Exception $e) { } // if payment service isn't available return $taxrates; }