/**
  * Get a shopping cart instance.
  */
 protected function getShoppingCart()
 {
     $shoppingCart = new ShoppingCart();
     $shoppingCart->setContainer($this->container);
     $shoppingCart->setCheckoutHelper(new CheckoutHelper());
     return $shoppingCart;
 }
Exemple #2
0
 /**
  * Start mocking around.
  *
  * @param ShoppingCart shoppingCart The current shopping cart.
  * @param ZenMagick\StoreBundle\Entity\Address shippingAddress Optional shipping address; default is <code>null</code>.
  */
 public static function startMock(ShoppingCart $shoppingCart, $shippingAddress = null)
 {
     global $order, $shipping_weight, $shipping_quoted, $shipping_num_boxes, $total_count, $order_total_modules;
     global $_order, $_shipping_weight, $_shipping_quoted, $_shipping_num_boxes, $_total_count, $_order_total_modules;
     if (self::$mock++) {
         // already mocking
         return;
     }
     // save originals
     $_order = $order;
     $_shipping_weight = $shipping_weight;
     $_shipping_quoted = $shipping_quoted;
     $_shipping_num_boxes = $shipping_num_boxes;
     $_total_count = $total_count;
     $_order_total_modules = $order_total_modules;
     if (!isset($_SESSION['cart'])) {
         $_SESSION['cart'] = new \ZenMagick\ZenCartBundle\Compat\ShoppingCart();
     }
     // get total number of products, not line items...
     $total_count = 0;
     foreach ($shoppingCart->getItems() as $item) {
         $total_count += $item->getQuantity();
     }
     //$order_total_modules = new ZenCartOrderTotal();
     if (null == $order || !$order instanceof ZenCartCheckoutOrder) {
         $mockOrder = new ZenCartCheckoutOrder();
         $mockOrder->setContainer(Runtime::getContainer());
         $mockOrder->setShoppingCart($shoppingCart);
         if (null != $shippingAddress) {
             $mockOrder->setShippingAddress($shippingAddress);
         }
         $order = $mockOrder;
     }
     // START: adjust boxes, weight and tare
     $shipping_quoted = '';
     $shipping_num_boxes = 1;
     $shipping_weight = $shoppingCart->getWeight();
     $za_tare_array = preg_split("/[:,]/", SHIPPING_BOX_WEIGHT);
     $zc_tare_percent = $za_tare_array[0];
     $zc_tare_weight = $za_tare_array[1];
     $za_large_array = preg_split("/[:,]/", SHIPPING_BOX_PADDING);
     $zc_large_percent = $za_large_array[0];
     $zc_large_weight = $za_large_array[1];
     switch (true) {
         // large box add padding
         case SHIPPING_MAX_WEIGHT <= $shipping_weight:
             $shipping_weight = $shipping_weight + $shipping_weight * ($zc_large_percent / 100) + $zc_large_weight;
             break;
         default:
             // add tare weight < large
             $shipping_weight = $shipping_weight + $shipping_weight * ($zc_tare_percent / 100) + $zc_tare_weight;
             break;
     }
     if ($shipping_weight > SHIPPING_MAX_WEIGHT) {
         // Split into many boxes
         $shipping_num_boxes = ceil($shipping_weight / SHIPPING_MAX_WEIGHT);
         $shipping_weight = $shipping_weight / $shipping_num_boxes;
     }
     // END: adjust boxes, weight and tare
 }
 /**
  * {@inheritDoc}
  */
 public function calculate($request, ShoppingCart $shoppingCart)
 {
     $paymentType = $shoppingCart->getSelectedPaymentType();
     // iterate over all conditions
     $output = array();
     foreach ($this->container->get('settingsService')->get('plugins.paymentSurcharge.conditions', array()) as $condition) {
         if ($paymentType->getId() == $condition['code'] || null === $condition['code']) {
             // payment module match
             if (null != $condition['cvalue']) {
                 $cvalueToken = explode(':', $condition['cvalue']);
                 if (2 == count($cvalueToken)) {
                     $cvalueType = $cvalueToken[0];
                     $cvalueName = $cvalueToken[1];
                 } else {
                     $cvalueType = 'field';
                     $cvalueName = $cvalueToken[0];
                 }
                 // evaluate the value to use with the regexp
                 $cvalueNames = explode(';', $cvalueName);
                 switch ($cvalueType) {
                     case 'field':
                         $cvalue = null;
                         foreach ($cvalueNames as $name) {
                             if (isset($payment->{$name})) {
                                 $cvalue = $payment->{$name};
                             } else {
                                 $cvalue = $request->getParameter($name, null);
                             }
                             if (null !== $cvalue) {
                                 break;
                             }
                         }
                         break;
                     default:
                         $this->container->get('logger')->err('invalid condition value type: ' . $cvalueType);
                         return null;
                 }
             }
             // check eregexp
             if (null == $condition['cvalue'] && null == $condition['regexp'] || ereg($condition['regexp'], $cvalue)) {
                 // match, so apply condition
                 // evaluate the condition's value
                 $amount = 0;
                 if (is_numeric($condition['value'])) {
                     $amount = (double) $condition['value'];
                 }
                 if (0 === strpos($condition['value'], '%:')) {
                     $amount = trim(str_replace('%:', '', $condition['value']));
                     $amount = $shoppingCart->getSubtotal() * ($amount / 100);
                 }
                 $details = Beans::getBean('ZMOrderTotalLineDetails');
                 $details->setTitle($condition['title']);
                 $details->setAmount($amount);
                 $details->setDisplayValue($amount);
                 $output[] = $details;
             }
         }
     }
     return $output;
 }
 /**
  * Get zen-cart order totals.
  *
  * @param  ShoppingCart $shoppingCart The current shopping cart.
  * @return array        zencart order totals.
  */
 protected function getZenTotals(ShoppingCart $shoppingCart)
 {
     global $order, $shipping_modules;
     // save
     $otmp = $order;
     $smtmp = $shipping_modules;
     $order = new \order();
     if (!isset($shipping_modules)) {
         $ssm = array();
         if (null != ($shippingMethod = $shoppingCart->getSelectedShippingMethod())) {
             $ssm = array('id' => $shippingMethod->getShippingId(), 'title' => $shippingMethod->getName(), 'cost' => $shippingMethod->getCost());
         }
         $shipping_modules = new \shipping($ssm);
     }
     $zenTotals = new \order_total();
     $zenTotals->collect_posts();
     $zenTotals->pre_confirmation_check();
     $zenTotals->process();
     // restore
     $order = $otmp;
     $shipping_modules = $smtmp;
     return $zenTotals;
 }
 /**
 * Get the plain product id of this item.
 *
 * <p>For items without attributes this is going to be the same as <code>getId()</code>. If attributes are present,
  to use the store address.l return something like a skuId while this method will always return the plain
 * product id without any attribute hash.</p>
 *
 * @return int The product id.
 */
 public function getProductId()
 {
     return ShoppingCart::getBaseProductIdFor($this->getId());
 }
 /**
  * Populate info.
  *
  * @param ShoppingCart shoppingCart The shopping cart.
  */
 protected function populateInfo(ShoppingCart $shoppingCart)
 {
     // general stuff
     // TODO: where from/to??
     $languageId = $this->container->get('settingsService')->get('storeDefaultLanguageId');
     // TODO: move all cart/session values into ShoppingCart
     $currencyCode = $this->container->get('session')->get('currency');
     $couponAmount = 0;
     $couponCode = null;
     if (null != ($couponCodeId = $this->container->get('session')->get('cc_id'))) {
         $coupon = $this->container->get('couponService')->getCouponForId($couponCodeId, $languageId);
         if (null != $coupon) {
             $couponCode = $coupon->getCode();
             $couponAmount = $coupon->getAmount();
         }
     }
     $shippingMethod = $shoppingCart->getSelectedShippingMethod();
     $paymentType = $shoppingCart->getSelectedPaymentType();
     $orderStatus = DEFAULT_ORDERS_STATUS_ID;
     if (null != $paymentType && null !== ($pos = $paymentType->getOrderStatus())) {
         $orderStatus = $pos;
     }
     $tax = 0;
     $taxGroups = array();
     foreach ($shoppingCart->getItems() as $item) {
         $itemTotal = $item->getItemTotal(false) + $item->getOneTimeCharge(false);
         $itemTotalPlusTax = $item->getItemTotal(true) + $item->getOneTimeCharge(true);
         $itemTax = $itemTotalPlusTax - $itemTotal;
         $productTaxRate = $item->getTaxRate();
         $description = $productTaxRate->getDescription();
         if (!array_key_exists($description, $taxGroups)) {
             $taxGroups[$description] = 0;
         }
         $taxGroups[$description] += $itemTax;
         $tax += $itemTax;
     }
     $info = array('order_status' => $orderStatus, 'currency' => $currencyCode, 'currency_value' => $this->container->get('currencyService')->getCurrencyForCode($currencyCode)->getRate(), 'payment_method' => null != $paymentType ? $paymentType->getTitle() : '', 'payment_module_code' => null != $paymentType ? $paymentType->getId() : '', 'coupon_code' => $couponCode, 'shipping_method' => null != $shippingMethod ? $shippingMethod->getName() : '', 'shipping_module_code' => null != $shippingMethod ? $shippingMethod->getShippingId() : '', 'shipping_cost' => null != $shippingMethod ? $shippingMethod->getCost() : '', 'subtotal' => $shoppingCart->getSubTotal(), 'shipping_tax' => 0, 'tax' => $tax, 'total' => $shoppingCart->getTotal() + $couponAmount, 'tax_groups' => $taxGroups, 'comments' => $shoppingCart->getComments());
     $this->info = array_merge($this->info, $info);
     if ($this->container->get('settingsService')->get('apps.store.assertZencart', false)) {
         $this->assertInfo(false);
     }
 }
 /**
  * {@inheritDoc}
  */
 public function getShippingMethods(ShoppingCart $shoppingCart, $address = null)
 {
     if (null == $address) {
         // now we just want the shipping method, but we need an address right now...
         $address = $shoppingCart->getShippingAddress();
     }
     $this->errors = array();
     ZenCartMock::startMock($shoppingCart, $address);
     // create new instance for each quote!
     // this is required as most modules do stuff in the c'tor (for example zone checks)
     $clazzName = get_class($this->zenModule);
     $module = new $clazzName();
     $quotes = $module->quote();
     // capture error(s)
     if (is_array($quotes) && array_key_exists('error', $quotes)) {
         $this->errors = array($quotes['error']);
         return array();
     }
     // capture tax
     $taxRate = Beans::getBean('ZenMagick\\StoreBundle\\Entity\\TaxRate');
     $taxRate->setRate(isset($quotes['tax']) ? $quotes['tax'] : 0);
     $methods = array();
     if (is_array($quotes) && array_key_exists('methods', $quotes)) {
         foreach ($quotes['methods'] as $method) {
             $shippingMethod = new ShippingMethodWrapper($this, $method);
             $shippingMethod->setProvider($this);
             $shippingMethod->setTaxRate($taxRate);
             $methods[$shippingMethod->getId()] = $shippingMethod;
         }
     }
     ZenCartMock::cleanupMock();
     return $methods;
 }