Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/Http/Controllers/CP/VariantsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@

use Illuminate\Http\Request;
use Statamic\Facades\Entry;
use Statamic\Facades\Site;
use Statamic\Http\Controllers\CP\CpController;
use StatamicRadPack\Shopify\Blueprints\VariantBlueprint;

class VariantsController extends CpController
{
public function fetch($product)
{
$site = Site::selected()->handle();

return Entry::query()
->where('collection', 'variants')
->where('product_slug', $product)
->where('site', $site)
->get()
->map(function ($variant) {
$values = [];
$values['id'] = $variant->id();
$values['slug'] = $variant->slug();

// Map all variant values to data to ensure we are getting everything.
foreach ($variant->data() as $key => $value) {
$values[$key] = $value;
}

return $values;
return $variant->values()->merge([
'id' => $variant->id(),
'slug' => $variant->slug(),
]);
});
}

Expand Down
120 changes: 120 additions & 0 deletions tests/Unit/VariantsControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace StatamicRadPack\Shopify\Tests\Unit;

use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades;
use Statamic\Facades\User;
use StatamicRadPack\Shopify\Tests\TestCase;

class VariantsControllerTest extends TestCase
{
private function actingAsSuperUser()
{
$user = User::make()->email('admin@example.com')->makeSuper()->save();

return $this->actingAs($user);
}

private function setupMultisite(): void
{
Facades\Site::setSites([
'en' => ['url' => '/', 'locale' => 'en_US'],
'de' => ['url' => '/de/', 'locale' => 'de_DE'],
]);

Facades\Collection::make(config('shopify.collection_handle', 'products'))->sites(['en', 'de'])->save();
Facades\Collection::make('variants')->sites(['en', 'de'])->save();
}

private function makeVariant(string $slug, array $data = [])
{
$variant = Facades\Entry::make()
->collection('variants')
->slug($slug)
->data(array_merge(['product_slug' => 'test-product'], $data));

$variant->save();

return $variant;
}

#[Test]
public function returns_variants_for_default_site()
{
$this->makeVariant('variant-en', ['title' => 'English Variant', 'price' => '10.00', 'sku' => 'EN-1']);

$response = $this->actingAsSuperUser()
->getJson(cp_route('shopify.variants.index', 'test-product'));

$response->assertOk();
$response->assertJsonCount(1);
$response->assertJsonPath('0.title', 'English Variant');
}

#[Test]
public function filters_variants_by_selected_site()
{
$this->setupMultisite();

$enVariant = $this->makeVariant('variant-en', ['title' => 'English Variant', 'price' => '10.00', 'sku' => 'EN-1']);
$enVariant->makeLocalization('de')->data(['title' => 'German Variant', 'price' => '12.00', 'sku' => 'DE-1'])->save();

Facades\Site::setSelected('en');

$response = $this->actingAsSuperUser()
->getJson(cp_route('shopify.variants.index', 'test-product'));

$response->assertOk();
$response->assertJsonCount(1);
$response->assertJsonPath('0.title', 'English Variant');

Facades\Site::setSelected('de');

$response = $this->actingAsSuperUser()
->getJson(cp_route('shopify.variants.index', 'test-product'));

$response->assertOk();
$response->assertJsonCount(1);
$response->assertJsonPath('0.title', 'German Variant');
}

#[Test]
public function localized_variant_inherits_title_price_and_sku_from_origin()
{
$this->setupMultisite();

$enVariant = $this->makeVariant('variant-en', ['title' => 'Origin Title', 'price' => '9.99', 'sku' => 'ORIGIN-1']);
$enVariant->makeLocalization('de')->data([])->save();

Facades\Site::setSelected('de');

$response = $this->actingAsSuperUser()
->getJson(cp_route('shopify.variants.index', 'test-product'));

$response->assertOk();
$response->assertJsonCount(1);
$response->assertJsonPath('0.title', 'Origin Title');
$response->assertJsonPath('0.price', '9.99');
$response->assertJsonPath('0.sku', 'ORIGIN-1');
}

#[Test]
public function localized_variant_values_take_precedence_over_origin()
{
$this->setupMultisite();

$enVariant = $this->makeVariant('variant-en', ['title' => 'Origin Title', 'price' => '9.99', 'sku' => 'ORIGIN-1']);
$enVariant->makeLocalization('de')->data(['title' => 'DE Title', 'price' => '12.00', 'sku' => 'DE-1'])->save();

Facades\Site::setSelected('de');

$response = $this->actingAsSuperUser()
->getJson(cp_route('shopify.variants.index', 'test-product'));

$response->assertOk();
$response->assertJsonPath('0.title', 'DE Title');
$response->assertJsonPath('0.price', '12.00');
$response->assertJsonPath('0.sku', 'DE-1');
}
}
Loading