Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FFWEB-2049: Price Export based on Cart Rules #21

Open
wants to merge 6 commits into
base: release/4.x
Choose a base branch
from
Open
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
25 changes: 23 additions & 2 deletions spec/Export/Field/PriceSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Omikron\FactFinder\Shopware6\Export\Formatter\NumberFormatter;
use PhpSpec\ObjectBehavior;
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
use Shopware\Core\Checkout\Cart\Price\Struct\PriceCollection;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity as Product;

class PriceSpec extends ObjectBehavior
Expand All @@ -27,11 +28,31 @@ function it_has_a_name()
$this->getName()->shouldReturn('Price');
}

function it_gets_the_product_price(Product $product, CalculatedPrice $price, NumberFormatter $numberFormatter)
{
function it_gets_the_calculated_product_price_if_no_calculated_prices(
Product $product,
CalculatedPrice $price,
PriceCollection $priceCollection,
NumberFormatter $numberFormatter
) {
$product->getCalculatedPrices()->willReturn($priceCollection);
$priceCollection->count()->willReturn(0);
$product->getCalculatedPrice()->willReturn($price);
$price->getTotalPrice()->willReturn(pi());
$numberFormatter->format(pi())->willReturn('3.14');
$this->getValue($product)->shouldReturn('3.14');
}

function it_gets_the_first_calculated_price_if_exist(
Product $product,
PriceCollection $priceCollection,
CalculatedPrice $price,
NumberFormatter $numberFormatter
) {
$priceCollection->count()->willReturn(1);
$product->getCalculatedPrices()->willReturn($priceCollection);
$priceCollection->first()->willReturn($price);
$price->getUnitPrice()->willReturn(pi());
$numberFormatter->format(pi())->willReturn('3.14');
$this->getValue($product)->shouldReturn('3.14');
}
}
5 changes: 4 additions & 1 deletion src/Export/Field/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public function getName(): string

public function getValue(Product $product): string
{
return $this->numberFormatter->format((float) $product->getCalculatedPrice()->getTotalPrice());
if ($product->getCalculatedPrices()->count() === 0) {
return $this->numberFormatter->format((float) $product->getCalculatedPrice()->getTotalPrice());
}
return $this->numberFormatter->format((float) $product->getCalculatedPrices()->first()->getUnitPrice());
}
}
24 changes: 17 additions & 7 deletions src/Export/SalesChannelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@
namespace Omikron\FactFinder\Shopware6\Export;

use Omikron\FactFinder\Shopware6\BackwardCompatibility\Extension\SalesChannelContextFactoryInterface;
use Shopware\Core\Checkout\Cart\CartRuleLoader;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Api\Context\SystemSource;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Util\Random;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SalesChannel\SalesChannelEntity;

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
class SalesChannelService
{
/** @var EntityRepositoryInterface */
Expand All @@ -23,27 +28,32 @@ class SalesChannelService
/** @var SalesChannelContextFactoryInterface */
private $channelContextFactory;

/** @var CartRuleLoader */
private $cartRuleLoader;

/** @var SalesChannelContext|null */
private $cachedSalesChannel;
private $cachedContext;

public function __construct(
SalesChannelContextFactoryInterface $channelContextFactory,
EntityRepositoryInterface $channelRepository
EntityRepositoryInterface $channelRepository,
CartRuleLoader $cartRuleLoader
) {
$this->channelRepository = $channelRepository;
$this->channelContextFactory = $channelContextFactory;
$this->cartRuleLoader = $cartRuleLoader;
}

public function getSalesChannelContext(SalesChannelEntity $salesChannel = null, $languageId = null): SalesChannelContext
{
if (!$this->cachedSalesChannel) {
$usedChannel = $salesChannel ?: $this->getDefaultStoreFrontSalesChannel();
$this->cachedSalesChannel = $this->channelContextFactory->create('', $usedChannel->getId(), [
if (!$this->cachedContext) {
$usedChannel = $salesChannel ?: $this->getDefaultStoreFrontSalesChannel();
$this->cachedContext = $this->channelContextFactory->create('', $usedChannel->getId(), [
SalesChannelContextService::LANGUAGE_ID => $languageId ?: $usedChannel->getLanguageId(),
]);
}

return $this->cachedSalesChannel;
$this->cartRuleLoader->loadByToken($this->cachedContext, Random::getAlphanumericString(32));
return $this->cachedContext;
}

private function getDefaultStoreFrontSalesChannel(): SalesChannelEntity
Expand Down