禁止在周日打开购物车3退房

问题描述

我有一家用Open Cart 3设置的商店。我想在星期日禁用结帐。我已经尝试了以下代码,但似乎无法正常工作。

我将代码添加到了以下文件目录/controller/checkout/checkput.PHP

class ControllerCheckoutCheckout extends Controller {
    public function index() {
        // Validate cart has products and has stock.
        if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasstock() && !$this->config->get('config_stock_checkout'))) {
            $this->response->redirect($this->url->link('checkout/cart'));
        }
        
        if (date("l") == "Sunday") {
            $this->response->redirect($this->url->link('checkout/cart'));
            $this->session->data['error'] = 'Sorry we are closed on Sundays';
        }

这是我添加行之前的完整代码

<?PHP

class ControllerCheckoutCheckout extends Controller {
    public function index() {
        // Validate cart has products and has stock.
        if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasstock() && !$this->config->get('config_stock_checkout'))) {
            $this->response->redirect($this->url->link('checkout/cart'));
        }
        
        
        // Validate minimum quantity requirements.
        $products = $this->cart->getProducts();

        foreach ($products as $product) {
            $product_total = 0;

            foreach ($products as $product_2) {
                if ($product_2['product_id'] == $product['product_id']) {
                    $product_total += $product_2['quantity'];
                }
            }

            if ($product['minimum'] > $product_total) {
                $this->response->redirect($this->url->link('checkout/cart'));
            }
        }

        $this->load->language('checkout/checkout');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->document->addScript('catalog/view/javascript/jquery/datetimepicker/moment/moment.min.js');
        $this->document->addScript('catalog/view/javascript/jquery/datetimepicker/moment/moment-with-locales.min.js');
        $this->document->addScript('catalog/view/javascript/jquery/datetimepicker/bootstrap-datetimepicker.min.js');
        $this->document->addStyle('catalog/view/javascript/jquery/datetimepicker/bootstrap-datetimepicker.min.css');

        // required by klarna
        if ($this->config->get('payment_klarna_account') || $this->config->get('payment_klarna_invoice')) {
            $this->document->addScript('http://cdn.klarna.com/public/kitt/toc/v1.0/js/klarna.terms.min.js');
        }

        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),'href' => $this->url->link('common/home')
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_cart'),'href' => $this->url->link('checkout/cart')
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),'href' => $this->url->link('checkout/checkout','',true)
        );

        $data['text_checkout_option'] = sprintf($this->language->get('text_checkout_option'),1);
        $data['text_checkout_account'] = sprintf($this->language->get('text_checkout_account'),2);
        $data['text_checkout_payment_address'] = sprintf($this->language->get('text_checkout_payment_address'),2);
        $data['text_checkout_shipping_address'] = sprintf($this->language->get('text_checkout_shipping_address'),3);
        $data['text_checkout_shipping_method'] = sprintf($this->language->get('text_checkout_shipping_method'),4);
        
        if ($this->cart->hasShipping()) {
            $data['text_checkout_payment_method'] = sprintf($this->language->get('text_checkout_payment_method'),5);
            $data['text_checkout_confirm'] = sprintf($this->language->get('text_checkout_confirm'),6);
        } else {
            $data['text_checkout_payment_method'] = sprintf($this->language->get('text_checkout_payment_method'),3);
            $data['text_checkout_confirm'] = sprintf($this->language->get('text_checkout_confirm'),4); 
        }
        
        if (isset($this->session->data['error'])) {
            $data['error_warning'] = $this->session->data['error'];
            unset($this->session->data['error']);
        } else {
            $data['error_warning'] = '';
        }

        $data['logged'] = $this->customer->isLogged();

        if (isset($this->session->data['account'])) {
            $data['account'] = $this->session->data['account'];
        } else {
            $data['account'] = '';
        }

        $data['shipping_required'] = $this->cart->hasShipping();

        $data['column_left'] = $this->load->controller('common/column_left');
        $data['column_right'] = $this->load->controller('common/column_right');
        $data['content_top'] = $this->load->controller('common/content_top');
        $data['content_bottom'] = $this->load->controller('common/content_bottom');
        $data['footer'] = $this->load->controller('common/footer');
        $data['header'] = $this->load->controller('common/header');

        $this->response->setoutput($this->load->view('checkout/checkout',$data));
    }

    public function country() {
        $json = array();

        $this->load->model('localisation/country');

        $country_info = $this->model_localisation_country->getCountry($this->request->get['country_id']);

        if ($country_info) {
            $this->load->model('localisation/zone');

            $json = array(
                'country_id'        => $country_info['country_id'],'name'              => $country_info['name'],'iso_code_2'        => $country_info['iso_code_2'],'iso_code_3'        => $country_info['iso_code_3'],'address_format'    => $country_info['address_format'],'postcode_required' => $country_info['postcode_required'],'zone'              => $this->model_localisation_zone->getZonesByCountryId($this->request->get['country_id']),'status'            => $country_info['status']
            );
        }

        $this->response->addHeader('Content-Type: application/json');
        $this->response->setoutput(json_encode($json));
    }

    public function customfield() {
        $json = array();

        $this->load->model('account/custom_field');

        // Customer Group
        if (isset($this->request->get['customer_group_id']) && is_array($this->config->get('config_customer_group_display')) && in_array($this->request->get['customer_group_id'],$this->config->get('config_customer_group_display'))) {
            $customer_group_id = $this->request->get['customer_group_id'];
        } else {
            $customer_group_id = $this->config->get('config_customer_group_id');
        }

        $custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id);

        foreach ($custom_fields as $custom_field) {
            $json[] = array(
                'custom_field_id' => $custom_field['custom_field_id'],'required'        => $custom_field['required']
            );
        }

        $this->response->addHeader('Content-Type: application/json');
        $this->response->setoutput(json_encode($json));
    }
  }

任何人都无法知道为什么这种方法不起作用或以其他方式在周日禁用结帐吗?

解决方法

如果在重定向之前将此行放在下面,则会显示错误消息。

$this->session->data['error'] = 'Sorry we are closed on Sundays';

这是更改后的代码

    class ControllerCheckoutCheckout extends Controller {
        public function index() {
            // Validate cart has products and has stock.
            if ((!$this->cart->hasProducts() && empty($this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
            $this->response->redirect($this->url->link('checkout/cart'));
            }
    
            if (date("l") == "Sunday") {
                $this->session->data['error'] = 'Sorry we are closed on Sundays';
                $this->response->redirect($this->url->link('checkout/cart'));
            }

result