max-commerce/shop-catalog

此包已被废弃且不再维护。未建议替代包。
关于此包最新版本(dev-master)没有可用的许可证信息。

Maxcommerce 产品目录模块

dev-master 2020-02-06 16:26 UTC

This package is auto-updated.

Last update: 2020-02-06 16:28:12 UTC


README

    "require": {
        "max-commerce/shop-catalog": "dev-master"
    }

基本配置

组件 Shop 的基本配置在 main.php

'components' => [
    ..
    'shop' => [
        'class' => 'maxcom\catalog\ShopComponent',
    ],
    ..
]

组件 Shop 的配置选项,包含额外事件(以下示例中在 Category 模型的 afterFind 事件中添加了 ImageByIdBehavior 行为)

'components' => [
    ..
    'shop' => [
        'class' => 'maxcom\catalog\ShopComponent',
        'on init' => function($event){
            Event::on('maxcom\catalog\models\Category', 'afterFind', function ($e) {
                $e->sender->attachBehavior('image', [
                    'class' => 'maxcom\catalog\components\ImageByIdBehavior',
                    'path' => 'images/category'
                ]);
            });
        }
    ],
    ..
]

商品类别

通过全局组件 Yii::$app->shop->categories 处理类别。

#1 输出顶级菜单类别

<ul>
  <?php foreach (Yii::$app->shop->categories->find()->roots()->limit(99)->all() as $category): ?>
    <li><a href="<?= $category->url ?>"><?= Html::encode($category->title) ?></a></li>
  <?php endforeach; ?>
</ul>

#2 输出双层菜单类别

<ul>
  <?php foreach (Yii::$app->shop->categories->find()->roots()->all() as $category): ?>
  <li<?php if ($category->hasChilds()): ?> class="expandable"<?php endif; ?>>
    <a href="<?= $category->url ?>"><?= Html::encode($category->title) ?></a>
    <?php if ($category->hasChilds()): ?>
    <ul>
      <?php foreach ($category->childs as $child_category): ?>
        <li><a href="<?= $child_category->url ?>"><?= Html::encode($child_category->title) ?></a></li>
      <?php endforeach; ?>
    </ul>
    <?php endif; ?>
  </li>
  <?php endforeach; ?>
</ul>

#3 输出带有图片的双层菜单类别(ImageByIdBehavior 行为)

<ul>
    <?php foreach (Yii::$app->shop->categories->find()->roots()->all() as $category): ?>
    <li<?php if ($category->hasChilds()): ?> class="expandable"<?php endif; ?>>
        <a href="<?= $category->url ?>">
            <?= $category->imageUrl ? "<img src=\"" . $category->imageUrl . "\" />" : '' ?><?= Html::encode($category->title) ?>
        </a>
        <?php if ($category->hasChilds()): ?>
        <ul>
            <?php foreach ($category->childs as $child_category): ?>
            <li><a href="<?= $child_category->url ?>"><?= Html::encode($child_category->title) ?></a></li>
            <?php endforeach; ?>
        </ul>
        <?php endif; ?>
    </li>
    <?php endforeach; ?>
</ul>