public function calculate_shipping($package) { $package = WbsPackage::fromWcPackage($package); if (!$this->weight->includes($package->getWeight()) || !$this->subtotal->includes($package->getPrice($this->subtotalWithTax))) { return; } $defaultRate = new WbsBucketRate('___wbs_base_pseudo_class', $this->fee, $this->weightRate); /** @var WbsItemBucket[] $buckets */ $buckets = array(); foreach ($package->getLines() as $line) { $product = $line->getProduct(); $class = $product->get_shipping_class(); $rate = $this->shippingClassRates->findById($class); if ($rate == null) { $rate = $defaultRate; } $class = $rate->getId(); if (!isset($buckets[$class])) { $buckets[$class] = new WbsItemBucket(0, $rate); } $buckets[$class]->add($line->getWeight()); } $defaultClass = $defaultRate->getId(); if (!isset($buckets[$defaultClass])) { $buckets[$defaultClass] = new WbsItemBucket(0, $defaultRate); } $price = 0; foreach ($buckets as $bucket) { $price += $bucket->calculate(); } $price = $this->priceClamp->clamp($price); $this->add_rate(array('id' => $this->id, 'label' => $this->title, 'cost' => $price, 'taxes' => '', 'calc_tax' => 'per_order')); }