在维护产品的时候颜色属性存在双色的,如white & blue,在保存的时候会把第一个空格替换成号,如下图
但是在html5前端获取产品数据的时候,并没有做号的处理,导致多色的产品属性出不来,代码如下:
public function getAllItems()
{
$custom_option_attr_info = Yii::$service->product->getCustomOptionAttrInfo($this->attr_group);
//#########
$my_arr = [];
$arr = [];
//#在custom_option里面第一个属性
$img_attr = '';
if (is_array($custom_option_attr_info) && !empty($custom_option_attr_info)) {
foreach ($custom_option_attr_info as $attr => $info) {
if (isset($info['showAsImg']) && $info['showAsImg']) {
$img_attr = $attr;
break;
}
}
}
$img_arr = [];
if (is_array($this->custom_option) && (!empty($this->custom_option))) {
foreach ($this->custom_option as $option) {
$qty = $option['qty'];
if ($qty > 0) {
$this->_custom_option_arr[] = $option;
if (isset($option[$img_attr])) {
$val = $option[$img_attr];
$img_arr[$val] = $option['image'];
}
foreach ($option as $k=>$v) {
$my_arr[$k][] = $v;
}
}
}
}
if (is_array($custom_option_attr_info) && !empty($custom_option_attr_info)) {
foreach ($custom_option_attr_info as $attr => $info) {
if (isset($info['display']['type']) && ($info['display']['type'] == 'select')) {
if (isset($info['display']['data']) && is_array($info['display']['data'])) {
foreach ($info['display']['data'] as $key=>$val) {
if (is_array($my_arr[$attr]) && in_array($key, $my_arr[$attr])) {
$t_arr = [
'key' => $key,
'val' => $val,
];
$require = isset($info['require']) ? $info['require'] : 0;
if (isset($info['showAsImg']) && $info['showAsImg']) {
if (isset($img_arr[$key])) {
$t_arr['image'] = $img_arr[$key];
}
}
$arr[$attr]['info'][] = $t_arr;
$arr[$attr]['require'] = $require;
}
}
}
}
}
}
return $arr;
}
在以上的代码中,并没有加入*号的判断导致多色的产品属性出不来。
is_array($my_arr[$attr]) && in_array($key, $my_arr[$attr]