Source of file CheckoutStepShippingMethod.php
Size: 3,033 Bytes - Last Modified: 2021-12-23T10:25:09+00:00
/var/www/docs.ssmods.com/process/src/src/Checkout/Step/CheckoutStepShippingMethod.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 | <?php namespace SilverShop\Shipping\Checkout\Step; use SilverShop\Checkout\Step\CheckoutStep; use SilverShop\Cart\ShoppingCart; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\OptionsetField; use SilverStripe\Forms\LiteralField; use SilverStripe\Forms\FormAction; use SilverStripe\Forms\Form; use SilverShop\Shipping\Model\ShippingMethod; /** * Gives methods to ship by, based on previously given address and order items. */ class CheckoutStepShippingMethod extends CheckoutStep { private static $allowed_actions = [ 'shippingmethod', 'ShippingMethodForm' ]; public function shippingmethod() { $form = $this->ShippingMethodForm(); $cart = ShoppingCart::singleton()->current(); if ($cart->ShippingMethodID) { $form->loadDataFrom($cart); } return [ 'OrderForm' => $form ]; } /** * @return Form */ public function ShippingMethodForm() { $order = $this->owner->Cart(); if (!$order) { return null; } $estimates = $order->getShippingEstimates(); $fields = new FieldList(); if ($estimates->exists()) { // if there is only one option then automatically select the option if ($estimates->count() === 1) { $order->setShippingMethod($estimates->First()); } $fields->push( OptionsetField::create( "ShippingMethodID", _t('CheckoutStep_ShippingMethod.ShippingOptions', 'Shipping Options'), $estimates->map('ID', 'getTitle'), $estimates->First()->ID ) ); } else { $fields->push( LiteralField::create( "NoShippingMethods", _t('CheckoutStep_ShippingMethod.NoShippingMethods', '<p class=\"message warning\">There are no shipping methods available</p>' ) ) ); } $actions = new FieldList( new FormAction("setShippingMethod", _t('SilverShop\Checkout\Step\CheckoutStep.Continue', 'Continue')) ); $form = new Form($this->owner, "ShippingMethodForm", $fields, $actions); $this->owner->extend('updateShippingMethodForm', $form); return $form; } public function setShippingMethod($data, $form) { $order = $this->owner->Cart(); $option = null; if (isset($data['ShippingMethodID'])) { $option = ShippingMethod::get()->byID($data['ShippingMethodID']); if ($option) { $order->setShippingMethod($option); } } $this->owner->extend('onSetShippingMethod', $order, $data, $form); // perform write to store changes $order->calculate(); $order->write(); return $this->owner->redirect($this->NextStepLink()); } } |