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

时间:2016-09-20 13:10:55

标签: php prestashop prestashop-1.6

我需要为我的Prestashop(v1.6.1.0)实现一个简单的函数:

根据购物车内容(产品参考资料),我需要对第4天发货的运营商进行分类"运输"页面/标签

作为一个例子:我有3个运营商,但只有当其中一个购物车的产品参考以1开头时,必须删除其中一个主题(这意味着它的新鲜食物可以&#39 ;通过这个承运人发货。)

我该怎么做?

1 个答案:

答案 0 :(得分:0)

覆盖getAvailableCarrierList类中的Carrier函数。

编辑最终循环:

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;
}

未测试。