Skip to content

Commit

Permalink
Support JB customer pricing module, drop support for Mageplaza (#6)
Browse files Browse the repository at this point in the history
* Support JB customer pricing module, drop support for Mageplaza better tier pricing

* Upgrade guide
  • Loading branch information
VincentBean authored Jun 22, 2023
1 parent f59972e commit 4c4330d
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 138 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
</p>

This package provides a way to add customer specific prices to Magento from a Laravel app.
By default, it uses the [Magaplaza Better Tier Price](https://www.mageplaza.com/magento-2-better-tier-price/) module for customer specific prices.
You can implement another customer specific price module, see [Replacing Mageplaza](#replacing-mageplaza).
By default, it uses the [JustBetter Magento 2 Customer Pricing](https://github.com/justbetter/magento2-customer-pricing) module for customer specific prices.
You can implement another customer specific price module, see [Updating Customer Prices](#magento-2-customer-prices).

## Features
This package can:
Expand Down Expand Up @@ -88,11 +88,12 @@ Must return an enumerable of strings
#### Example
See the `\JustBetter\MagentoCustomerPrices\Retriever\DummyCustomerPriceRetriever` class for an example.

## Replacing Mageplaza
## Magento 2 Customer Prices

By default this package uses the [Magaplaza Better Tier Price](https://www.mageplaza.com/magento-2-better-tier-price/) module for updating prices to Magento.
You can use another package by creating a class that implements `JustBetter\MagentoCustomerPrices\Contracts\UpdatesMagentoCustomerPrices`.
See `\JustBetter\MagentoCustomerPrices\Actions\UpdateMageplazaCustomerPrices` for an example.
By default this package uses the [JustBetter Magento 2 Customer Pricing](https://github.com/justbetter/magento2-customer-pricing) module for updating prices to Magento.
If you use another Magento 2 module for customer specific pricing you can write your own class that updates prices in Magento.
You can do this by implementing `JustBetter\MagentoCustomerPrices\Contracts\UpdatesMagentoCustomerPrices`.
See `\JustBetter\MagentoCustomerPrices\Actions\UpdateCustomerPrices` for an example.

Don't forget to bind your own class!
```
Expand Down
6 changes: 6 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Upgrading from 1.x to 2.x

You do not need to modify code in your price/sku retriever.

If you were using the Mageplaza Better Tier Price module you now have to implement your update class yourself or upgrade to the [JustBetter Magento 2 Customer Pricing](https://github.com/justbetter/magento2-customer-pricing) module.
See the readme on how you can implement your own updater class, you can use the default class from version 1.x as a template.
27 changes: 12 additions & 15 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Tests">
<directory>./tests/*</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Tests">
<directory>./tests/*</directory>
</testsuite>
</testsuites>
<coverage/>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,24 @@
use JustBetter\MagentoCustomerPrices\Data\CustomerPriceData;
use JustBetter\MagentoCustomerPrices\Models\MagentoCustomerPrice;

class UpdateMageplazaCustomerPrices implements UpdatesMagentoCustomerPrices
class UpdateCustomerPrices implements UpdatesMagentoCustomerPrices
{
public function __construct(protected Magento $magento)
{
}

public function update(MagentoCustomerPrice $model): void
{
$recordId = 0;
$mageplazaData = $model->getDataCollection()->map(function (CustomerPriceData $price) use (&$recordId) {
$data = $price->toMageplazaData();

// Retrieve the customer name from Magento
$customer = $this->magento->get('customers/'.$price->getCustomerId())->json();

$data['customer'] = implode(' ', [$customer['firstname'], $customer['lastname']]);
$data['record_id'] = (string) $recordId;

$recordId++;

return $data;
$prices = $model->getDataCollection()->map(function (CustomerPriceData $price) {
return [
'customer_id' => $price->customerId,
'quantity' => $price->quantity,
'price' => $price->price->getAmount()->toFloat(),
];
})->toArray();

$response = $this->magento->put("products/$model->sku", [
'product' => [
'custom_attributes' => [
[
'attribute_code' => 'mp_specific_customer',
'value' => json_encode($mageplazaData),
],
],
],
$response = $this->magento->post('customer-pricing/'.urlencode($model->sku), [
'customerPrices' => $prices,
]);

if (! $response->ok()) {
Expand Down
12 changes: 0 additions & 12 deletions src/Data/CustomerPriceData.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,6 @@ public static function fromArray(array $data): static
);
}

public function toMageplazaData(): array
{
return [
'website_id' => (string) $this->storeId,
'customer_id' => (string) $this->customerId,
'price_qty' => (string) max($this->quantity, 1),
'value_type' => 'fixed',
'price' => (string) $this->getPrice()->getAmount()->toFloat(),
'initialize' => '1',
];
}

public function equals(self $other): bool
{
/** @var string $a */
Expand Down
4 changes: 2 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use JustBetter\MagentoCustomerPrices\Actions\RetrieveCustomerPrice;
use JustBetter\MagentoCustomerPrices\Actions\RetrieveUpdatedPriceSkus;
use JustBetter\MagentoCustomerPrices\Actions\RunCustomerPriceSync;
use JustBetter\MagentoCustomerPrices\Actions\UpdateMageplazaCustomerPrices;
use JustBetter\MagentoCustomerPrices\Actions\UpdateCustomerPrices;
use JustBetter\MagentoCustomerPrices\Actions\UpdatePrices;
use JustBetter\MagentoCustomerPrices\Commands\RetrieveAllCustomerPricesCommand;
use JustBetter\MagentoCustomerPrices\Commands\RetrieveCustomerPriceCommand;
Expand Down Expand Up @@ -53,7 +53,7 @@ public function registerActions(): static
RunCustomerPriceSync::bind();

UpdatePrices::bind();
UpdateMageplazaCustomerPrices::bind();
UpdateCustomerPrices::bind();

return $this;
}
Expand Down
68 changes: 68 additions & 0 deletions tests/Actions/UpdateCustomerPricesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace JustBetter\MagentoCustomerPrices\Tests\Actions;

use Illuminate\Http\Client\Request;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;
use JustBetter\MagentoCustomerPrices\Actions\UpdateCustomerPrices;
use JustBetter\MagentoCustomerPrices\Models\MagentoCustomerPrice;
use JustBetter\MagentoCustomerPrices\Tests\TestCase;

class UpdateCustomerPricesTest extends TestCase
{
public UpdateCustomerPrices $action;

public MagentoCustomerPrice $model;

protected function setUp(): void
{
parent::setUp();

$this->action = app(UpdateCustomerPrices::class);
$this->model = MagentoCustomerPrice::create([
'sku' => '::sku::',
'prices' => [
[
'sku' => '13901952', 'price' => '40.52000000', 'storeId' => 0, 'quantity' => 1, 'customerId' => 1,
],
],
]);
}

public function test_it_updates_price(): void
{
Http::fake([
'*customer-pricing*' => Http::response([
'firstname' => '::firstname::',
'lastname' => '::lastname::',
]),
'*products/::sku::' => Http::response(),
]);

$this->action->update($this->model);

Http::assertSent(function (Request $request) {
return $request->data() === [
'customerPrices' => [
[
'customer_id' => 1,
'quantity' => 1,
'price' => 40.52,
],
],
];
});
}

public function test_it_throws_error_on_fail(): void
{
Http::fake([
'*customer-pricing*' => Http::response('::error::', 500),
]);

$this->expectException(RequestException::class);

$this->action->update($this->model);
}
}
80 changes: 0 additions & 80 deletions tests/Actions/UpdateMageplazaCustomerPricesTest.php

This file was deleted.

0 comments on commit 4c4330d

Please sign in to comment.