计算基于选项组和期权产品变型 [英] calculate product variants based on option groups and options
问题描述
我在写一个电子商务网站,并需要计算变型产品的好方法。该网站有产品,产品可以有很多选项组,选项组可以有很多选择。
因此,一个T恤产品有3个选项组和选项:
尺寸: 小, 中, 大,
颜色: 红, 蓝, 黄色, 黑色,
材料: 棉, 尼龙,
这将创建:红色小棉,红色小尼龙,小蓝棉布,小蓝尼龙,......等等等等
我知道下面作品的脚本,而且它可以被优化。任何人都可以提供一个更好的工作的这个例子吗?它应该可以使用递归,以及...但我打一个心理障碍。
如果(计数($ option_groups)→1)
{
//开始变了
的foreach($ option_groups [0] - > get_options(),为$选项)
{
$变种[] =阵列($选择);
}
//通过所有其他选项组中,使组合
为($ X = 1; $ X<计数($ option_groups); $ X ++)
{
$组合=阵列();
的foreach($变种为$变种)
{
$ =新阵列();
的foreach($ option_groups [$ X] - > get_options()为$选项)
{
$ TMP = $变种;
$ TMP [] = $选项;
$新的[] = $ TMP;
}
$连击[] = $新;
}
$变种=阵列();
的foreach($组合为$组合)
{
的foreach($组合为$ TMP)
{
$变种[] = $ TMP;
}
}
}
}
这是不是超时间敏感的,但我想有code一个更易于维护的大块,这是pretty的毛。
也做这个问题(我觉得这不是一个原始的问题,很多车做到这一点)有名字吗?我不拉任何东西在谷歌这个问题。
修改 这是我结束了,它的基础断profitphp的解决方案,但仍保持了给我的每个变体串联为一个字符串的选择我的对象,而不是。以Profitphp!感谢所有
私有函数_possible_combos($团体,$preFIX =阵列())
{
$结果=阵列();
$组= array_shift($组);
的foreach($组 - > get_options()为$选择)
{
如果($群)
{
$ TMP = $preFIX;
$ TMP [] = $选择;
$结果= array_merge($结果,$这个 - > _possible_combos($团体,$ TMP));
}
其他
{
$ TMP = $preFIX;
$ TMP [] = $选择;
$结果[] = $ TMP;
}
}
返回$结果;
}
这应该做的伎俩:
&LT ;?
$数据[] =阵列('衬衫');
$数据[] =阵列(红,黄,黑);
$数据[] =阵列(小,中,大);
$组合= possible_combos($的数据);
//计算从给定的选择阵列中的所有可能的comobos可创建
功能possible_combos($团体,$preFIX =''){
$结果=阵列();
$组= array_shift($组);
的foreach($组为$选择){
如果($组){
$结果= array_merge($结果,possible_combos($团体,$preFIX $选择''));
} 其他 {
$结果[] = $preFIX。 $选择;
}
}
返回$结果;
}
回声计数($组合)。 \ N的;
的print_r($连击);
测试: http://www.ideone.com/NZE5S
I am writing an ecommerce site, and need a good way to calculate product variations. The Site has products, products can have many option groups, option groups can have many options.
So a Tshirt Product has 3 option groups and options:
Size: Small, Medium, Large,
Color: Red, Blue, Yellow, Black,
Material: Cotton, Nylon,
which creates: small red cotton, small red nylon, small blue cotton, small blue nylon, ... so on and so forth
I know that the script below works, but also that it can be optimized. Can anyone furnish a better working example of this? It should be possible using recursion as well... but I'm hitting a mental block.
if(count($option_groups) > 1)
{
// start the variants up
foreach($option_groups[0]->get_options() as $option)
{
$variants[] = array($option);
}
// go through every other option group to make combos
for($x = 1; $x < count($option_groups); $x++)
{
$combos = array();
foreach($variants as $variant)
{
$new = array();
foreach($option_groups[$x]->get_options() as $option)
{
$tmp = $variant;
$tmp[] = $option;
$new[] = $tmp;
}
$combos[] = $new;
}
$variants = array();
foreach($combos as $combo)
{
foreach($combo as $tmp)
{
$variants[] = $tmp;
}
}
}
}
This isn't super time sensitive, but I'd like to have a more maintainable chunk of code, this is pretty gross.
Also does this problem (I feel like it isn't an original problem, many carts do this) have a name? I wasn't pulling anything up on google for this problem.
EDIT This is what I ended up with, its based off of profitphp's solution, but maintains my objects instead of giving me the options per variant concatenated as a string. All thanks to Profitphp!
private function _possible_combos($groups, $prefix = array())
{
$result = array();
$group = array_shift($groups);
foreach($group->get_options() as $selected)
{
if($groups)
{
$tmp = $prefix;
$tmp[] = $selected;
$result = array_merge($result, $this->_possible_combos($groups, $tmp));
}
else
{
$tmp = $prefix;
$tmp[] = $selected;
$result[] = $tmp;
}
}
return $result;
}
This should do the trick:
<?
$data[]=array('shirt');
$data[]=array('red','yellow','black');
$data[]=array('small','medium','large');
$combos=possible_combos($data);
//calculate all the possible comobos creatable from a given choices array
function possible_combos($groups, $prefix='') {
$result = array();
$group = array_shift($groups);
foreach($group as $selected) {
if($groups) {
$result = array_merge($result, possible_combos($groups, $prefix . $selected. ' '));
} else {
$result[] = $prefix . $selected;
}
}
return $result;
}
echo count($combos) . "\n";
print_r($combos);
Tested: http://www.ideone.com/NZE5S
这篇关于计算基于选项组和期权产品变型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!