| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- /**
- * Created by PhpStorm.
- * Name: 芸众商城系统
- * Author: 广州市芸众信息科技有限公司
- * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
- * Date: 2021/9/14
- * Time: 17:38
- */
- namespace app\backend\modules\goods\widget;
- //商品规格
- use app\backend\modules\goods\models\GoodsOption;
- use app\backend\modules\goods\models\GoodsSpec;
- class OptionWidget extends BaseGoodsWidget
- {
- public $group = 'base';
- public $widget_key = 'option';
- public $code = 'option';
- public function pluginFileName()
- {
- return 'goods';
- }
- public function getData()
- {
- $specs = [];
- $option = [];
- if (!is_null($this->goods)) {
- $goodsSpecs = GoodsSpec::select('id','title','goods_id')->where('goods_id', $this->goods->id)
- ->with(['hasManySpecsItem' => function($item) {
- return $item->select('id','specid','title','show')->orderBy('display_order', 'asc');
- }])->orderBy('display_order', 'asc')->get();
- $spec_title_key = [];
- $spec_item_title_arr = [];
- if (!$goodsSpecs->isEmpty()) {
- foreach ($goodsSpecs as $spec) {
- $temporary = $spec->getAttributes();
- $temporary['spec_item'] = $spec->hasManySpecsItem->toArray();
- $specs[] = $temporary;
- $spec_item_title_arr = $spec_item_title_arr + array_column($temporary['spec_item'],'title','id');
- $spec_title_key[$temporary['title']] = array_column($temporary['spec_item'],'id');
- }
- }
- $option = GoodsOption::where('goods_id', $this->goods->id)->orderBy('display_order', 'asc')->get()->toArray();
- foreach ($option as $key=>$item) {
- if ($item['thumb']) {
- $option[$key]['thumb'] = yz_tomedia($item['thumb']);
- }
- //这里那id做判断,名称可能会重复
- $spec_item_ids = explode('_',$item['specs']);
- foreach ($spec_item_ids as $title_key => $spec_item_id) {
- $spec_title_array = array_filter($spec_title_key, function ($title_key) use ($spec_item_id) {
- return in_array($spec_item_id, $title_key);
- });
- $title_key_first = array_key_first($spec_title_array);
- if ($title_key_first) {
- $option[$key][$title_key_first] = $spec_item_title_arr[$spec_item_id];
- }
- }
- }
- }
- return [
- 'has_option'=> is_null($this->goods)?0:$this->goods->has_option,
- 'specs'=> $specs,
- 'option'=>$option
- ];
- }
- public function pagePath()
- {
- return $this->getPath('resources/views/goods/assets/js/components/');
- }
- }
|