andrewdanilov/yii2-shop

Yii2 Shop

资助包维护!
Patreon

安装: 98

依赖者: 0

建议者: 0

安全: 0

星标: 2

关注者: 2

分支: 0

开放问题: 0

类型:yii2-extension

1.1.2 2021-12-25 11:20 UTC

README

可定制的商店模块,具有层次分类、产品属性和选项、购物车、产品标签。支持国际化。

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一

composer require andrewdanilov/yii2-shop "~1.1.0"

"andrewdanilov/yii2-shop": "~1.1.0"

将以下内容添加到您的 composer.json 文件的 require 部分。

然后运行数据库迁移,创建所需的表

php yii migrate --migrationPath=@andrewdanilov/shop/console/migrations

更新扩展后,也请运行迁移。

用法

在后台主配置文件 modules 部分,添加以下内容

$config = [
    // ...
    'modules' => [
        // ...
        'shop' => [
            'class' => 'andrewdanilov\shop\backend\Module',
            // access role for module controllers, optional, default is ['@']
            'access' => ['admin'],
            // path to user translates, optional, default is '@andrewdanilov/shop/common/messages'
            'translatesPath' => '@common/messages/shop',
            // file manager configuration, optional, default is:
            'fileManager' => [
                'basePath' => '@frontend/web',
                'paths' => [
                    [
                        'name' => 'Product images',
                        'path' => 'upload/images/product',
                    ],
                    [
                        'name' => 'Category images',
                        'path' => 'upload/images/category',
                    ],
                    [
                        'name' => 'Brand images',
                        'path' => 'upload/images/brand',
                    ],
                    [
                        'name' => 'Sticker images',
                        'path' => 'upload/images/sticker',
                    ],
                    [
                        'name' => 'Documents',
                        'path' => 'upload/images/docs',
                    ],
                ],
            ],
        ],
    ],
];

在这里,access 选项允许限制对定义角色的访问。

在前端主配置文件 modules 部分和 bootstrap 部分,添加 shop 模块

$config = [
    // ...
    'modules' => [
        // ...
        'shop' => [
            'class' => 'andrewdanilov\shop\frontend\Module',
            // path to template views, optional, default is '@andrewdanilov/shop/frontend/views'
            'templatesPath' => '@frontend/views/shop',
            // path to mail template views, optional, default is '@andrewdanilov/shop/common/mail'
            'mailTemplatesPath' => '@common/mail/shop',
            // path to user translates, optional, default is '@andrewdanilov/shop/common/messages'
            'translatesPath' => '@common/messages/shop',
            // main currency, using by shop, optional, default is 'USD'
            'currency' => '$',
        ],
    ],
    // If you use own templates paths, you need to add `shop` module to `bootstrap` section
    // to enable i18n and other settings before using module parts.
    // This is optional, but recommended in any way.
    'bootstrap' => [
        // ...
        'shop',
    ],
];

common/config/params.php 配置文件中添加/修改 adminEmailsenderEmailsenderName 参数,如下所示

return [
    // ...
    'adminEmail' => ['admin@example.com', 'admin2@example.com'],
    'senderEmail' => 'noreply@example.com',
    'senderName' => 'Robot',
    // ...
];

您将收到系统消息(例如,网站订单)来自 senderEmail 电子邮件的 adminEmail 电子邮件之一。

要传输电子邮件消息,您必须在 common/config/main-local.php 中正确设置 mailer 组件。例如 SMTP 传输

return [
    'components' => [
        // ...
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'useFileTransport' => false,
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'username' => 'sender@example.com',
                'password' => 'example_password',
                'host' => 'smtp.example.com',
                'port' => '465',
                'encryption' => 'ssl',
            ],
        ],
    ],
];

后台菜单项

$shop_menu_items = [
    ['label' => 'Orders', 'url' => ['/shop/order'], 'icon' => 'shopping-cart'],
    ['label' => 'Products', 'url' => ['/shop/product'], 'icon' => 'shopping-bag'],
    ['label' => 'Brands', 'url' => ['/shop/brand'], 'icon' => 'leaf'],
    ['label' => 'Options', 'url' => ['/shop/option'], 'icon' => 'check-square'],
    ['label' => 'Properties', 'url' => ['/shop/property'], 'icon' => 'list'],
    ['label' => 'Property groups', 'url' => ['/shop/group'], 'icon' => 'tags'],
    ['label' => 'Linking', 'url' => ['/shop/relation'], 'icon' => 'share-alt'],
    ['label' => 'Pay methods', 'url' => ['/shop/pay'], 'icon' => 'credit-card'],
    ['label' => 'Shipping methods', 'url' => ['/shop/delivery'], 'icon' => 'truck'],
    ['label' => 'Stickers', 'url' => ['/shop/sticker'], 'icon' => 'bookmark'],
];

echo \yii\widgets\Menu::widget(['items' => $shop_menu_items]);

小部件

您可以使用这些小部件将购物车、迷你购物车、模态窗口和购买按钮添加到您的商店

// Place this widget everywhere you want to see buy button
echo \andrewdanilov\shop\frontend\widgets\Buttons\Buy::widget([
    'product_id' => 123,
    // optional, text label displaying on buy button
    'label' => 'Buy it!',
    // optional, html tag uset for representing buy button
    'tag' => 'div',
    // optional, extended classes for buy button
    'classes' => 'orange-buy-button',
    // optional, use to define own template for buy button
    'template' => '@frontend/views/shop/buy-button',
]);

// Place these two widgets on checkout page.
// First is for displaying form to retrieve client information like e-mail, phone, etc.
// Second is for displaying cart contents table.
\andrewdanilov\shop\frontend\widgets\Checkout\Client::widget();
\andrewdanilov\shop\frontend\widgets\Checkout\FullCart::widget();

// Widget for mini-cart button. Place it somewhere in main layout.
\andrewdanilov\shop\frontend\widgets\Checkout\MiniCart::widget();

// Widget for placing modal windows on page
\andrewdanilov\shop\frontend\widgets\Forms\Modals::widget();

如果您需要在小部件内部使用自己的内容,您可以复制小部件目录到您希望的位置,然后从那里调用它们。如果这样做,请记住相应地更改命名空间。

功能

I18n

扩展支持国际化。您可以在 common/config/main.php 中设置您的语言。

return [
    // ...
    'language' => 'ru-RU',
    // ...
];

目前您可以使用以下语言之一:英语、俄语。此外,您可以通过定义商店模块的 translatesPath 属性来创建和使用自己的翻译(见上文)。因此,您需要将语言文件放置在 translatesPath 路径内的 xx-XX 文件夹中。您可以从扩展的 src/common/messages 路径中复制示例。