Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class OrderRequestBuilder {
- private $siteId,$shippingMethodId;
- private $items = array();
- function __construct($orderOrSiteId) {
- if ( $orderOrSiteId instanceof Order ) {
- $this->siteId = $orderOrSiteId->getSite()->getId();
- $this->shippingMethodId =
- $orderOrSiteId->getShippingMethod()->getId();
- foreach ( $orderOrSiteId->getLines() as $line ) {
- $this->items[$line->getSaleableItem()->getId()] =
- $line->getQuantity();
- }
- } else {
- $this->siteId = $orderOrSiteId;
- }
- }
- function setItem($saleableItemId, $quantity) {
- $this->items[$saleableItemId] = $quantity;
- }
- function setShippingMethod($shippingMethodId) {
- $this->shippingMethodId = $shippingMethodId;
- }
- function buildParams() {
- $items = array();
- foreach ( $this->items as $id => $qty ) {
- $items[] = sprintf('%d:%d', $id, $qty);
- }
- if ( ! $items ) {
- throw new OrderRequestBuilderException('No items');
- }
- if ( ! $this->shippingMethodId ) {
- throw new OrderRequestBuilderException('Shipping method undefined');
- }
- return array(
- 'site' => $this->siteId,
- 'method' => $this->shippingMethodId,
- 'items' => implode(',', $items),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment