提交 b79b5098 编写于 作者: R root

产品的custom_option在前端页面的展示

上级 8edc1ed7
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\app\appfront\modules\Catalog\block\product;
use Yii;
use fec\helpers\CModule;
use fec\helpers\CRequest;
use yii\base\InvalidValueException;
use fecshop\app\appfront\modules\Catalog\helpers\Review as ReviewHelper;
/**
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class CustomOption {
public $custom_option;
public $attr_group;
protected $_custom_option_arr;
public function getLastData(){
$items = $this->getAllItems();
//var_dump($items);exit;
return [
'items' => $items ,
'custom_option_arr' => json_encode($this->_custom_option_arr),
];
}
public function getAllItems(){
$custom_option_attr_info = Yii::$service->product->getCustomOptionAttrInfo($this->attr_group);
##########
$my_arr = [];
$arr = [];
##custom_optionһ
$img_attr = '';
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){
$i = 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,
];
$t_arr['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][] = $t_arr;
}
}
}
}
}
}
return $arr;
}
}
......@@ -36,6 +36,7 @@ class Index {
'image' => $this->_product['image'],
'sku' => $this->_product['sku'],
'spu' => $this->_product['spu'],
'attr_group' => $this->_product['attr_group'],
'review_count' => $review_count,
'reviw_rate_star_average'=> $reviw_rate_star_average,
'price_info' => $this->getProductPriceInfo(),
......
......@@ -1803,9 +1803,7 @@ border: solid 1px #adadad;
.product_options a.noactive{
border: 1px dashed #ccc;
}
.product_options a.current{
border:1px solid #cc0000;
}
.product_options a.active{
border:1px solid #ccc;
......@@ -1874,8 +1872,7 @@ border: solid 1px #adadad;
.product_qty{margin:20px 0 0}
.pg .rg.chose_color{width:auto;}
.pg .rg.chose_size{width:auto;}
.product_options .pg .rg{width:auto;}
.product_options .label{line-height:62px;}
......@@ -2300,9 +2297,24 @@ a.next_images {
.product_options a.no_active{
border: 1px dashed #ccc;
cursor:not-allowed;
}
.product_options a.noimgshow{
font-size: 11px;
height: 35px;
line-height: 35px;
width: 35px;
}
.product_options a.active{
border: 1px solid #ccc;
}
.product_options a.current{
border:1px solid #cc0000;
}
......@@ -46,11 +46,12 @@
<div class="product_custom_options">
<?php # custom options部分
$optionsView = [
'view' => 'catalog/product/index/custom_option.php'
];
$optionsParam = [
'class' => 'fecshop\app\appfront\modules\Catalog\block\product\CustomOption',
'view' => 'catalog/product/index/custom_option.php',
'custom_option' => $custom_option,
'attr_group' => $attr_group,
];
?>
<?= Yii::$service->page->widget->render($optionsView,$optionsParam); ?>
......
<?php $custom_option = $parentThis['custom_option']; ?>
<?php if(is_array($custom_option) && !empty($custom_option)){ ?>
<?php $custom_option = \fec\helpers\CFunc::array_sort($custom_option,'sort_order','asc'); ?>
<?php foreach($custom_option as $one){ ?>
<?php $option_title = $one['title']; ?>
<?php $is_require = $one['is_require']; ?>
<?php $data = $one['data']; ?>
<?php if(is_array($data) && !empty($data)){ ?>
<div class="one_option">
<div class="one_option_title"><?= $option_title ?>:</div>
<select class="custom_option <?= $title ?> <?= $is_require ? 'required' : '' ?>" name="custom_option[<?= $title ?>]">
<?php $data = \fec\helpers\CFunc::array_sort($data,'sort_order','asc'); ?>
<?php foreach($data as $one){ ?>
<?php $title = $one['title']; ?>
<?php $price = $one['price']; ?>
<?php $val = Yii::$service->store->getStoreAttrVal($title,'title'); ?>
<option value="<?= $val ?>"><?= $val ?></option>
<?php } ?>
</select>
<div class="clear"></div>
</div>
<?php } ?>
<?php //var_dump($items);exit; ?>
<?php if(is_array($items) && !empty($items)){ ?>
<div class="product_options">
<?php foreach($items as $attr => $info){ ?>
<div class="pg">
<div class="label"><?= $attr ?>:</div>
<div class="chose_<?= $attr ?> rg <?= $attr ?>">
<ul class="no_chosen_ul" attr="<?= $attr ?>">
<?php if(is_array($info) && !empty($info)){ ?>
<?php foreach($info as $one){ ?>
<?php $val = $one['val']; ?>
<?php $key = $one['key']; ?>
<?php $image = $one['image']; ?>
<?php $require = $one['require']; ?>
<?php if($image){ ?>
<li>
<a attr="<?= $attr ?>" class="imgshow active" value="<?= $key ?>"><img src="<?= Yii::$service->product->image->getResize($image,[50,55],false) ?>" /></a>
<b></b>
</li>
<?php }else{ ?>
<li>
<a attr="<?= $attr ?>" class="noimgshow active" value="<?= $key ?>"><?= $val ?></a>
<b></b>
</li>
<?php } ?>
<?php } ?>
<?php } ?>
</ul>
</div>
<div class="clear"></div>
</div>
<?php } ?>
<?php } ?>
\ No newline at end of file
</div>
<?php } ?>
<script>
</script>
<script>
<?php $this->beginBlock('product_custom_option') ?>
$(document).ready(function(){
custom_option_arr = <?= $custom_option_arr ?>;
//for(x in custom_option_arr){
// alert(custom_option_arr[x]['sku']);
//}
$(".product_custom_options ul li a").click(function(){
if(!$(this).hasClass('no_active')){
$chosen_custom_option_arr = [];
$(this).parent().parent().find("a").removeClass("current");
$(this).parent().parent().find("li").removeClass("current");
$(this).addClass("current");
$(this).parent().addClass("current");
$(this).parent().parent().removeClass("no_chosen_ul");
$(this).parent().parent().addClass("chosen_ul");
$chosen_attr = [];
// 选择后的部分
$c_arr = [];
$c_chosen_custom_option_arr = new Object();;
$(".product_custom_options ul li a.current").each(function(){
attr = $(this).attr('attr');
val = $(this).attr('value');
for(x in custom_option_arr){
one = custom_option_arr[x];
i = 1;
$(".product_custom_options ul li a.current").each(function(){
attr2 = $(this).attr('attr');
val2 = $(this).attr('value');
//alert(attr+"###"+val);
if((attr != attr2) && (one[attr2] != val2)){
i = 0;
}
});
if(i){
if($c_chosen_custom_option_arr[attr] == undefined){
$c_chosen_custom_option_arr[attr] = [];
}
$c_chosen_custom_option_arr[attr].push(one);
}
}
});
// 每一个属性对应的允许的值,的出来,譬如 color 允许 red white等
c_my_arr = new Object();
for(attr in $c_chosen_custom_option_arr){
//alert(attr);
if(c_my_arr[attr] == undefined){
c_my_arr[attr] = new Object();;
}
arr = $c_chosen_custom_option_arr[attr];
for(x in arr){
one = arr[x];
for(y in one){
//alert(one[y]);
if(c_my_arr[attr][y] == undefined){
c_my_arr[attr][y] = [];
}
//alert(attr+"##"+y);
c_my_arr[attr][y].push(one[y]);
}
}
}
$(".product_custom_options ul.chosen_ul").each(function(){
attr = $(this).attr('attr');
$(this).find("li a").each(function(){
val = $(this).attr('value');
//alert(val);
//alert(my_arr[attr]);
if($.inArray(val, c_my_arr[attr][attr]) > -1){
$(this).removeClass('no_active');
$(this).addClass('active');
}else{
//alert(val);
//alert(222);
$(this).addClass('no_active');
$(this).removeClass('active');
}
});
});
///////
for(x in custom_option_arr){
one = custom_option_arr[x];
i = 1;
$(".product_custom_options ul li a.current").each(function(){
attr = $(this).attr('attr');
val = $(this).attr('value');
//alert(attr+"###"+val);
if(one[attr] != val){
i = 0;
}
});
if(i){
$chosen_custom_option_arr.push(one);
}
}
//alert(1);
my_arr = new Object();
for(x in $chosen_custom_option_arr){
one = $chosen_custom_option_arr[x];
for(y in one){
//alert(one[y]+"###"+y);
if(my_arr[y] == undefined){
my_arr[y] = [];
}
//alert(y+"__"+one[y]);
my_arr[y].push(one[y]);
}
}
$(".product_custom_options ul.no_chosen_ul").each(function(){
attr = $(this).attr('attr');
$(this).find("li a").each(function(){
val = $(this).attr('value');
//alert(val);
//alert(my_arr[attr]);
if($.inArray(val, my_arr[attr]) > -1){
$(this).removeClass('no_active');
$(this).addClass('active');
}else{
//alert(val);
//alert(222);
$(this).addClass('no_active');
$(this).removeClass('active');
}
});
});
}
});
});
<?php $this->endBlock(); ?>
</script>
<?php $this->registerJs($this->blocks['product_custom_option'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册