Source of file OrderShippingExtension.php
Size: 3,349 Bytes - Last Modified: 2021-12-23T10:25:09+00:00
/var/www/docs.ssmods.com/process/src/src/Extension/OrderShippingExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 | <?php namespace SilverShop\Shipping\Extension; use SilverStripe\ORM\DataExtension; use SilverShop\Shipping\ShippingPackage; use SilverShop\Shipping\ShippingEstimator; use SilverShop\Shipping\Model\ShippingMethod; use SilverShop\Shipping\Model\Zone; use Exception; class OrderShippingExtension extends DataExtension { private static $db = [ 'ShippingTotal' => 'Currency' ]; private static $has_one = [ 'ShippingMethod' => ShippingMethod::class ]; private static $casting = [ 'TotalWithoutShipping' => 'Currency' ]; public function TotalWithoutShipping(){ return $this->owner->Total() - $this->owner->ShippingTotal; } public function createShippingPackage($value=0) { //create package, with total weight, dimensions, value, etc $weight = $width = $height = $depth = $quantity = 0; $items = $this->owner->Items(); if (!$items->exists()) { $package = ShippingPackage::create(); } else { $weight = $items->Sum('Weight', true); //Sum is found on OrdItemList (Component Extension) $width = $items->Sum('Width', true); $height = $items->Sum('Height', true); $depth = $items->Sum('Depth', true); if( !$value ) { $value = $this->owner->SubTotal(); } $quantity = $items->Quantity(); $package = ShippingPackage::create( $weight, [$height,$width,$depth], [ 'value' => $value, 'quantity' => $quantity ] ); } $this->owner->extend('updateShippingPackage', $package); return $package; } /** * Get shipping estimates. * * @return DataList */ public function getShippingEstimates() { $address = $this->owner->getShippingAddress(); $estimator = ShippingEstimator::create($this->owner, $address); $estimates = $estimator->getEstimates(); return $estimates; } /** * Set shipping method and shipping cost * * @param $option - shipping option to set, and calculate shipping from * @return boolean sucess/failure of setting */ public function setShippingMethod(ShippingMethod $option) { $package = $this->owner->createShippingPackage(); if (!$package) { throw new Exception(_t("OrderShippingExtension.NoPackage", "Shipping package information not available")); } $address = $this->owner->getShippingAddress(); if (!$address || !$address->exists() && $option->requiresAddress()) { throw new Exception(_t("OrderShippingExtension.NoAddress", "No address has been set")); } $this->owner->ShippingTotal = $option->calculateRate($package, $address); $this->owner->ShippingMethodID = $option->ID; $this->owner->write(); return true; } public function onSetBillingAddress($address) { if ($address) { Zone::cache_zone_ids($address); } return $this; } public function onSetShippingAddress($address) { if ($address) { Zone::cache_zone_ids($address); } return $this; } } |