bitbag/shipping-export-plugin

Sylius平台应用的数据导出环境

安装: 101 388

依赖项: 16

建议者: 0

安全: 0

星级: 29

关注者: 9

分支: 29

开放问题: 1

类型:sylius-plugin


README

BitBag SyliusShippingExportPlugin

Slack Support

我们希望影响许多独特的电子商务项目并在全球范围内建立我们的品牌知名度,所以我们大量参与创建开源解决方案,特别是Sylius。我们已经创建了超过35个扩展,这些扩展已被下载近200万次。

您可以在我们的网站上找到更多有关我们的电子商务服务和技术的信息:https://bitbag.io/。我们还创建了一个专门用于创建插件的服务:https://bitbag.io/services/sylius-plugin-development

您喜欢我们的工作吗?您想加入我们吗?请查看“职业”选项卡:https://bitbag.io/pl/kariera

关于我们

BitBag是一家软件公司,提供定制电子商务平台,包括从创建电子商务平台到实施PIM和CMS系统,再到开发定制电子商务应用程序,专业B2B解决方案以及从其他平台迁移。

我们积极参与Sylius的开发。我们已经完成了超过150个项目,与来自世界各地的客户合作,包括小型企业和大型国际公司。我们已经为诸如Mytheresa、Foodspring、Planeta Huerto(家乐福集团)、Albeco、Mollie和ArtNight等重要品牌完成了项目。

我们有一个由70名专家组成的团队:业务分析师和电子商务顾问、开发者、项目经理和QA测试员。

我们的服务

  • B2B和B2C电子商务平台实施
  • 多供应商市场平台实施
  • 电子商务迁移
  • Sylius插件开发
  • Sylius咨询
  • 项目维护和长期支持
  • PIM和CMS实施

关于BitBag和Sylius的一些数字

  • 70位专家
  • +150个基于Sylius的项目交付
  • BitBag客户来自30个国家
  • 在Sylius生态系统中7年
  • +35个为Sylius创建的插件

目录

概述

在任何电子商务应用程序中管理运输可能有些棘手。有众多运输提供商,每个都有其自己的API格式,您可能想使用它来导出运输数据和请求取货。为了使此过程更简单和通用,我们为基于Sylius平台的应用程序创建了一个抽象层。此插件允许您为特定的运输提供商编写简单的API调用和配置表单。工作流程相当简单 - 配置导出运输所需的数据,例如访问密钥或取货时间,单击一下预订快递,如果API收到任何运输标签文件,则获取运输标签文件。实现限制在编写运输提供商网关配置表单、一个事件监听器和网络服务访问层。

安装

有关完整安装指南,请访问此处

要求

我们在稳定、受支持和最新的包版本上工作。我们建议您也这样做。

使用

添加运输导出配置表单

<?php

declare(strict_types=1);

namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\NotBlank;

final class FrankMartinShippingGatewayType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('iban', TextType::class, [
                'label' => 'IBAN',
                'constraints' => [
                    new NotBlank([
                        'message' => 'IBAN number cannot be blank.',
                        'groups' => 'bitbag',
                    ]),
                ],
            ])
            ->add('address', TextType::class, [
                'label' => 'Address',
                'constraints' => [
                    new NotBlank([
                        'message' => 'Address cannot be blank.',
                        'groups' => 'bitbag',
                    ]),
                ],
            ])
        ;
    }
}

服务定义

services:
    app.form.type.frank_martin_shipping_gateway:
        class: App\Form\Type\FrankMartinShippingGatewayType
        tags:
            - { name: bitbag.shipping_gateway_configuration_type, type: "frank_martin_shipping_gateway", label: "Transporter Gateway" }

添加货运导出事件监听器

<?php

declare(strict_types=1);

namespace App\EventListener;

use BitBag\SyliusShippingExportPlugin\Entity\ShippingExportInterface;
use Doctrine\Persistence\ObjectManager;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\RequestStack;
use Webmozart\Assert\Assert;

final class FrankMartinShippingExportEventListener
{
    /** @var RequestStack */
    private $requestStack;

    /** @var Filesystem */
    private $filesystem;

    /** @var ObjectManager */
    private $shippingExportManager;

    /** @var string */
    private $shippingLabelsPath;

    public function __construct(
        RequestStack $requestStack,
        Filesystem $filesystem,
        ObjectManager $shippingExportManager,
        string $shippingLabelsPath
    ) {
        $this->requestStack = $requestStack;
        $this->filesystem = $filesystem;
        $this->shippingExportManager = $shippingExportManager;
        $this->shippingLabelsPath = $shippingLabelsPath;
    }

