如何在 Prestashop 中限制每笔交易的凭证绑定

How do I limit voucher's binding per transaction in Prestashop

prestashop 在购物车定价规则中的一个特点是能够在一次交易中绑定不同的凭证。默认单笔交易可绑定20张凭证

问题是:如何将每笔交易的代金券限制为10张代金券?

我是否需要在后端自定义代码,或者在 prestahop 的管理面板中有一个选项?

希望您立即回复。

谢谢,

您必须以这种方式覆盖 ParentOrderController class:

<?php
class ParentOrderController extends ParentOrderControllerCore {
    public function init()
    {
        $this->isLogged = (bool)($this->context->customer->id && Customer::customerIdExistsStatic((int)$this->context->cookie->id_customer));

        FrontController::init();

        /* Disable some cache related bugs on the cart/order */
        header('Cache-Control: no-cache, must-revalidate');
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');

        $this->nbProducts = $this->context->cart->nbProducts();

        if (!$this->context->customer->isLogged(true) && $this->useMobileTheme() && Tools::getValue('step'))
            Tools::redirect($this->context->link->getPageLink('authentication', true, (int)$this->context->language->id));

        // Redirect to the good order process
        if (Configuration::get('PS_ORDER_PROCESS_TYPE') == 0 && Dispatcher::getInstance()->getController() != 'order')
            Tools::redirect('index.php?controller=order');

        if (Configuration::get('PS_ORDER_PROCESS_TYPE') == 1 && Dispatcher::getInstance()->getController() != 'orderopc')
        {
            if (Tools::getIsset('step') && Tools::getValue('step') == 3)
                Tools::redirect('index.php?controller=order-opc&isPaymentStep=true');
            Tools::redirect('index.php?controller=order-opc');
        }

        if (Configuration::get('PS_CATALOG_MODE'))
            $this->errors[] = Tools::displayError('This store has not accepted your new order.');

        if (Tools::isSubmit('submitReorder') && $id_order = (int)Tools::getValue('id_order'))
        {
            $oldCart = new Cart(Order::getCartIdStatic($id_order, $this->context->customer->id));
            $duplication = $oldCart->duplicate();
            if (!$duplication || !Validate::isLoadedObject($duplication['cart']))
                $this->errors[] = Tools::displayError('Sorry. We cannot renew your order.');
            else if (!$duplication['success'])
                $this->errors[] = Tools::displayError('Some items are no longer available, and we are unable to renew your order.');
            else
            {
                $this->context->cookie->id_cart = $duplication['cart']->id;
                $this->context->cookie->write();
                if (Configuration::get('PS_ORDER_PROCESS_TYPE') == 1)
                    Tools::redirect('index.php?controller=order-opc');
                Tools::redirect('index.php?controller=order');
            }
        }

        if ($this->nbProducts)
        {
            if (CartRule::isFeatureActive())
            {
                if (Tools::isSubmit('submitAddDiscount'))
                {
                    $cart_rules = $this->context->cart->getCartRules();

                    if (!($code = trim(Tools::getValue('discount_name'))))
                        $this->errors[] = Tools::displayError('You must enter a voucher code.');
                    elseif (!Validate::isCleanHtml($code))
                        $this->errors[] = Tools::displayError('The voucher code is invalid.');
                    elseif(count($cart_rules)>=10)
                        $this->errors[] = Tools::displayError('You have reached the maximum number of coupons that can be used');
                    else
                    {
                        if (($cartRule = new CartRule(CartRule::getIdByCode($code))) && Validate::isLoadedObject($cartRule))
                        {
                            if ($error = $cartRule->checkValidity($this->context, false, true))
                                $this->errors[] = $error;
                            else
                            {
                                $this->context->cart->addCartRule($cartRule->id);
                                if (Configuration::get('PS_ORDER_PROCESS_TYPE') == 1)
                                    Tools::redirect('index.php?controller=order-opc&addingCartRule=1');
                                Tools::redirect('index.php?controller=order&addingCartRule=1');
                            }
                        }
                        else
                            $this->errors[] = Tools::displayError('This voucher does not exists.');
                    }
                    $this->context->smarty->assign(array(
                        'errors' => $this->errors,
                        'discount_name' => Tools::safeOutput($code)
                    ));
                }
                elseif (($id_cart_rule = (int)Tools::getValue('deleteDiscount')) && Validate::isUnsignedId($id_cart_rule))
                {
                    $this->context->cart->removeCartRule($id_cart_rule);
                    Tools::redirect('index.php?controller=order-opc');
                }
            }
            /* Is there only virtual product in cart */
            if ($isVirtualCart = $this->context->cart->isVirtualCart())
                $this->setNoCarrier();
        }

        $this->context->smarty->assign('back', Tools::safeOutput(Tools::getValue('back')));
    }
}

这就是所有的init()方法,我已经全部粘贴以避免错误或错误。

差异:

  • 第 7 行 - 调用 FrontController::init() 而不是 parent::init()
  • 第 56 行 - 获取存储的当前购物车规则
  • 第 62-63 行 - 根据您的要求,检查购物车规则的数量

注意如果你升级你的ps版本,记住你已经覆盖了整个方法。

尽情享受 ;)