如何根据购物车中的产品以及链接到这些产品的运输选项从购物车中删除运输选项
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
我有一个市场,卖家可以在其中销售多种产品并分别指定运输选项,以多对多关系将运输选项链接到产品。
在购物车控制器中,我正在尝试智能地删除送货选项,这样卖家就不会因为运费而剩下的钱太少。
例如,考虑一个包含两种产品的购物车。卖家为每件产品选择了一种运输方式:
$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
)
);
所以,我们有两种产品,每种都链接到不同的运输选项。使用昂贵的运输选项,两种产品可以在同一个包裹中运输。
现在,我需要从送货选项数组中删除便宜的送货选项。这将使客户只能选择一种运输方式——昂贵的运输方式。
泛化
条件
购物车中的两种或多种产品没有至少一种共同的送货方式。
动作
删除所有运输选项,但链接到具有最昂贵运输选项的产品的除外。
我通过以下方式解决了这个问题:
// 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']);
}
我有一个市场,卖家可以在其中销售多种产品并分别指定运输选项,以多对多关系将运输选项链接到产品。
在购物车控制器中,我正在尝试智能地删除送货选项,这样卖家就不会因为运费而剩下的钱太少。
例如,考虑一个包含两种产品的购物车。卖家为每件产品选择了一种运输方式:
$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
)
);
所以,我们有两种产品,每种都链接到不同的运输选项。使用昂贵的运输选项,两种产品可以在同一个包裹中运输。
现在,我需要从送货选项数组中删除便宜的送货选项。这将使客户只能选择一种运输方式——昂贵的运输方式。
泛化
条件
购物车中的两种或多种产品没有至少一种共同的送货方式。
动作
删除所有运输选项,但链接到具有最昂贵运输选项的产品的除外。
我通过以下方式解决了这个问题:
// 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']);
}