    public function exportShipment(ResourceControllerEvent $event): void
    {
        /** @var ShippingExportInterface $shippingExport */
        $shippingExport = $event->getSubject();
        Assert::isInstanceOf($shippingExport, ShippingExportInterface::class);

        $shippingGateway = $shippingExport->getShippingGateway();
        Assert::notNull($shippingGateway);

        if ('frank_martin_shipping_gateway' !== $shippingGateway->getCode()) {
            return;
        }

        $session = $this->requestStack->getSession();
        $flashBag = $session->getBag('flashes');

        if (false) {
            $flashBag->add('error', 'bitbag.ui.shipping_export_error'); // Add an error notification

            return;
        }

        $flashBag->add('success', 'bitbag.ui.shipment_data_has_been_exported'); // Add success notification
        $this->saveShippingLabel($shippingExport, 'Some label content received from external API', 'pdf'); // Save label
        $this->markShipmentAsExported($shippingExport); // Mark shipment as "Exported"
    }

    public function saveShippingLabel(
        ShippingExportInterface $shippingExport,
        string $labelContent,
        string $labelExtension
    ): void {
        $labelPath = $this->shippingLabelsPath
            . '/' . $this->getFilename($shippingExport)
            . '.' . $labelExtension;

        $this->filesystem->dumpFile($labelPath, $labelContent);
        $shippingExport->setLabelPath($labelPath);

        $this->shippingExportManager->persist($shippingExport);
        $this->shippingExportManager->flush();
    }

    private function getFilename(ShippingExportInterface $shippingExport): string
    {
        $shipment = $shippingExport->getShipment();
        Assert::notNull($shipment);

        $order = $shipment->getOrder();
        Assert::notNull($order);

        $orderNumber = $order->getNumber();

        $shipmentId = $shipment->getId();

        return implode(
            '_',
            [
                $shipmentId,
                preg_replace('~[^A-Za-z0-9]~', '', $orderNumber),
            ]
        );
    }

    private function markShipmentAsExported(ShippingExportInterface $shippingExport): void
    {
        $shippingExport->setState(ShippingExportInterface::STATE_EXPORTED);
        $shippingExport->setExportedAt(new \DateTime());

        $this->shippingExportManager->persist($shippingExport);
        $this->shippingExportManager->flush();
    }
}

服务定义

services:
    app.event_listener.frank_martin_shipping_export:
        class: App\EventListener\FrankMartinShippingExportEventListener
        arguments:
            - '@request_stack'
            - '@filesystem'
            - '@bitbag.manager.shipping_export'
            - '%bitbag.shipping_labels_path%'
        tags:
            - { name: kernel.event_listener, event: 'bitbag.shipping_export.export_shipment', method: exportShipment }

插件参数

parameters:
    bitbag.shipping_gateway.validation_groups: ['bitbag']
    bitbag.shipping_labels_path: '%kernel.project_dir%/shipping_labels'

您可以装饰的可用服务和您可以扩展的表单

$ bin/console debug:container | grep bitbag

您可以在您的 parameters.yml(.dist) 文件中覆盖的参数

$ bin/console debug:container --parameters | grep bitbag

测试

$ composer install
$ APP_ENV=test symfony server:start --port=8080 --dir=tests/Application/public --daemon
$ cd tests/Application
$ yarn install
$ yarn run gulp
$ bin/console assets:install public -e test
$ bin/console doctrine:schema:create -e test
$ shipping-export
$ open http://localhost:8080
$ vendor/bin/behat
$ vendor/bin/phpspec run
$ vendor/bin/phpstan analyse -c phpstan.neon -l max src/
$ vendor/bin/ecs check src

功能

插件的全部主要功能在此描述

演示

我们创建了一个演示应用程序,其中包含一些有用的插件用例!请访问http://demo.sylius.com/查看。

如果您需要了解 Sylius 的功能概述,请安排与我们的专家进行咨询。

开发者额外资源

要了解更多关于我们的贡献工作流程和其他信息,我们鼓励您使用以下资源

许可

此插件的源代码完全免费,并按照 MIT 许可证的条款发布。

联系和支持

此开源插件是为了帮助 Sylius 社区而开发的。如果您有任何其他问题,需要安装或配置插件的帮助,或者需要任何关于您的 Sylius 项目的协助 - 请告诉我们!联系我们或发送电子邮件至hello@bitbag.io(带您的疑问)

社区

我们邀请您在 Sylius Slack 上与我们一起聊天和其他用户进行在线交流。