Prestashop 1.7在最终订单步骤检查可用库存

Prestashop 1.7在最终订单步骤检查可用库存,prestashop,cart,prestashop-1.7,Prestashop,Cart,Prestashop 1.7,我在Prestashop 1.6上做了一些工作,在客户可以购买之前检查购物车的数量。以下是我在Prestashop 1.7中遇到的问题: 如果客户今天将商品放入购物车,他将在2天后返回,并且仍然登录。购物车仍然可用,而产品实际上已经脱销。 客户可以下订单,我的库存数量为-1。由于我升级到Prestashop1.7,这是一场灾难,我有数量在-5,-10…因为如果这个未检查的场景 abstract class PaymentModule extends PaymentModuleCore {

我在Prestashop 1.6上做了一些工作,在客户可以购买之前检查购物车的数量。以下是我在Prestashop 1.7中遇到的问题: 如果客户今天将商品放入购物车,他将在2天后返回,并且仍然登录。购物车仍然可用,而产品实际上已经脱销。 客户可以下订单,我的库存数量为-1。由于我升级到Prestashop1.7,这是一场灾难,我有数量在-5,-10…因为如果这个未检查的场景

abstract class PaymentModule extends PaymentModuleCore
{
    public function validateOrder($id_cart, $id_order_state, $amount_paid, $payment_method = 'Unknown',
        $message = null, $extra_vars = array(), $currency_special = null, $dont_touch_amount = false,
        $secure_key = false, Shop $shop = null)
    {

        if (!isset($this->context))
            $this->context = Context::getContext();
        $this->context->cart = new Cart($id_cart);

        if (!$this->context->cart->checkQuantities()){
            Tools::redirect(__PS_BASE_URI__.'order.php?step=0');
        }
        return parent::validateOrder($id_cart, $id_order_state, $amount_paid, $payment_method, $message, 
            $extra_vars, $currency_special, $dont_touch_amount, $secure_key, $shop);
    }
}

实际上,最好的解决方案是使用以下插件:

Prestashop处理非常糟糕的购物车库存

无论如何,如果你想自己做,并检查可用的库存,它非常简单:

<?php

    $cart = $this->context->cart;
    $cart_products = $cart->getProducts();

    if (!empty($cart_products)) {

        $db = Db::getInstance();

        foreach ($cart_products as $key => $cart_product) {

            $real_quantity = StockAvailable::getQuantityAvailableByProduct($cart_product['id_product'], $cart_product['id_product_attribute']);

            if ( (int) $real_quantity < (int) $cart_product['quantity'] ) {

                // If negative
                $real_quantity = (int) $real_quantity < 0 ? 0 : $real_quantity;

                $sql = '
                        UPDATE `'._DB_PREFIX_.'cart_product`
                        SET quantity = '.(int) $real_quantity.',`date_add` = NOW()
                        WHERE `id_product` = '.(int) $cart_product['id_product'].
                        (!empty($cart_product['id_product_attribute']) ? ' AND `id_product_attribute` = '.(int) $cart_product['id_product_attribute'] : '').'
                        AND `id_cart` = '.(int) $cart->id;

                    $db->execute($sql);
            }

        }

        // Garbage collector
        $db->execute('DELETE FROM '._DB_PREFIX_.'cart_product WHERE quantity < 1 ');

    }

对于1.7,结账后的数量是错误的吗?不,我面临的问题是在购物车中添加东西和结账之间购物车没有被检查。因此,当商品仍然不可用时,可以订购2天前的挂起购物车。然后你的商品库存中的金额低于零。谢谢Matt Loye的代码,但是,你能告诉我在你点击“结帐”后,我必须把代码放在购物车的什么地方进行检查吗?我的意思是,当你看到购物车的汇总时,你必须用正确的钩子创建一个模块(请参见此处的生成器:,你需要先创建一个帐户)。您的签出过程肯定会有displayAfterShoppingCartBlock,您可以在其中执行此代码。太好了,非常感谢。还有一件事,实际上它将客户重定向到webiste索引。请告诉我,在代码中,我可以在哪里添加重定向到首选位置?我将添加为设置选项创建的CMS页面,以选择一个CMS页面,告诉客户刚才发生了什么。请打开另一张票据,并将此票据标记为已解决,以便在此处保持秩序;)这是另一个主题。注意:对于流量大的网站,不建议使用该插件。性能将急剧下降。插件以非常积极的方式使用数据库。。。