Package to send prices to Magento from a Laravel application using a configurable source.

Overview

Laravel Magento Prices

Package to send prices to Magento from a Laravel application using a configurable source.

Features

The idea is that we want to push prices to Magento but we do not want to rewrite the logic of keeping track and updating prices to Magento. This package can:

  • Retrieve prices from any source
  • Push prices to Magento (base / tier / special)
  • Only update prices in Magento when are modified. i.e. when you retrieve the same price ten times it only updates once to Magento
  • Search for missing prices in Magento
  • Automatically stop syncing when updating fails
  • Logs activities using Spatie activitylog
  • Logs errors using JustBetter Error Logger
  • Checks if Magento products exist using JustBetter Magento Products

Installation

Require this package: composer require justbetter/laravel-magento-prices

Publish the config

php artisan vendor:publish --provider="JustBetter\MagentoPrices\ServiceProvider" --tag="config"

Publish the activity log's migrations:

php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-migrations"

Run migrations.

Usage

Add the following commands to your scheduler:

<?php

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

        $schedule->command(\JustBetter\MagentoPrices\Commands\RetrievePricesCommand::class)->daily();
        // Or for example
        $schedule->command(\JustBetter\MagentoPrices\Commands\RetrievePricesCommand::class)->weekly(); // Retrieve all weekly
        $schedule->command('price:retrieve --date=today')->dailyAt('23:00'); // Retrieve updated daily
    }

Retrieving Prices

In order to retrieve prices you have to create two classes. One to retrieve skus and one to retrieve the price(s) per sku.

Price retriever

This class is responsible for retrieving prices for products. Your class must extend \JustBetter\MagentoPrices\Retriever\PriceRetriever. Your class must return a PriceData object or null. The PriceData object is a wrapper around three collections:

  • Base prices
  • Tier prices
  • Special Prices

See the classes BasePriceData and TierPriceData in the JustBetter\MagentoPrices\Data namespace on how to use them.

For example:

<?php

namespace JustBetter\MagentoPrices\Retriever;

use JustBetter\MagentoPrices\Data\BasePriceData;
use JustBetter\MagentoPrices\Data\PriceData;
use JustBetter\MagentoPrices\Data\SpecialPriceData;
use JustBetter\MagentoPrices\Data\TierPriceData;
use JustBetter\MagentoPrices\Helpers\MoneyHelper;

class ExamplePriceRetriever extends PriceRetriever
{
    public function __construct(protected ExternalService $externalService, protected MoneyHelper $moneyHelper) {}

    public function retrieve(string $sku): ?PriceData
    {
        $basePrice = new BasePriceData(
            $this->moneyHelper->getMoney($this->externalService->getBasePrice($sku))
        );

        $tierPrices = $this->externalService->getTierPrices($sku)
            ->mapInto(TierPriceData::class);

        $specialPrices = $this->externalService->getSpecialPrices($sku)
            ->mapInto(SpecialPriceData::class);

        return new PriceData($sku, collect([$basePrice]), $tierPrices, $specialPrices);
    }
}

Then register your retriever in the config file config/magento-prices.php:

<?php

