提交 4289b214 编写于 作者: R root

product page: relate product by color and size in product page

上级 0a62cc15
......@@ -27,9 +27,13 @@ class Index {
$productImgMagnifier = Yii::$app->controller->module->params['productImgMagnifier'];
$this->initProduct();
return [
'name' => Yii::$service->store->getStoreAttrVal($this->_product['name'],'name'),
'image' => $this->_product['image'],
'sku' => $this->_product['sku'],
'price_info' => $this->getProductPriceInfo(),
'tier_price' => $this->_product['tier_price'],
'media_size' => [
......@@ -38,10 +42,150 @@ class Index {
'middle_img_width' => $productImgSize['middle_img_width'],
],
'productImgMagnifier' => $productImgMagnifier,
'options' => $this->getSameSpuInfo(),
];
}
protected function getSpuData(){
$spu = $this->_product['spu'];
$filter = [
'select' => ['name','image','color','size','url_key'],
'where' => [
['spu' => $spu],
],
'asArray' => true,
];
$coll = Yii::$service->product->coll($filter);
return $coll['coll'];
}
protected function getColorAndSizeInfo($data,$current_size,$current_color){
$all_color = [];
$all_size = [];
$color_2_size = [];
$size_2_color = [];
foreach($data as $one){
$color = $one['color'];
$size = $one['size'];
//echo $color;
//echo $size;
//echo '##';
$image = $one['image'];
$name = $one['name'];
$url_key= $one['url_key'];
if($color || $size){
$all_color[$color] = [
'name' => $name,
'image' => $image,
'url_key' => $url_key,
];
$all_size[$size] = [
'name' => $name,
'image' => $image,
'url_key' => $url_key,
];
//echo $size.'#'.$current_size;
//echo '<br/>';
if($current_size == $size){
$color_2_size[$color] = [
'name' => $name,
'image' => $image,
'url_key' => $url_key,
];
}
if($current_color == $color){
$size_2_color[$size] = [
'name' => $name,
'image' => $image,
'url_key' => $url_key,
];
}
}
}
return [$all_color,$all_size,$color_2_size,$size_2_color ];
}
protected function getSameSpuInfo(){
//echo $this->_product['sku'];
//var_dump($this->_product);
$current_color = isset($this->_product['color']) ? $this->_product['color'] :'';
$current_size = isset($this->_product['size']) ? $this->_product['size'] : '';
if(!$current_size && !$current_color){
return ;
}
$data = $this->getSpuData();
list($all_color,$all_size,$color_2_size,$size_2_color) = $this->getColorAndSizeInfo($data,$current_size,$current_color);
//var_dump($color_2_size);
//var_dump($data);
$str = '';
$all_size = $this->sortSizeArr($all_size);
//var_dump($color_2_size);
//var_dump($all_color);
if(is_array($all_color) && !empty($all_color)){
$str .= '<div class="chose_color">';
foreach($all_color as $color => $info){
$main_img = isset($info['image']['main']['image']) ? $info['image']['main']['image'] : '';
$url = '';
$active = 'class="active"';
if(isset($color_2_size[$color])){
$url = Yii::$service->url->getUrl($color_2_size[$color]['url_key']);
}else{
$url = Yii::$service->url->getUrl($info['url_key']);
}
if($color == $current_color){
$active = 'class="current"';
}
$str .= '<a '.$active.' href="javascript:void(0)" rel="'.$url.'"><img src="'.Yii::$service->product->image->getResize($main_img,[50,55],false).'"/></a>';
}
$str .= '<div class="clear"></div>';
$str .= '</div>';
}
if(is_array($all_size) && !empty($all_size)){
$str .= '<div class="chose_size">';
foreach($all_size as $size => $info){
$url = '';
$active = 'class="noactive"';
if(isset($size_2_color[$size])){
$url = Yii::$service->url->getUrl($size_2_color[$size]['url_key']);
$active = 'class="active"';
}
if($size == $current_size){
$active = 'class="current"';
}
$str .= '<a '.$active.' href="javascript:void(0)" rel="'.$url.'"><span>'.$size.'</span></a>';
}
$str .= '<div class="clear"></div>';
$str .= '</div>';
}
//echo $str;exit;
return $str;
}
protected function sortSizeArr($data){
# 对size排序一下
$size = [];
$attr_group = $this->_product['attr_group'];
$attrInfo = Yii::$service->product->getGroupAttrInfo($attr_group);
$size_arr = isset($attrInfo['size']['display']['data']) ? $attrInfo['size']['display']['data'] : '';
$d_arr = [];
if(is_array($size_arr) && !empty($size_arr)){
foreach($size_arr as $size){
if(isset($data[$size])){
$d_arr[$size] = $data[$size];
}
}
return $d_arr;
}
return $data;
}
protected function getProductPriceInfo(){
$price = $this->_product['price'];
......@@ -73,6 +217,18 @@ class Index {
$this->_title = $this->_title ? $this->_title : $name;
Yii::$app->view->title = $this->_title;
//$this->_where = $this->initWhere();
# 通过上面查询的属性组,得到属性组对应的属性列表
# 然后重新查询产品
$attr_group = $this->_product['attr_group'];
$attrInfo = Yii::$service->product->getGroupAttrInfo($attr_group);
if(is_array($attrInfo) && !empty($attrInfo)){
$attrs = array_keys($attrInfo);
\fecshop\models\mongodb\Product::addCustomProductAttrs($attrs);
}
# 重新查询产品信息。
$product = Yii::$service->product->getByPrimaryKey($primaryVal);
$this->_product = $product ;
}
# 面包屑导航
......
......@@ -1790,8 +1790,25 @@ padding: 0 0 0 20px;
.tier_price_info table tr{}
.tier_price_info table tr td{height:25px;width:50px;}
.chose_size a {
border:1px solid #ccc;padding:2px 10px;margin:2px 4px;;
}
.chose_color{margin:2px 0 20px;}
.product_options{margin:40px 0 0;}
.product_options a {
display:block;float:left;margin:0 5px;
}
.product_options a.noactive{
border: 1px dashed #ccc;
}
.product_options a.current{
border:1px solid #cc0000;
}
.product_options a.active{
border:1px solid #ccc;
}
......@@ -18,6 +18,7 @@
</div>
<div class="product_info">
<h1><?= $name; ?></h1>
<div><?= $sku; ?></div>
<div class="price_info">
<?php # 价格部分
$priceView = [
......@@ -31,9 +32,28 @@
</div>
<div style="height:300px;">
<div class="product_options">
<?= $options; ?>
</div>
<script>
<?php $this->beginBlock('product_options') ?>
$(document).ready(function(){
$(".product_options a").click(function(){
$url = $(this).attr("rel");
if($url){
window.location.href=$url;
}else{
}
});
});
<?php $this->endBlock(); ?>
</script>
<?php $this->registerJs($this->blocks['product_options'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>
</div>
<div class="tier_price_info">
......
......@@ -76,7 +76,19 @@ class Product extends Service
return $this->_product->getByPrimaryKey($primaryKey);
}
/**
* 通过sku查询产品
*/
protected function actionGetBySku($sku){
return $this->_product->getBySku($sku);
}
/**
* 通过spu查询产品
*/
protected function actionGetBySpu($spu){
return $this->_product->getBySpu($spu);
}
/**
* @property $filter|Array
......
......@@ -30,6 +30,47 @@ class ProductMongodb implements ProductInterface
return new Product;
}
}
/**
* @property $sku|Array
* @property $returnArr|boolean 返回的数据是否是数组格式,如果设置为
* false,则返回的是对象数据
* @return Array or Object
* 通过sku 获取产品,一个产品
*/
public function getBySku($sku,$returnArr=true){
if($sku){
if($returnArr){
return Product::find()->asArray()
->where(['sku' => $sku])
->one()
;
}else{
return Product::findOne(['sku' => $sku]);
}
}
}
/**
* @property $spu|Array
* @property $returnArr|boolean 返回的数据是否是数组格式,如果设置为
* false,则返回的是对象数据
* @return Array or Object
* 通过spu 获取产品数组
*/
public function getBySpu($spu,$returnArr=true){
if($spu){
if($returnArr){
return Product::find()->asArray()
->where(['spu' => $spu])
->all();
}else{
return Product::find()
->where(['spu' => $spu])
->all();
}
}
}
/*
* example filter:
* [
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册