public function add_coupon() { $request = $this->application->getRequest(); $code = $request->get('couponCode'); $orderID = $request->get('orderID'); $response = new LiveCartSimpleXMLElement('<response datetime="' . date('c') . '"></response>'); if (strlen($code)) { $order = CustomerOrder::getInstanceByID($orderID, CustomerOrder::LOAD_DATA); $condition = DiscountCondition::getInstanceByCoupon($code); if ($condition) { if (!$order->hasCoupon($code)) { $coupon = OrderCoupon::getNewInstance($order, $code); $coupon->save(); $order->getCoupons(true); if ($order->hasCoupon($code)) { $order->loadAll(); $order->getTotal(true); $order->totalAmount->set($order->getTotal(true)); $order->getTaxAmount(); $order->save(); $response->addChild('message', 'Coupon ' . $code . ' was successfully added'); } else { throw new Exception("Cant add coupon " . $code); } } else { throw new Exception("The coupon " . $code . " already added"); } } else { throw new Exception("Cant add coupon " . $code); } $order->getCoupons(true); } else { throw new Exception("Incorrect coupon code" . $code); } return new SimpleXMLResponse($response); }
/** * Update product quantities */ public function update() { // TOS if ($this->isTosInCartPage()) { $this->session->set('tos', $this->request->get('tos')); } // coupon code if ($this->request->get('coupon')) { $code = $this->request->get('coupon'); if ($condition = DiscountCondition::getInstanceByCoupon($code)) { if (!$this->order->hasCoupon($code)) { $coupon = OrderCoupon::getNewInstance($this->order, $code); $coupon->save(); $this->order->getCoupons(true); if ($this->order->hasCoupon($code)) { $this->setMessage($this->makeText('_coupon_added', array($code))); } } } else { $this->setErrorMessage($this->makeText('_coupon_not_found', array($code))); } $this->order->getCoupons(true); } $this->updateEstimateAddress(); $this->order->loadItemData(); $validator = $this->buildCartValidator($this->order, $this->getItemOptions()); if (!$validator->isValid()) { return new ActionRedirectResponse('order', 'index'); } $this->order->loadRequestData($this->request); foreach ($this->order->getOrderedItems() as $item) { if ($this->request->isValueSet('item_' . $item->getID())) { foreach ($item->getProduct()->getOptions(true) as $option) { $this->modifyItemOption($item, $option, $this->request, $this->getFormFieldName($item, $option)); } $item->save(); $this->order->updateCount($item, $this->request->get('item_' . $item->getID(), 0)); } } if ($this->order->isMultiAddress->get()) { $addresses = $this->user->getShippingAddressSet(); $this->order->getShipments(); foreach ($this->order->getOrderedItems() as $item) { if ($addressId = $this->request->get('address_' . $item->getID())) { if (!$item->shipment->get() || !$item->shipment->get()->shippingAddress->get() || $item->shipment->get()->shippingAddress->get()->getID() != $addressId) { foreach ($this->order->getShipments() as $shipment) { if ($shipment->shippingAddress->get() && $shipment->shippingAddress->get()->getID() == $addressId) { if (!$item->shipment->get() || $item->shipment->get()->getID() != $shipment->getID()) { if ($item->shipment->get()) { $item->shipment->get()->removeItem($item); } $shipment->addItem($item); break; } } $shipment = null; } if (!isset($shipment) || !$shipment) { $address = ActiveRecordModel::getInstanceById('UserAddress', $addressId, true); $shipment = Shipment::getNewInstance($this->order); $shipment->shippingAddress->set($address); $shipment->save(); $this->order->addShipment($shipment); $shipment->addItem($item); } $item->save(); } } if ($item->shipment->get()) { $item->shipment->get()->shippingAmount->set(0); $item->shipment->get()->shippingServiceData->set(null); $item->shipment->get()->save(); } } } $this->order->mergeItems(); SessionOrder::save($this->order); // proceed with the checkout if ($this->request->get('proceed')) { return new ActionRedirectResponse('checkout', 'index'); } // redirect to payment gateway if ($url = $this->request->get('redirect')) { return new RedirectResponse($url); } return new ActionRedirectResponse('order', 'index', array('query' => 'return=' . $this->request->get('return'))); }
/** * Validate a discount condition against customer properties * * @param DiscountCondition * @return bool */ public function isValidCondition(DiscountCondition $condition) { switch ($condition->getSourceField()) { case 'group': $condition->setSourceValue($this->getGroup()); break; case 'billing_state': $condition->setSourceValue($this->getBillingState()); break; case 'shipping_state': $condition->setSourceValue($this->getShippingState()); break; default: //no-op break; } return $condition->isValid(); }
public function updateActionField() { list($fieldName, $id) = explode('_', $this->request->get('field')); $value = $this->request->get('value'); $action = ActiveRecordModel::getInstanceByID('DiscountAction', $id, DiscountAction::LOAD_DATA, array('DiscountCondition', 'DiscountCondition_ActionCondition')); if ('type' == $fieldName) { switch ($value) { case DiscountAction::TYPE_ORDER_DISCOUNT: $action->type->set(DiscountAction::TYPE_ORDER_DISCOUNT); $action->actionCondition->set(null); break; case DiscountAction::TYPE_ITEM_DISCOUNT: $action->type->set(DiscountAction::TYPE_ITEM_DISCOUNT); $action->actionCondition->set($action->condition->get()); break; case DiscountAction::TYPE_CUSTOM_DISCOUNT: $newCondition = DiscountCondition::getNewInstance(); $newCondition->isEnabled->set(true); $newCondition->isActionCondition->set(true); $newCondition->isAnyRecord->set(true); $newCondition->save(); $action->actionCondition->set($newCondition); $action->type->set(DiscountAction::TYPE_ITEM_DISCOUNT); $action->save(); return new JSONResponse(array('field' => $fieldName, 'condition' => $newCondition->toArray())); break; } } else { if ($this->request->get('isParam') != 'false') { $action->setParamValue($fieldName, $value); $fieldName .= '_' . $action->getID(); } else { $action->{$fieldName}->set($value); } } $action->save(); return new JSONResponse($fieldName); }
/** * Check if this Item validates a condition * * @param DiscountCondition * @return bool */ public function isValidCondition(DiscountCondition $condition) { switch ($condition->getSourceEntityField()) { case 'price': $condition->setSourceValue($this->getPrice()); break; case 'weight': $condition->setSourceValue($this->getWeight()); break; case 'sku': $condition->setSourceValue($this->getSku()); break; case 'qty': $condition->setSourceValue($this->getQty()); break; case 'category_ids_csv': $condition->setSourceValue($this->getCategoryIdsCsv()); break; } return $condition->isValid(); }
/** * Create DiscountCondition */ static function createDiscountConditionFromEntity($entity) { $discountCondition = new DiscountCondition(); $discountCondition->setId($entity->getId())->setName($entity->getName())->setCompareType($entity->getCompareType())->setCompareValue($entity->getCompareValue())->setSourceEntityType($entity->getSourceEntityType())->setSourceEntityField($entity->getSourceEntityField())->setSourceEntityFieldType($entity->getSourceEntityFieldType()); return $discountCondition; }
/** * Validate a DiscountCondition against this Cart instance * * @param DiscountCondition * @return bool */ public function isValidCondition(DiscountCondition $condition) { /* Note: the Discount system is not using this yet */ switch ($condition->getSourceField()) { case 'total': $condition->setSourceValue($this->getCalculator()->getTotal()); break; case 'item_total': $condition->setSourceValue($this->getCalculator()->getItemTotal()); break; case 'shipment_total': $condition->setSourceValue($this->getCalculator()->getShipmentTotal()); break; case 'discounted_item_total': $condition->setSourceValue($this->getCalculator()->getDiscountedItemTotal()); break; case 'discounted_shipment_total': $condition->setSourceValue($this->getCalculator()->getDiscountedShipmentTotal()); break; default: //no-op break; } return $condition->isValid(); }
public function addCoupon() { ClassLoader::import('application.model.discount.DiscountCondition'); $response = $this->getRequest(); $code = $this->request->get('coupon'); $msg = '_coupon_not_found'; $error = true; if (strlen($code)) { $order = CustomerOrder::getInstanceByID($response->get('id'), CustomerOrder::LOAD_DATA); $condition = DiscountCondition::getInstanceByCoupon($code); if ($condition) { if (!$order->hasCoupon($code)) { $coupon = OrderCoupon::getNewInstance($order, $code); $coupon->save(); $order->getCoupons(true); if ($order->hasCoupon($code)) { $msg = '_coupon_added'; $this->recalculateDiscounts(); $error = false; } else { $msg = '_cant_add_coupon'; } } else { $msg = '_coupon_already_added'; } } $order->getCoupons(true); } $this->loadLanguageFile('Order'); return new JSONResponse(null, $error ? 'failure' : 'success', $this->makeText($msg, array($code))); }
public function validateCoupons() { if (!$this->isFinalized->get()) { foreach ($this->getCoupons() as $coupon) { if (!$coupon->discountCondition->get()) { $coupon->discountCondition->set(DiscountCondition::getInstanceByCoupon($coupon->couponCode->get())); $coupon->save(); } if (!$coupon->discountCondition->get() || $coupon->discountCondition->get()->couponCode->get() != $coupon->couponCode->get() || !$coupon->isValid()) { $coupon->delete(); $this->getCoupons()->removeRecord($coupon); } } } }
/** * Import object from stdClass * * @param stdClass * @param bool * @return DiscountConditionCompare */ public function importStdClass($obj, $reset = true) { if ($reset) { $this->reset(); } $op = isset($obj->op) ? $obj->op : ''; $isNot = (bool) isset($obj->is_not) ? $obj->is_not : false; $left = null; $right = null; $leftData = isset($obj->left) ? $obj->left : null; $rightData = isset($obj->right) ? $obj->right : null; $conditions = isset($obj->conditions) ? $obj->conditions : array(); if (is_array($leftData) || $leftData instanceof stdClass) { if (isset($leftData['op']) || isset($leftData->op)) { //we have DiscountConditionCompare data if ($leftData instanceof stdClass) { $left = new DiscountConditionCompare(); $left->importStdClass($leftData); } else { if (is_array($leftData)) { $left = new DiscountConditionCompare(); $left->importJson(json_encode($leftData)); } } } else { //we have DiscountCondition data if ($leftData instanceof stdClass) { $left = new DiscountCondition(); $left->importStdClass($leftData); } else { if (is_array($leftData)) { $left = new DiscountCondition(); $left->importJson(json_encode($leftData)); } } } } if (is_array($rightData) || $rightData instanceof stdClass) { if (isset($rightData['op']) || isset($rightData->op)) { //we have DiscountConditionCompare data if ($rightData instanceof stdClass) { $right = new DiscountConditionCompare(); $right->importStdClass($rightData); } else { if (is_array($rightData)) { $right = new DiscountConditionCompare(); $right->importJson(json_encode($rightData)); } } } else { //we have DiscountCondition data if ($rightData instanceof stdClass) { $right = new DiscountCondition(); $right->importStdClass($rightData); } else { if (is_array($rightData)) { $right = new DiscountCondition(); $right->importJson(json_encode($rightData)); } } } } if ((is_array($conditions) || $conditions instanceof stdClass) && count($conditions) > 0) { foreach ($conditions as $data) { if (isset($data->op) || is_array($data) && isset($data['op'])) { //we have DiscountConditionCompare data $compare = new DiscountConditionCompare(); if ($data instanceof stdClass) { $compare->importStdClass($data); $this->addCondition($compare); } else { if (is_array($data)) { $compare->importJson(json_encode($data)); $this->addCondition($compare); } } } else { //we have DiscountCondition data $condition = new DiscountCondition(); if ($data instanceof stdClass) { $condition->importStdClass($data); $this->addCondition($condition); } else { if (is_array($data)) { $condition->importJson(json_encode($data)); $this->addCondition($condition); } } } } } $this->setOp($op)->setIsNot($isNot)->setLeftCondition($left)->setRightCondition($right)->setConditions($conditions); return $this; }
public static function create(DiscountCondition $condition) { return self::createFromArray($condition->toArray()); }
public function testPastOrdersInQuantityPrices() { ActiveRecordModel::getApplication()->getConfig()->setRuntime('INVENTORY_TRACKING', 'DISABLE'); $condition = DiscountCondition::getNewInstance(); $condition->isEnabled->set(true); $condition->save(); $action = DiscountAction::getNewInstance($condition); $action->actionClass->set('RuleActionIncludePastOrdersInQuantityPrices'); $action->isEnabled->set(true); $action->save(); $this->product1->isEnabled->set(true); $this->product1->stockCount->set(10); $this->product1->save(); // price = 10 $price = $this->product1->getPricingHandler()->getPrice($this->usd); $price->setPriceRule(3, null, 5); $price->setPriceRule(4, null, 4); $price->save(); $this->order->addProduct($this->product1, 2, true); $this->order->save(); $this->assertEquals(20, $this->order->getTotal(true)); $this->order->finalize(); $this->order->isPaid->set(true); $this->order->save(); $this->order->reload(); ActiveRecord::clearPool(); // new order, purchase 1 item, but receive discount like for 3 items $this->validatePastOrderQtPriceOrderTotal(1, 5); $this->validatePastOrderQtPriceOrderTotal(2, 8); // test with date range $action->setParamValue('days', 3); $action->save(); $this->order->dateCompleted->set(strtotime('now -4 days')); $this->order->save(); $this->validatePastOrderQtPriceOrderTotal(1, 10); $action->setParamValue('days', 4); $action->save(); $this->validatePastOrderQtPriceOrderTotal(1, 5); $this->order->dateCompleted->set(strtotime('now -2 days')); $this->order->save(); $action->save(); $this->validatePastOrderQtPriceOrderTotal(1, 5); $this->order->dateCompleted->set(strtotime('now -20 days')); $this->order->save(); $action->save(); $this->validatePastOrderQtPriceOrderTotal(1, 10); }
public function testPaymentMethodSurcharge() { $condition = DiscountCondition::getNewInstance(); $condition->isEnabled->set(true); $condition->conditionClass->set('RuleConditionPaymentMethodIs'); $condition->addValue('TESTING'); $condition->save(); $action = DiscountAction::getNewInstance($condition, 'RuleActionPercentageSurcharge'); $action->actionCondition->set($condition); $action->isEnabled->set(true); $action->type->set(DiscountAction::TYPE_ORDER_DISCOUNT); $action->amount->set(10); $action->save(); $this->order->addProduct($this->products[1], 1, true); $this->order->setPaymentMethod('TESTING'); $this->order->save(); $price = $this->products[1]->getPrice($this->usd); $this->assertEquals((int) ($price * 1.1), (int) $this->order->getTotal(true)); $action->actionClass->set('RuleActionFixedDiscount'); $action->save(); $this->assertEquals((int) ($price - 10), (int) $this->order->getTotal(true)); $action->actionClass->set('RuleActionFixedSurcharge'); $action->save(); $this->assertEquals((int) ($price + 10), (int) $this->order->getTotal(true)); $action->actionClass->set('RuleActionPercentageDiscount'); $action->save(); $this->assertEquals((int) ($price * 0.9), (int) $this->order->getTotal(true)); }
/** * Check whether this shipment validates a discount condition * * @param DiscountCondition * @return bool */ public function isValidCondition(DiscountCondition $condition) { switch ($condition->getSourceEntityField()) { case 'code': $condition->setSourceValue($this->getCode()); break; case 'price': $condition->setSourceValue($this->getPrice()); break; default: //no-op break; } return $condition->isValid(); }
public function registerSubCondition(DiscountCondition $condition) { $this->subConditions[$condition->getID()] = $condition; }