定制 setono/sylius-wishlist-plugin 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

setono/sylius-wishlist-plugin

Composer 安装命令:

composer require setono/sylius-wishlist-plugin

包简介

Setono example plugin for Sylius.

README 文档

README

Latest Version Software License Build Status Code Coverage Mutation testing

Add a wishlist (a.k.a. favourites / save for later) to your Sylius store. Visitors can save products whether or not they're logged in, the wishlist follows them from anonymous browsing into their account, and they can move everything to the cart in one click.

Features

  • 🛍️ Guest and customer wishlists – anonymous visitors get a wishlist tied to a cookie; logged-in customers get one tied to their account.
  • 🔄 Automatic hand-off on login – when a guest with a wishlist logs in, their items are moved onto their customer wishlist, so nothing is lost.
  • ❤️ Drop-in toggle button – a ready-made "heart" button that adds/removes a product (or a specific variant) over AJAX, plus a JS event you can hook into.
  • 🛒 Add the whole wishlist to the cart in a single request.
  • 📝 Per-item notes – shoppers can add a free-text note to each wishlist item (e.g. a size or colour preference), editable by the owner and visible to anyone they share the wishlist with.
  • 🔗 Shareable wishlists – every wishlist has a UUID, so it can be viewed via a stable, shareable URL.
  • 🧩 Extensible – all resources (models, repositories, factories) are overridable, and the "whose wishlist is this?" logic is a tagged service you can plug into.

Heads up: the data model already supports multiple wishlists per customer, but the UI/flow for choosing between them is still a work in progress (the select-wishlists endpoint currently returns Not implemented).

How it works

A few concepts are worth understanding before you integrate the plugin.

Wishlists and items

Wishlist is an abstract resource with two concrete subclasses:

Class Belongs to Identified by
GuestWishlist an anonymous visitor a clientId (cookie, see below)
UserWishlist a logged-in Sylius\…\User\Model\UserInterface the user

Each wishlist holds WishlistItems. An item references either a product or a specific product variant (plus a quantity and an optional free-text note), and every wishlist exposes a uuid (UUID v7) used in its public, shareable URL.

Who owns the current wishlist?

Everything in the plugin asks one service – WishlistProviderInterface – for "the current visitor's wishlists". It's a composite built from two tagged providers, consulted by priority, returning the first non-empty result:

  1. UserWishlistProvider – the wishlists of the logged-in customer (via Symfony Security).
  2. GuestWishlistProvider – the guest wishlist for the current cookie client id.

The result is memoized for the duration of the request by a caching decorator. To add another source (e.g. a shared/team wishlist), implement WishlistProviderInterface and tag it – you never touch the composite itself.

Guest identity

Anonymous visitors are identified through setono/client-bundle, which assigns a stable client id stored in a cookie. That id keys the guest wishlist. When the visitor authenticates, a login subscriber (ConvertGuestWishlistToUserWishlistSubscriber) converts the guest wishlist into a user wishlist.

Templates & the toggle flow

The plugin exposes two Twig functions so your templates can react to wishlist state:

