setono/sylius-geo-plugin

Setono为Sylius的示例插件。

安装数: 1,744

依赖项: 0

建议者: 0

安全: 0

星级: 0

关注者: 1

分支: 0

开放问题: 3

类型:sylius-plugin

v1.0.0-alpha.7 2023-09-01 07:34 UTC

README

Latest Version Software License Build Status Code Coverage

此插件为您的商店添加地理相关功能。目前它包含以下两个功能

  • 根据访客所在国家进行重定向
  • 使用Twig函数输出hreflang链接标签,以帮助搜索引擎发现商店的区域版本

安装

步骤 1:下载插件

composer require setono/sylius-geo-plugin

步骤 2:启用插件

然后,通过将其添加到项目中config/bundles.php文件中注册的插件/捆绑包列表,在SyliusGridBundle之前(!)启用插件

<?php
$bundles = [
    Setono\SyliusGeoPlugin\SetonoSyliusGeoPlugin::class => ['all' => true],
    Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true],
];

步骤 3:配置插件

# config/packages/setono_sylius_geo.yaml
imports:
    - { resource: "@SetonoSyliusGeoPlugin/Resources/config/app/config.yaml" }

步骤 4:导入路由

# config/routes/setono_sylius_geo.yaml
setono_sylius_geo:
    resource: "@SetonoSyliusGeoPlugin/Resources/config/routes.yaml"

步骤 5:更新数据库模式

php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate

步骤 6:使用现有的国家代码提供者或创建一个新的提供者

该插件使用国家代码提供者来提供国家代码。插件自带一个基于请求头部的单个国家代码提供者。如果您想创建自己的提供者,只需实现CountryCodeProviderInterface即可。该服务将自动标记为setono_sylius_geo.country_code_provider

注意:如果您使用Cloudflare,只需在仪表板中转到规则 > 转换规则 > 管理转换,并启用添加访客位置头部即可。

用法

根据访客所在国家进行重定向

访问 https://your-store.com/admin/geo/rules/new 创建新规则。以下是一个示例

Rule example

如您所见,我们的目标是重定向丹麦访客到我们的丹麦地区商店。我们排除了爬虫和我们的办公IP,这样爬虫可以自由爬取我们的国际商店,我们也能在丹麦之外浏览国际商店,而不会重定向。

输出hreflang链接标签

将此Twig代码:{{ ssg_hreflang_tags() }} 添加到您的<head>部分,它将输出<link rel="alternate" hreflang="..." href="...">标签。