justbetter/laravel-magento-prices

向 Magento 推送价格的通用包

2.1.0 2024-09-10 06:46 UTC

README

Package banner

Laravel Magento Prices

Tests Coverage Analysis Total downloads

通过可配置的源将价格从 Laravel 应用程序发送到 Magento 的包。

功能

我们的想法是,我们想要将价格推送到 Magento,但不想重写跟踪和更新价格到 Magento 的逻辑。这个包可以:

  • 从任何来源检索价格
  • 将价格推送到 Magento(基础/层/特殊)
  • 只有当价格被修改时才更新 Magento 中的价格。例如,当你检索同一价格十次时,它只更新一次到 Magento
  • 在 Magento 中搜索缺失的价格
  • 更新失败时自动停止同步
  • 支持使用 Laravel Magento Async 更新 Magento 2 的异步批量请求
  • 使用 Spatie activitylog 记录活动
  • 使用 JustBetter Magento Products 检查 Magento 产品是否存在

也使用客户特定的价格? 查看我们的其他包! 我们还有一个 Magento Client,可轻松将 Laravel 连接到 Magento!

安装

需要此包: composer require justbetter/laravel-magento-prices

发布配置: php artisan vendor:publish --provider="JustBetter\MagentoPrices\ServiceProvider" --tag="config"

发布活动日志的迁移: php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-migrations"

运行迁移。 php artisan migrate

提示: 此包中的所有操作都是通过作业运行的,我们建议使用 Laravel Horizon 或其他队列系统来运行这些作业

Laravel Nova

我们为此包提供了 Laravel Nova 集成

用法

将以下命令添加到您的计划任务中

<?php

    protected function schedule(Schedule $schedule): void
    {
        $schedule->command(\JustBetter\MagentoPrices\Commands\ProcessPricesCommand::class)->everyMinute();

        $schedule->command(\JustBetter\MagentoPrices\Commands\Retrieval\RetrieveAllPricesCommand::class)->daily();
        $schedule->command(\JustBetter\MagentoPrices\Commands\Retrieval\RetrieveAllPricesCommand::class, ['from' => 'now -2 hours'])->hourly(); // Retrieve updated
    }

检索价格

此包与一个负责按 SKU 检索价格的存储库一起工作,您必须实现它。

存储库

此类负责检索产品的价格,检索 SKU 和设置。您的类必须扩展 \JustBetter\MagentoPrices\Repository\Repository 并实现 retrieve 方法。如果没有 SKU 的价格,您可能返回 null。在其他所有情况下,您需要返回一个包含四个元素的 PriceData 对象

  • sku 必需的
  • base_prices 可选的,基础价格数组
  • tier_prices 可选的,层价格数组
  • special_prices 可选的,特殊价格数组

价格数组的格式遵循 Magento 的 API。您可以在 PriceData 类中查看规则,以了解您需要提供的内容。

示例
<?php

namespace App\Integrations\MagentoPrices;

use JustBetter\MagentoPrices\Data\PriceData;
use JustBetter\MagentoPrices\Repository\Repository;

class MyPriceRepository extends Repository
{
  public function retrieve(string $sku): ?PriceData
    {
        return PriceData::of([
            'sku' => $sku,
            'base_prices' => [
                [
                    'store_id' => 0,
                    'price' => 10,
                ],
                [
                    'store_id' => 2,
                    'price' => 19,
                ],
            ],
            'tier_prices' => [
                [
                    'website_id' => 0,
                    'customer_group' => 'group_1',
                    'price_type' => 'fixed',
                    'quantity' => 1,
                    'price' => 8,
                ],
                [
                    'website_id' => 0,
                    'customer_group' => '4040',
                    'price_type' => 'group_2',
                    'quantity' => 1,
                    'price' => 7,
                ],
            ],
            'special_prices' => [
                [
                    'store_id' => 0,
                    'price' => 5,
                    'price_from' => now()->subWeek()->toDateString(),
                    'price_to' => now()->addWeek()->toDateString(),
                ],
            ],
        ]);
    }
}

检索 SKU

默认情况下,您所扩展的Repository将从justbetter/laravel-magento-products检索SKU。如果您想使用它,您需要将命令添加到您的调度器以自动导入产品。

如果您有其他来源的SKU,您可以自己实现skus方法。它接受一个可选的carbon实例,仅检索修改过的库存。

<?php

namespace App\Integrations\MagentoPrices;

use JustBetter\MagentoPrices\Repositories\Repository;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;

class MyPriceRepository implements Repository
{
    public function skus(?Carbon $from = null): ?Collection
    {
        return collect(['sku_1', 'sku_2']);
    }
}

配置仓库

仓库类有几个您可以调整的设置

class BaseRepository
{
    // How many prices may be retrieved at once when the process job runs
    protected int $retrieveLimit = 250;

    // How many prices may be updated at once when the process job runs
    protected int $updateLimit = 250;

    // How many times an update to Magento may fail before it stops trying
    protected int $failLimit = 3;
}

创建并配置仓库后,您必须在配置文件中设置它

<?php

return [
    'repository' => \App\Integrations\MagentoPrices\MyPriceRepository::class,
];

检查缺失的价格

有一个内置的操作可以检查所有在Magento中没有价格或价格为零的产品。对于每个产品,它将自动启动更新或检索。

您可以使用以下命令运行: php artisan magento-prices:process-missing-prices

处理失败

当更新失败时,它会再次尝试。失败计数器与模型一起存储,每次失败都会增加。在仓库中,您可以指定更新可以尝试的次数

事件

此包发出的事件包括

  • \JustBetter\MagentoPrices\Events\UpdatedPriceEvent - 当价格更新时触发

异步批量

为了大幅减少对Magento的请求量,您可以在配置文件中启用async选项。这将使用Laravel Magento Async来发送更新请求。不要忘记遵循该包的安装指南。

质量

为确保此包的质量,请运行以下命令

composer quality

这将执行三个任务

  1. 确保所有测试都通过
  2. 检查任何使用静态代码分析的问题
  3. 检查代码是否正确格式化

贡献

请参阅CONTRIBUTING以获取详细信息。

安全漏洞

请查看我们的安全策略,了解如何报告安全漏洞。

鸣谢

许可

MIT许可证(MIT)。有关更多信息,请参阅许可文件

Package footer