Custom Shipping Logic in PrestaShop 1.7 with Marketplace Module "JA MarketPlace"

29 Views Asked by At

I'm currently facing an issue with a custom shipping logic implementation in a PrestaShop 1.7 module called "JA MarketPlace". In the Cart.php file, I've successfully calculated delivery fees based on the number of sellers in the cart. However, during the checkout process, only a flat rate of 24.99$ is being charged, and the logic from the Cart.php file seems to be ignored.

The requirement is to charge 24.99$ per seller in the cart, except when the cart total is above 500$ with a single seller, in which case the delivery should be free.

Here's a snippet of the relevant code in Cart.php:

class Cart extends CartCore
{
    private function getNumberOfUniqueSellers($product_list)
    {
        if (count($product_list)     $uniqueSellers = [];

        foreach ($product_list as $product) {
            $currentProductSeller = $this->getSellerByProduct($product);

            if (!in_array($currentProductSeller, $uniqueSellers)) {
                $uniqueSellers[] = $currentProductSeller;
            }
        }

        return count($uniqueSellers);
    }

    private function areProductsFromSameSeller($product_list)
    {
        if (count($product_list) <= 1) {
            return true;
        }

        $firstProductSeller = $this->getSellerByProduct($product_list[0]);

        for ($i = 1; $i < count($product_list); $i++) {
            $currentProductSeller = $this->getSellerByProduct(
                $product_list[$i]
            );

            if ($firstProductSeller !== $currentProductSeller) {
                return false;
            }
        }

        return true;
    }

    private function getSellerByProduct($id_product)
    {
        $sql =
            "SELECT sp.id_seller FROM " .
            _DB_PREFIX_ .
            "seller_product sp
        WHERE sp.id_product = " .
            (int)$id_product;

        $id_seller = Db::getInstance()->getValue($sql);

        return $id_seller;
    }

    private function getSellerProductIds($id_seller)
    {
        $sql =
            "SELECT sp.id_product FROM " .
            _DB_PREFIX_ .
            "seller_product sp
        WHERE sp.id_seller = " .
            (int)$id_seller;

        $product_ids = Db::getInstance()->executeS($sql);

        return array_column($product_ids, "id_product");
    }

    public function getTotalShippingCost(
        $delivery_option = null,
        $use_tax = true,
        Country $default_country = null
    )
    {
        if (isset(Context::getContext()->cookie->id_country)) {
            $default_country = new Country(
                Context::getContext()->cookie->id_country
            );
        }
        if (null === $delivery_option) {
            $delivery_option = $this->getDeliveryOption(
                $default_country,
                false,
                false
            );
        }

        $_total_shipping = [
            "with_tax" => 0,
            "without_tax" => 0,
        ];
        $delivery_option_list = $this->getDeliveryOptionList($default_country);
        foreach ($delivery_option as $id_address => $key) {
            if (
                !isset($delivery_option_list[$id_address]) ||
                !isset($delivery_option_list[$id_address][$key])
            ) {
                continue;
            }

            $_total_shipping["with_tax"] +=
                $delivery_option_list[$id_address][$key]["total_price_with_tax"];
            $_total_shipping["without_tax"] +=
                $delivery_option_list[$id_address][$key]["total_price_without_tax"];
        }

        $products = $this->getProducts();
        $product_list = [];
        $product_sellers = [];
        $product_prices = [];

        foreach ($products as $product) {
            $product_id = (int)$product["id_product"];
            $seller_id = $this->getSellerByProduct($product_id);
            $product_sellers[$product_id] = $seller_id;
            $product_list[] = $product_id;

            $product_prices[$seller_id][] = (float)$product["price"];
        }

        $numberOfSellers = $this->getNumberOfUniqueSellers($product_list);

        $sumOfPrices = [];

        foreach ($product_prices as $seller_id => $seller_prices) {
            $sumOfPrices[$seller_id] = array_sum($seller_prices);
        }

        $productsPerSeller = [];

        foreach ($sumOfPrices as $seller_id => $sum) {
            if ($sum >= 500) {
                $productsPerSeller += $product_prices[$seller_id];
            }
        }

        $numberOfProducts = count($product_list) - count($productsPerSeller);

        return $use_tax
            ? $_total_shipping["with_tax"] * $numberOfProducts
            : $_total_shipping["without_tax"] * $numberOfProducts; // Example: Double the shipping cost for different sellers
    }

}

I've noticed that the calculation works correctly in the cart, but when the order is placed, the PrestaShop default shipping logic takes over.

enter image description here

I'm using PrestaShop 1.7 with the "JA MarketPlace" module. Could someone please guide me on how to ensure that my custom shipping logic is applied during the checkout process? Any help or insights would be greatly appreciated.

0

There are 0 best solutions below