From 4289b2144b94389dca7fdbbd73ba22ef8209bb36 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 20 Oct 2016 19:21:43 +0800 Subject: [PATCH] product page: relate product by color and size in product page --- .../modules/Catalog/block/product/Index.php | 156 ++++++++++++++++++ .../theme/base/front/assets/css/style.css | 17 ++ .../base/front/catalog/product/index.php | 24 ++- services/Product.php | 12 ++ services/product/ProductMongodb.php | 41 +++++ 5 files changed, 248 insertions(+), 2 deletions(-) diff --git a/app/appfront/modules/Catalog/block/product/Index.php b/app/appfront/modules/Catalog/block/product/Index.php index 09eb3f48..0fd3daad 100644 --- a/app/appfront/modules/Catalog/block/product/Index.php +++ b/app/appfront/modules/Catalog/block/product/Index.php @@ -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 '
'; + 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 .= '
'; + 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 .= ''; + } + $str .= '
'; + $str .= '
'; + } + + if(is_array($all_size) && !empty($all_size)){ + $str .= '
'; + 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 .= ''.$size.''; + } + $str .= '
'; + $str .= '
'; + } + //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 ; } # 面包屑导航 diff --git a/app/appfront/theme/base/front/assets/css/style.css b/app/appfront/theme/base/front/assets/css/style.css index 07777436..502f24f2 100644 --- a/app/appfront/theme/base/front/assets/css/style.css +++ b/app/appfront/theme/base/front/assets/css/style.css @@ -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; +} diff --git a/app/appfront/theme/base/front/catalog/product/index.php b/app/appfront/theme/base/front/catalog/product/index.php index faab7fc8..b2f3f436 100644 --- a/app/appfront/theme/base/front/catalog/product/index.php +++ b/app/appfront/theme/base/front/catalog/product/index.php @@ -18,6 +18,7 @@

+
+
+ +
- - + + registerJs($this->blocks['product_options'],\yii\web\View::POS_END);//灏嗙紪鍐欑殑js浠g爜娉ㄥ唽鍒伴〉闈㈠簳閮 ?> +
diff --git a/services/Product.php b/services/Product.php index 9fa706c6..b73c3279 100644 --- a/services/Product.php +++ b/services/Product.php @@ -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 diff --git a/services/product/ProductMongodb.php b/services/product/ProductMongodb.php index 97dfb8f9..847cb79f 100644 --- a/services/product/ProductMongodb.php +++ b/services/product/ProductMongodb.php @@ -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: * [ -- GitLab