{# is this product (or variant) on the current visitor's wishlist? #}
{{ setono_sylius_wishlist_on_wishlist(product) }}   {# returns a bool #}

{# does the current visitor have any non-empty wishlist? #}
{{ setono_sylius_wishlist_has_wishlist() }}          {# returns a bool #}

Adding/removing happens against small JSON endpoints. The bundled wishlist-action-handler.js listens for clicks on button.ssw-toggle, calls the endpoint, flips the button state, and dispatches a ssw:product-toggled event you can listen to (e.g. to update a header counter).

Requirements

Package Version
PHP ^8.1
Sylius ^1.13
Symfony ^6.4
Twig ^3.8

Installation

1. Require the package

composer require setono/sylius-wishlist-plugin

2. Register the plugin

Add the bundle to config/bundles.php before SyliusGridBundle, otherwise you'll get a You have requested a non-existent parameter "setono_sylius_wishlist.model.wishlist.class" exception:

<?php
# config/bundles.php
return [
    // ...
    Setono\SyliusWishlistPlugin\SetonoSyliusWishlistPlugin::class => ['all' => true],
    Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true],
    // ...
];

3. Import the routing

# config/routes/setono_sylius_wishlist.yaml
setono_sylius_wishlist:
    resource: "@SetonoSyliusWishlistPlugin/Resources/config/routes.yaml"

If your shop doesn't use locales in the URL, import routes_no_locale.yaml instead:

# config/routes/setono_sylius_wishlist.yaml
setono_sylius_wishlist:
    resource: "@SetonoSyliusWishlistPlugin/Resources/config/routes_no_locale.yaml"

4. Update your database

The plugin ships Doctrine mappings; generate and run a migration for your app:

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

5. Install the assets

The toggle button's stylesheet and script are injected into the shop layout automatically (via sylius_ui events), but the JS file still needs to be published:

php bin/console assets:install

Usage

Add a wishlist button to your product page

Include the bundled partial wherever you render a product (e.g. the product box or the product show page), passing the product and – optionally – the selected productVariant:

{% include '@SetonoSyliusWishlistPlugin/shop/wishlist/_toggle_button.html.twig' with {
    'product': product,
    'productVariant': variant ?? null,
} %}

It renders a heart button.ssw-toggle whose data-url points at the correct add/remove route and whose state reflects setono_sylius_wishlist_on_wishlist(...). No extra wiring is needed – the CSS/JS are already on the page.

React to toggles in JavaScript

Every toggle dispatches a bubbling ssw:product-toggled event:

document.addEventListener('ssw:product-toggled', (e) => {
    // e.detail.event === 'added' | 'removed'
    // e.detail.wishlistItemsCount === total items across the visitor's wishlists
    document.querySelector('#wishlist-counter').textContent = e.detail.wishlistItemsCount;
});

You can override the handler's defaults by setting window.sswWishlist before the script loads (e.g. to change the selector.toggle or provide your own callback.onToggle).

Link to the wishlist

The wishlist index and a single (shareable) wishlist are available at these routes:

Route name Method Path
setono_sylius_wishlist_shop_wishlist_index GET /wishlists
setono_sylius_wishlist_shop_wishlist_show GET, PATCH /wishlists/{uuid}
setono_sylius_wishlist_shop_wishlist_add_to_cart GET /wishlists/{uuid}/add-to-cart
setono_sylius_wishlist_shop_wishlist_item_remove GET /wishlists/{uuid}/remove-item/{id}
setono_sylius_wishlist_shop_wishlist_add_product GET /wishlist/add-product/{id}
setono_sylius_wishlist_shop_wishlist_remove_product GET /wishlist/remove-product/{id}

(The *_product_variant variants of the add/remove routes exist too.) Because a wishlist is addressed by its uuid, the _show URL can be shared with anyone.

Customization

Override a resource

Models, repositories and factories follow the standard Sylius Resource configuration. To swap in your own model:

# config/packages/setono_sylius_wishlist.yaml
setono_sylius_wishlist:
    resources:
        wishlist:
            classes:
                model: App\Entity\Wishlist\Wishlist
        # guest_wishlist, user_wishlist and wishlist_item are configurable too

Provide your own wishlists

Implement Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface and tag it; the composite provider picks it up automatically (higher priority is consulted first):

# config/services.yaml
services:
    App\Wishlist\Provider\SharedWishlistProvider:
        tags:
            - { name: 'setono_sylius_wishlist.wishlist_provider', priority: 0 }

Edit authorization

Edit actions are guarded by WishlistVoter (the wishlist_edit attribute), which grants access only when the subject wishlist belongs to the current visitor. Override the voter if you need different rules.

Development

The repository contains a full test Sylius application under tests/Application used for integration testing.

composer install
composer analyse        # PHPStan (level max)
composer check-style    # Easy Coding Standard
composer phpunit        # unit tests

# commands that need the booted app:
(cd tests/Application && bin/console lint:container)

License

This plugin is released under the MIT License.

setono/sylius-wishlist-plugin 适用场景与选型建议

setono/sylius-wishlist-plugin 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.93k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 10 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「sylius」 「sylius-plugin」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 setono/sylius-wishlist-plugin 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 setono/sylius-wishlist-plugin 我们能提供哪些服务?
定制开发 / 二次开发

基于 setono/sylius-wishlist-plugin 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5.93k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-08