根据购物车产品对可用的承运商进行排序



我需要为我的prestshop (v1.6.1.0)实现一个简单的功能:

基于购物车内容(产品引用),我需要在第4个"shipping"页面/选项卡上对用户可用的承运人进行排序。

举个例子:我有3个载体,但只有当其中一个购物车的产品参考以1开头时(这意味着它是新鲜食品,不能通过该载体运输),才能删除其中一个主题。

我该怎么做呢?

Carrier类重写getAvailableCarrierList函数

编辑最后一个循环:

if ($product->width > 0 || $product->height > 0 || $product->depth > 0 || $product->weight > 0)
{
    foreach ($carrier_list as $key => $id_carrier)
    {
        $carrier = new Carrier($id_carrier);
        // Get the sizes of the carrier and the product and sort them to check if the carrier can take the product.
        $carrier_sizes = array((int)$carrier->max_width, (int)$carrier->max_height, (int)$carrier->max_depth);
        $product_sizes = array((int)$product->width, (int)$product->height, (int)$product->depth);
        rsort($carrier_sizes, SORT_NUMERIC);
        rsort($product_sizes, SORT_NUMERIC);
        if (($carrier_sizes[0] > 0 && $carrier_sizes[0] < $product_sizes[0])
            || ($carrier_sizes[1] > 0 && $carrier_sizes[1] < $product_sizes[1])
            || ($carrier_sizes[2] > 0 && $carrier_sizes[2] < $product_sizes[2]))
        {
            $error[$carrier->id] = Carrier::SHIPPING_SIZE_EXCEPTION;
            unset($carrier_list[$key]);
        }
        if ($carrier->max_weight > 0 && $carrier->max_weight < $product->weight * $cart_quantity)
        {
            $error[$carrier->id] = Carrier::SHIPPING_WEIGHT_EXCEPTION;
            unset($carrier_list[$key]);
        }

        //Here goes the magic
        foreach ($cart->getProducts(false, $product->id) as $cart_product) {
            $aProduct = new Product($cart_product['id_product']);
            if (substr($aProduct->reference, 0, 1) == 1) {
                unset($carrier_list[$key]);
            }
        }

    }
}
return $carrier_list;

更好,不需要重写整个函数:

public static function getAvailableCarrierList(Product $product, $id_warehouse, $id_address_delivery = null, $id_shop = null, $cart = null, &$error = array())
{
    $carrier_list = parent::getAvailableCarrierList($product, $id_warehouse, $id_address_delivery, $id_shop, $cart, &$error);
    if (is_null($cart))
        $cart = Context::getContext()->cart;
    if ($product->width > 0 || $product->height > 0 || $product->depth > 0 || $product->weight > 0)
    {
        foreach ($carrier_list as $key => $id_carrier)
        {
            //Here goes the magic
            foreach ($cart->getProducts(false, $product->id) as $cart_product) {
                $aProduct = new Product($cart_product['id_product']);
                if (substr($aProduct->reference, 0, 1) == 1) {
                    unset($carrier_list[$key]);
                }
            }
        }
    }
    return $carrier_list;
}

测试。

最新更新