return [
    'retrievers' => [
        'price' => ExamplePriceRetriever::class,
    ],

You can retrieve the price by running: php artisan price:retrieve {sku}

Storing Money

We use Brick/money for storing prices, to create a price use:

 $basePrice = new BasePriceData(
            Money::of(10, config('laravel-magento-prices.currency'))
        );

There is a helper for that which adds precision, context and the rounding mode from the config: JustBetter\MagentoPrices\Helpers\MoneyHelper

Example

To help you get started you can look at the \JustBetter\MagentoPrices\Retriever\DummyPriceRetriever

SKU Retriever

In order to know what SKU's to retrieve prices for you have to create a SKU retriever class. This class must extend \JustBetter\MagentoPrices\Retriever\SkuRetriever.

It is required to have a method that retrieves all skus. You can optionally implement the retrieveByDate method to retrieve updated skus.

And example can be found in \JustBetter\MagentoPrices\Retriever\DummySkuRetriever

Don't forget to register your retriever in the config file config/magento-prices.php:

<?php

return [
    'retrievers' => [
        'sku' => MyAwewsomeSkuRetriever::class,
    ],

Checking for missing prices

There is a build in action that checks all products in Magento where there is no price or the price is zero. For each product it will automatically start an update or retrieve.

You can run this with the command: php artisan price:missing

Syncing

The php artisan price:sync command will check the retrieve and update flags and dispatch jobs to retrieve/update the prices. In order to not overload your price source or Magento you can set limits in the config file.

<?php

return [
    /* How many price retrieval jobs may be dispatched per sync */
    'retrieve_limit' => 25,

    /* How many prices update jobs may be dispatched per sync */
    'update_limit' => 100,
];

Long Waits

The sync limits the amount of products that are retrieved/updated each sync. This may result in long waits if not properly configured for the amount of updates you get.

To detect this you can add the \JustBetter\MagentoPrices\Commands\MonitorWaitTimesCommand to your schedule. This will fire the \JustBetter\MagentoPrices\Events\LongWaitDetectedEvent event in which you can for example trigger more updates or send a notification.

You can configure the limits of when the event will be fired in the config:

<?php

return [
    'monitor' => [
        /* Max wait time in minutes, if exceeded the LongWaitDetected event is dispatched */
        'retrieval_max_wait' => 30,

        /* Max wait time in minutes, if exceeded the LongWaitDetected event is dispatched */
        'update_max_wait' => 30,
    ]
];

Handling failures

When an update fails it will try again. A fail counter is stored with the model which is increased at each failure. A common failure is a missing required attribute in Magento.

In the config you can specify how many times the update may be attempted:

<?php

return [
    /* How many times can a price update failed before being cancelled */
    'fail_count' => 5,
];

Note This applies to all three types of updates. Base, tier and special.

Quality

To ensure the quality of this package, run the following command:

composer quality

This will execute three tasks:

  1. Makes sure all tests are passed
  2. Checks for any issues using static code analysis
  3. Checks if the code is correctly formatted

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

You might also like...
Simple configurable plugin to see all commands executed by players
Simple configurable plugin to see all commands executed by players

About [FR] Simple plugin configurable permettant de voir toutes les commandes exécutées par les joueurs [ENG] Simple configurable plugin to see all co

Magento 2 Italian Language Pack is special gift for Magento 2 Community Edition. The data of the translation is supplied by Magento 2 Translation Project of Crowdin site, on which you will contribute into the project and download the zip file to install on your own store. This knowledge base will include full of the clear guides that supports you to convert the default language (English) into Italian language on your entire store.
A tool that allows to quickly export data from Magento 1 and Magento 2 store and import it back into Magento 2

Simple Import / Export tool A tool that allows to quickly export data from Magento 1 and Magento 2 store and import it back into Magento 2. Table data

Send tokens using PHP

Waiting vs Not Waiting The $transactionCount obtained by calling getTransactionCount() is only accurate if ALL the previously submitted transactions h

Magento 2 Finnish Language Pack is the perfect guide so that you can enable Finnish on your magento 2 store. This translation is really necessary for everyone who are living in the Finland. Here is a step-by-step guide to install Finnish package and use it as the default language.
Magento 2 Spanish (Spain) language package - Paquete de idioma Español (España) para Magento 2

Magento 2 Spanish (Spain) language package - Paquete de idioma Español (España) para Magento 2 Traducciones Se han recopilado las traducciones existen

A simple package to send HTTP header responses ✨

PHP HTTP Response Simple PHP package to easily send the right HTTP header responses to the browser 🐘 Requirement PHP v8.0 or higher 🚀 Installation T

Send your laravel apps to the moon with web3 enabled 🚀

Laravel Web3 🚀 🌝 Laravel Web3 helps you to kickstart your web3 apps. No more headache with handling wallet changing address and sync with laravel ba

Laravel & MySQL, jQuery, Ajax, Bootstrap. Also, it's include email send function without any API.
Laravel & MySQL, jQuery, Ajax, Bootstrap. Also, it's include email send function without any API.

Rewards-Dacor Laravel & MySQL, jQuery, Ajax, Bootstrap. Also, it's include email send function without any API. [Live site link] ( https://rewardsdaco

Releases(1.0.3)
  • 1.0.3(Oct 12, 2022)

    What's Changed

    • Long wait event by @VincentBean in https://github.com/justbetter/laravel-magento-prices/pull/3

    Full Changelog: https://github.com/justbetter/laravel-magento-prices/compare/1.0.2...1.0.3

    Source code(tar.gz)
    Source code(zip)
  • 1.0.2(Oct 11, 2022)

    What's Changed

    • Add GitHub Actions for tests, static analysis and style checks by @VincentBean in https://github.com/justbetter/laravel-magento-prices/pull/1
    • Do not log non existing product error by @VincentBean in https://github.com/justbetter/laravel-magento-prices/pull/2

    New Contributors

    • @VincentBean made their first contribution in https://github.com/justbetter/laravel-magento-prices/pull/1

    Full Changelog: https://github.com/justbetter/laravel-magento-prices/compare/1.0.1...1.0.2

    Source code(tar.gz)
    Source code(zip)
  • 1.0.0(Sep 20, 2022)

Owner
JustBetter
We're an innovative development agency from The Netherlands building awesome websites, webshops and web applications with Laravel and Magento.
JustBetter
Enhancement to Magento to allow simple product prices to be used instead of the default special-case configurable product prices

Simple Configurable Products Extension For Magento This documentation applies to SCP versions 0.7 onwards. The documentation for SCP v0.6 and earlier

Simon King 288 Nov 7, 2022
This package is the Laravel Nova integration for justbetter/laravel-magento-prices

Laravel Magento Products Nova This package is the Laravel Nova integration for justbetter/laravel-magento-products. Installation Install the package.

JustBetter 14 Nov 4, 2022
Laravel Nova integration for justbetter/laravel-magento-customer-prices

Laravel Magento Customer Prices Nova Laravel Nova integration for justbetter/laravel-magento-customer-prices. Installation Install the package. compos

JustBetter 13 Nov 4, 2022
Laravel Nova integration for justbetter/laravel-magento-prices

Laravel Magento Prices Nova This package is the Laravel Nova integration for justbetter/laravel-magento-prices. Installation Install the package. comp

JustBetter 15 Nov 29, 2022
A Magento 2 module that enables configurable CORS Headers on the GraphQL and REST APIs

Magento 2 CORS Magento Version Support Ever try to work with the Magento GraphQL API or REST API from your browser and see the following? Access to XM

Graycore, LLC 62 Dec 8, 2022
Configure Magento 2 to send email using Google App, Gmail, Amazon Simple Email Service (SES), Microsoft Office365 and many other SMTP (Simple Mail Transfer Protocol) servers

Magento 2 SMTP Extension - Gmail, G Suite, Amazon SES, Office 365, Mailgun, SendGrid, Mandrill and other SMTP servers. For Magento 2.0.x, 2.1.x, 2.2.x

MagePal :: Magento Extensions 303 Oct 7, 2022
A tool for creating configurable dumps of large MySQL-databases.

slimdump slimdump is a little tool to help you create configurable dumps of large MySQL-databases. It works off one or several configuration files. Fo

webfactory GmbH 176 Dec 26, 2022
Oui player - Manage configurable media players in @Textpattern CMS

oui_player Introduction An extendable plugin to easily embed customized audio and video players. . This plugin does not use oembed, it builds iframe e

Nicolas Morand 3 Aug 15, 2018