【问题标题】:How to remove shipping options from shopping cart based on which products are in the cart and what shipping options are linked to these products如何根据购物车中的产品以及与这些产品相关联的运输选项从购物车中删除运输选项
【发布时间】:2015-04-15 10:44:19
【问题描述】:

我有一个市场,卖家可以销售多种产品并分别指定运输选项,以多对多关系将运输选项链接到产品。

在购物车控制器中,我试图智能地删除运输选项,这样卖家就不会因为运输费用而支付的钱太少。

例如,考虑一个包含两种产品的购物车。卖家为每种产品选择了一种运输方式:

$products = array(
    array(
        'id' => 1,
        'name' => 'Lightweight widget',
        'shipping_option_ids' => array(
            1
        )
    ),
    array(
        'id' => 2,
        'name' => 'Heavyweight widget',
        'shipping_option_ids' => array(
            2
        )
    )
);

以下是两种运输方式:

$shipping_options = array(
    array(
        'id' => 1,
        'name' => 'Cheap shipping option',
        'price' => 100
    ),
    array(
        'id' => 2,
        'name' => 'Expensive shipping option',
        'price' => 200
    )
);

因此,我们有两种产品,每种产品都链接到不同的运输选项。使用昂贵的运输选项,两种产品可以在同一个包裹中运输。

现在,我需要从运输选项数组中删除廉价运输选项。这将使客户只有一种运输选项——昂贵的选项。

概括

条件

  • 购物车中的两个或多个产品至少没有一个共同的运输选项。

    动作

  • 删除所有运输选项,除了那些链接到具有最昂贵运输选项的产品的选项。

【问题讨论】:

  • 那么,您可以将多个配送选项与一种产品相关联吗?您对“最昂贵的运输方式”是什么意思?它是产品所有运输选项的价格总和吗?

标签: php arrays logic e-commerce


【解决方案1】:

我通过以下方式解决了这个问题:

// Prepare for removal procedure
foreach ($store['shipping_options'] as &$shipping_option)
{
    $shipping_option['removal_candidate'] = FALSE;
}
unset($shipping_option);

// Label shipping options that aren't linked to all products:
foreach ($products as $product)
{
    if (!in_array($shipping_option['id'], $product['shipping_option_ids']))
    {
        $shipping_option['removal_candidate'] = TRUE;
    }
}

$number_of_shipping_options = count($shipping_options);

// Loop through each shipping option:
for ($i = 0; $i < $number_of_shipping_options; $i++)
{
    $shipping_option_a = $shipping_options[$i];

    // Compare each shipping option with each of the other shipping options:
    foreach ($shipping_options as $key => $shipping_option_b)
    {
        // Compare different shipping options only:
        if ($shipping_option_a['id'] != $shipping_option_b['id'])
        {
            // Remove the shipping option with the lowest price:
            if ($shipping_option_a['price'] < $shipping_option_b['price'])
            {
                if ($shipping_option_a['removal_candidate'])
                {
                    unset($store['shipping_options'][$i]);
                    $shipping_options_removed = TRUE;
                    $number_of_shipping_options = count($store['shipping_options']);
                }
                elseif ($shipping_option_a['price'] > $shipping_option_b['price'])
                {
                    if ($shipping_option_b['removal_candidate'])
                    {
                        unset($store['shipping_options'][$key]);
                        $shipping_options_removed = TRUE;
                        $number_of_shipping_options = count($store['shipping_options']);
                    }
                }
            }
        }
    }
    // Refresh key numbers:
    $store['shipping_options'] = array_values($store['shipping_options']);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 2017-04-27
    相关资源
    最近更新 更多