private static function getCategoryCondition(Category $category) { $own = new EqualsCond(new ARFieldHandle(__CLASS__, 'categoryID'), $category->getID()); $parent = new EqualsOrLessCond(new ARFieldHandle('Category', 'lft'), $category->lft->get()); $parent->addAND(new EqualsOrMoreCond(new ARFieldHandle('Category', 'rgt'), $category->rgt->get())); $parent->addAND(new EqualsCond(new ARFieldHandle(__CLASS__, 'isSubcategories'), true)); $own->addOR($parent); return $own; }
/** * Calculate a delivery rate for a particular shipment * * @return ShipmentDeliveryRate */ public function getDeliveryRate(Shipment $shipment) { $hasFreeShipping = false; // get applicable rates if (self::WEIGHT_BASED == $this->rangeType->get()) { $weight = $shipment->getChargeableWeight($this->deliveryZone->get()); $cond = new EqualsOrLessCond(new ARFieldHandle('ShippingRate', 'weightRangeStart'), $weight * 1.000001); $cond->addAND(new EqualsOrMoreCond(new ARFieldHandle('ShippingRate', 'weightRangeEnd'), $weight * 0.99999)); } else { $total = $shipment->getSubTotal(Shipment::WITHOUT_TAXES); $cond = new EqualsOrLessCond(new ARFieldHandle('ShippingRate', 'subtotalRangeStart'), $total * 1.000001); $cond->addAND(new EqualsOrMoreCond(new ARFieldHandle('ShippingRate', 'subtotalRangeEnd'), $total * 0.99999)); } $f = new ARSelectFilter(new EqualsCond(new ARFieldHandle('ShippingRate', 'shippingServiceID'), $this->getID())); $f->mergeCondition($cond); $rates = ActiveRecordModel::getRecordSet('ShippingRate', $f); if (!$rates->size()) { return null; } $itemCount = $shipment->getChargeableItemCount($this->deliveryZone->get()); $maxRate = 0; foreach ($rates as $rate) { $charge = $rate->flatCharge->get(); foreach ($shipment->getItems() as $item) { $charge += $rate->getItemCharge($item) * $item->getCount(); } if (self::WEIGHT_BASED == $this->rangeType->get()) { $charge += $rate->perKgCharge->get() * $weight; } else { $charge += $rate->subtotalPercentCharge->get() / 100 * $total; } if ($charge > $maxRate) { $maxRate = $charge; } } return ShipmentDeliveryRate::getNewInstance($this, $maxRate); }
public static function loadOptionsForProductSet(ARSet $products) { // load category options $f = new ARSelectFilter(); $categories = $productIDs = array(); foreach ($products as $product) { foreach ($product->getAllCategories() as $cat) { $categories[$cat->getID()] = $cat; } $productIDs[] = $product->getID(); if ($product->parent->get()) { $productIDs[] = $product->parent->get()->getID(); } } foreach ($categories as $category) { if ($category->isLoaded() == false) { $category->load(); } $c = new EqualsOrLessCond(new ARFieldHandle('Category', 'lft'), $category->lft->get()); $c->addAND(new EqualsOrMoreCond(new ARFieldHandle('Category', 'rgt'), $category->rgt->get())); if (!isset($categoryCond)) { $categoryCond = $c; } else { $categoryCond->addOR($c); } } // product options $productCond = new INCond(new ARFieldHandle('ProductOption', 'productID'), $productIDs); if (!isset($categoryCond)) { $categoryCond = $productCond; } else { $categoryCond->addOR($productCond); } $f->setCondition($categoryCond); // ordering $f->setOrder(new ARFieldHandle('ProductOption', 'productID'), 'DESC'); $f->setOrder(new ARFieldHandle('Category', 'lft'), 'DESC'); $f->setOrder(new ARFieldHandle('ProductOption', 'position'), 'DESC'); $options = ProductOption::getRecordSet($f, array('DefaultChoice' => 'ProductOptionChoice', 'Category')); self::loadChoicesForRecordSet($options); // sort by products $sorted = array(); foreach ($products as $product) { foreach ($options as $index => $option) { if ($option->product->get() && ($option->product->get()->getID() == $product->getID() || $product->parent->get() && $option->product->get()->getID() == $product->parent->get()->getID())) { $sorted[$product->getID()][] = $option; } if ($option->category->get()) { $option->category->get()->load(); foreach ($product->getAllCategories() as $category) { if ($option->category->get()->isAncestorOf($category)) { $sorted[$product->getID()][] = $option; break; } } } } } return $sorted; }