如何在 Prestashop 中将付款费用添加到购物车?

How to add payment fee to cart in Prestashop?

我想将支付费用添加到购物车中,并使用为支付费用创建的产品。但是,当我将收费的产品添加到购物车时,我使用 $cart->getOrderTotal(),它不会显示收费的实际价格。它显示订单总额为产品价格,不含费用。

我为此操作使用 SpecificPrice class。我认为后台的 prestashop 订单页面首先读取具有特定价格的产品。但是不显示付款价格,而是和我添加的商品价格一起显示。

也许你不这么理解,但我在下面的评论中解释得更好。请帮我。我已经挣扎了2天了。 :(

$specific_price = new SpecificPrice();
        $specific_price->id_product = 50; // choosen product id
        $specific_price->id_customer = (int) $cart->id_customer;
        $specific_price->id_product_attribute = 0;
        $specific_price->id_cart = (int)$cart->id;
        $specific_price->id_shop = (int) $cart->id_shop;
        $specific_price->id_currency = (int) $cart->id_currency;
        $specific_price->id_country = Context::getContext()->country->id;
        $specific_price->id_group = 0;
        $specific_price->from_quantity = 1;
        $specific_price->price = $installment_fee; //installment fee is here. (example: 1.13)
        $specific_price->reduction_type = 'amount';
        $specific_price->reduction_tax = 1;
        $specific_price->reduction = 0;
        $specific_price->from = date("Y-m-d H:i:s");
        $dateUntil = new DateTime(); $dateUntil->modify("+2 days");
        $specific_price->to = $dateUntil->format('Y-m-d H:i:s'); // or set date x days from now
        try{
            $specific_price->add(1,1);
        } catch (\Exception $exception)
        {
            echo $exception->getMessage(); die;
        }

        //cart product price = 30
        //installment fee product = 10 (i want to edit it with installment fee (ex: 1.13))
        $cart->updateQty(1,50); 
        //the real product is here. Price: 10, but i want to use it with specific price created above.So product price + specific price.
        //but it shows product price(30) + product price(10)
        //but i want product price(30) + product price(1.13) 
        
        echo '<pre>';
        print_r($cart->getOrderTotal()); //it shows 40 but i want 31.13
        exit;

我的问题解决了。问题是在将产品添加到购物车后清除卡片缓存。

如果遇到此问题,请在validateOrder函数前添加如下代码。

Cart::resetStaticCache();