Version: current

     1 <?php
     2 /**
     3  * Доставка
     4  * Gives methods to ship by, based on previously given address and order items.
     5  *
     6  * @author menedem
     7  */
     8 class CheckoutStep_ShippingMethod extends CheckoutStep{
     9
    10     static $allowed_actions = array(
    11         'shippingmethod',
    12         'ShippingMethodForm'
    13     );
    14
    15     function StepIsActive() {
    16         $order = Cart::current_order();
    17         $methods = $order->getShippingEstimates();
    18         if (!$methods || ($methods->Count() == 0)) {
    19             return false;
    20         }
    21         if ($methods->Count() == 1) {
    22             return false;
    23         }
    24         return true;
    25     }
    26
    27     function shippingmethod(){
    28         $order = Cart::current_order();
    29         $methods = $order->getShippingEstimates();
    30         if (!$methods || ($methods->Count() == 0)) {
    31             user_error("Необходимо создать хотя бы 1 способ доставки", E_USER_ERROR);
    32         }
    33         if ($methods->Count() == 1) {
    34             $method = $methods->First();
    35             return $this->setShippingMethod(array(
    36                 'ShippingMethodID' => $method->ID,
    37                 'ShippingRate_' . $method->ID => $method->CalculatedRate,
    38             ), null);
    39         }
    40         //?? Может ли быть уже заданным ShippingMethodID (в sshop есть проверка на это)
    41         return array(
    42             'Title' => $this->stepTitle(),
    43             'Content' => $this->stepMessage(),
    44             'Form' => $this->ShippingMethodForm()
    45         );
    46     }
    47
    48     function ShippingMethodForm(){
    49         $order = Cart::current_order();
    50         $methods = $order->getShippingEstimates();
    51         $fields = new FieldSet();
    52         if ($methods->exists()) {
    53             $fields->push(new OptionsetField("ShippingMethodID", "", $methods->map('ID', 'ListTitle'), $methods->First()->ID));
    54             foreach ($methods as $method) {
    55                 $fields->push(new HiddenField('ShippingRate_' . $method->ID, 'ShippingRate_' . $method->ID, $method->CalculatedRate));
    56             }
    57         } else {
    58             $fields->push(new LiteralField("NoShippingMethods", sprintf('<p class="message warning">%s</p>', _t('ShippingMethodForm.NoShippingMethods', 'There are no shipping methods available'))));
    59         }
    60         $form = new Form(
    61             $this->owner,
    62             'ShippingMethodForm',
    63             $fields,
    64             $this->getStepActions(new FormAction('setshippingmethod', _t('OrderForm.NextStepButton', 'Continue')))
    65         );
    66         $form->addExtraClass('checkoutOptions');
    67
    68         $this->owner->extend('updateShippingMethodForm', $form);
    69         return $form;
    70     }
    71
    72     function setshippingmethod($data, $form) {
    73         $shippingMethodID = (int) $data['ShippingMethodID'];
    74         Cart::set_info('ShippingMethodID', $shippingMethodID);
    75         // !!! по-хорошему надо заново вычислить стоимость - доверять стоимости из интернета нельзя
    76         Cart::set_info('ShippingTotal', (float) $data['ShippingRate_'.$shippingMethodID]);
    77         Director::redirect($this->NextStepLink());
    78     }
    79 }
    80

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -