Jump to content

Bug in Cart.php


Nico_presta

Recommended Posts

Hello

I am taking over a Prestashop site. The theme has been custom developed. I have the prices which are rounded off in the categories page, the home ... But when we arrive on the basket, it works. The ps_currency table is correct with decimal at 2, the settings look good to me. I believe it comes from the Cart.php file that I am posting to you here.

My Prestashop version : 1.7.7.1

<?php



require_once _PS_MODULE_DIR_ . 'spectrocommerce/models/SpectroPack.php';



use PrestaShop\PrestaShop\Adapter\AddressFactory;

use PrestaShop\PrestaShop\Adapter\Cache\CacheAdapter;

use PrestaShop\PrestaShop\Adapter\Customer\CustomerDataProvider;

use PrestaShop\PrestaShop\Adapter\Group\GroupDataProvider;

use PrestaShop\PrestaShop\Adapter\Product\PriceCalculator;

use PrestaShop\PrestaShop\Adapter\ServiceLocator;

use PrestaShop\PrestaShop\Adapter\Database;

use PrestaShop\PrestaShop\Core\Cart\Calculator;

use PrestaShop\PrestaShop\Core\Cart\CartRow;

use PrestaShop\PrestaShop\Core\Cart\CartRuleData;



class Cart extends CartCore

{

    public function getOrderTotal(

        $withTaxes = true,

        $type = Cart::BOTH,

        $products = null,

        $id_carrier = null,

        $use_cache = false,

        $keepOrderPrices = false



    ) {

        if ((int) $id_carrier <= 0) {

            $id_carrier = null;

        }



        // deprecated type

        if ($type == Cart::ONLY_PRODUCTS_WITHOUT_SHIPPING) {

            $type = Cart::ONLY_PRODUCTS;

        }



        // check type

        $type = (int)$type;

        $allowedTypes = array(

            Cart::ONLY_PRODUCTS,

            Cart::ONLY_DISCOUNTS,

            Cart::BOTH,

            Cart::BOTH_WITHOUT_SHIPPING,

            Cart::ONLY_SHIPPING,

            Cart::ONLY_WRAPPING,

            Cart::ONLY_PHYSICAL_PRODUCTS_WITHOUT_SHIPPING,

        );

        if (!in_array($type, $allowedTypes)) {

            throw new \Exception('Invalid calculation type: ' . $type);

        }



        // EARLY RETURNS



        // if cart rules are not used

        if ($type == Cart::ONLY_DISCOUNTS && !CartRule::isFeatureActive()) {

            return 0;

        }

        // no shipping cost if is a cart with only virtuals products

        $virtual = $this->isVirtualCart();

        if ($virtual && $type == Cart::ONLY_SHIPPING) {

            return 0;

        }

        if ($virtual && $type == Cart::BOTH) {

            $type = Cart::BOTH_WITHOUT_SHIPPING;

        }



        // filter products

        if (is_null($products)) {

            $products = $this->getProducts();

        }



        if ($type == Cart::ONLY_PHYSICAL_PRODUCTS_WITHOUT_SHIPPING) {

            foreach ($products as $key => $product) {

                if ($product['is_virtual']) {

                    unset($products[$key]);

                }

            }

            $type = Cart::ONLY_PRODUCTS;

        }



        if (Tax::excludeTaxeOption()) {

            $withTaxes = false;

        }



        $quantities = array();

        foreach ($products as $product) {

            // product cart quantities for pack

            if (isset($quantities[$product['id_product']])) {

                $quantities[$product['id_product']] += $product['cart_quantity'];

            } else {

                $quantities[$product['id_product']] = $product['cart_quantity'];

            }

        }



        // applicate cart rules if products exist in this cart

        $cart_rules_packs = SpectroPackModel::getCartRulesPacks();



        $CartRules_exists = array();

        $rules = SpectroPackModel::getCartRules($this->id);

        if(!empty($rules))

            $CartRules_exists = array_column($rules,'id_cart_rule');



        foreach($cart_rules_packs as $cart_rule_pack) {

            $products_pack = SpectroPackModel::getIdsProductByIdCartRuleId($cart_rule_pack['id_cart_rule']);

            $use_cart_rules = true;

            foreach ($products_pack as $test) {

                if (!isset($quantities[$test['id_item']]) || $quantities[$test['id_item']] < $test['minimum_quantity']) {

                    $use_cart_rules = false;

                    $this->removeCartRule($cart_rule_pack['id_cart_rule']);

                    break;

                }

            }

            if($use_cart_rules && !empty($products_pack) && !in_array($cart_rule_pack['id_cart_rule'],$CartRules_exists)){

                $this->addCartRule($cart_rule_pack['id_cart_rule']);

            }

        }



        // CART CALCULATION

        $cartRules = array();

        if (in_array($type, [Cart::BOTH, Cart::ONLY_DISCOUNTS])) {

            $cartRules = $this->getCartRules();

        }



        $calculator = $this->newCalculator($products, $cartRules, $id_carrier);

        $computePrecision = $this->configuration->get('_PS_PRICE_COMPUTE_PRECISION_');

        switch ($type) {

            case Cart::ONLY_SHIPPING:

                $calculator->calculateRows();

                $calculator->calculateFees($computePrecision);

                $amount = $calculator->getFees()->getInitialShippingFees();

                break;

            case Cart::ONLY_WRAPPING:

                $calculator->calculateRows();

                $calculator->calculateFees($computePrecision);

                $amount = $calculator->getFees()->getInitialWrappingFees();

                break;

            case Cart::BOTH:

                $calculator->processCalculation($computePrecision);

                $amount = $calculator->getTotal();

                break;

            case Cart::BOTH_WITHOUT_SHIPPING:

            case Cart::ONLY_PRODUCTS:

                $calculator->calculateRows();

                $amount = $calculator->getRowTotal();

                break;

            case Cart::ONLY_DISCOUNTS:

                $calculator->processCalculation($computePrecision);

                $amount = $calculator->getDiscountTotal();

                break;

            default:

                throw new \Exception('unknown cart calculation type : ' . $type);

        }



        // TAXES ?



        $value = $withTaxes ? $amount->getTaxIncluded() : $amount->getTaxExcluded();



        // ROUND AND RETURN



        $compute_precision = $this->configuration->get('_PS_PRICE_COMPUTE_PRECISION_');

        return Tools::ps_round($value, $compute_precision);

    }



    public  function newCalculator($products, $cartRules, $id_carrier)

    {

        $calculator = new Calculator($this, $id_carrier);



        /** @var PriceCalculator $priceCalculator */

        $priceCalculator = ServiceLocator::get(PriceCalculator::class);



        // set cart rows (products)

        $useEcotax = $this->configuration->get('PS_USE_ECOTAX');

        $precision = $this->configuration->get('_PS_PRICE_COMPUTE_PRECISION_');

        $configRoundType = $this->configuration->get('PS_ROUND_TYPE');

        $roundTypes = [

            Order::ROUND_TOTAL => CartRow::ROUND_MODE_TOTAL,

            Order::ROUND_LINE  => CartRow::ROUND_MODE_LINE,

            Order::ROUND_ITEM  => CartRow::ROUND_MODE_ITEM,

        ];

        if (isset($roundTypes[$configRoundType])) {

            $roundType = $roundTypes[$configRoundType];

        } else {

            $roundType = CartRow::ROUND_MODE_ITEM;

        }



        foreach ($products as $product) {

            $cartRow = new CartRow(

                $product,

                $priceCalculator,

                new AddressFactory,

                new CustomerDataProvider,

                new CacheAdapter,

                new GroupDataProvider,

                new Database,

                $useEcotax,

                $precision,

                $roundType

            );

            $calculator->addCartRow($cartRow);

        }



        // set cart rules

        foreach ($cartRules as $cartRule) {

            $calculator->addCartRule(new CartRuleData($cartRule));

        }



        return $calculator;

    }

}

Thanks !

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...