Skip to content

Commit

Permalink
PES-2537: carrier may be unavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
J7F authored and J7F committed Mar 3, 2025
1 parent cc68696 commit 5b53903
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 51 deletions.
32 changes: 13 additions & 19 deletions src/Packetery/Core/Entity/Carrier.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ class Carrier {
*/
private $maxWeight;

/**
* Tells if carrier is available.
*
* @var bool
*/
private $isAvailable;

/**
* Carrier isDeleted.
*
Expand All @@ -127,25 +134,6 @@ class Carrier {
*/
private $ageVerification;

/**
* Carrier constructor.
*
* @param string $id Carrier id.
* @param string $name Carrier name.
* @param bool $hasPickupPoints Carrier hasPickupPoints.
* @param bool $hasDirectLabel Carrier hasDirectLabel.
* @param bool $requiresSeparateHouseNumber Carrier requiresSeparateHouseNumber.
* @param bool $requiresCustomsDeclarations Carrier requiresCustomsDeclarations.
* @param bool $requiresEmail Carrier requiresEmail.
* @param bool $requiresPhone Carrier requiresPhone.
* @param bool $requiresSize Carrier requiresSize.
* @param bool $supportsCod Carrier supportsCod.
* @param string $country Carrier country.
* @param string $currency Carrier currency.
* @param float $maxWeight Carrier maxWeight.
* @param bool $isDeleted Carrier isDeleted.
* @param bool $ageVerification Carrier supports age verification.
*/
public function __construct(
string $id,
string $name,
Expand All @@ -160,6 +148,7 @@ public function __construct(
string $country,
string $currency,
float $maxWeight,
bool $isAvailable,
bool $isDeleted,
bool $ageVerification
) {
Expand All @@ -176,6 +165,7 @@ public function __construct(
$this->country = $country;
$this->currency = $currency;
$this->maxWeight = $maxWeight;
$this->isAvailable = $isAvailable;
$this->isDeleted = $isDeleted;
$this->ageVerification = $ageVerification;
}
Expand Down Expand Up @@ -306,6 +296,10 @@ public function getMaxWeight(): float {
return $this->maxWeight;
}

public function isAvailable(): bool {
return $this->isAvailable;
}

/**
* Gets carrier isDeleted.
*
Expand Down
7 changes: 4 additions & 3 deletions src/Packetery/Module/Carrier/CarrierActivityBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Packetery\Module\Carrier;

use Packetery\Core\Entity;
use Packetery\Module\Carrier;
use Packetery\Module\Options\OptionsProvider;
use Packetery\Module\Shipping\BaseShippingMethod;
Expand Down Expand Up @@ -41,11 +42,11 @@ private function getActiveCarrierIds(): array {
return $activeMethods;
}

public function isActive( string $carrierId, Carrier\Options $carrierOptions ): bool {
public function isActive( Entity\Carrier $carrier, Carrier\Options $carrierOptions ): bool {
if ( $this->optionsProvider->isWcCarrierConfigEnabled() ) {
return in_array( $carrierId, $this->getActiveCarrierIds(), true );
return $carrier->isAvailable() && in_array( $carrier->getId(), $this->getActiveCarrierIds(), true );
}

return $carrierOptions->isActive();
return $carrier->isAvailable() && $carrierOptions->isActive();
}
}
2 changes: 1 addition & 1 deletion src/Packetery/Module/Carrier/CountryListingPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ private function getCarriersDataByCountry( string $countryCode ): array {
$carriersData[ $carrierId ] = [
'name' => $carrier->getName(),
'isActivatedByUser' => $carrierOptions->isActive(),
'isActive' => $this->carrierActivityBridge->isActive( $carrier->getId(), $carrierOptions ),
'isActive' => $this->carrierActivityBridge->isActive( $carrier, $carrierOptions ),
'detailUrl' => add_query_arg(
[
'page' => OptionsPage::SLUG,
Expand Down
18 changes: 10 additions & 8 deletions src/Packetery/Module/Carrier/EntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Packetery\Module\Carrier;

use Packetery\Core\Entity;
use Packetery\Core\Entity\Carrier;
use Packetery\Module\EntityFactory;

/**
Expand Down Expand Up @@ -121,9 +122,9 @@ public function getAnyById( string $carrierId ): ?Entity\Carrier {
*
* @return Entity\Carrier[]
*/
public function getByCountry( string $country ): array {
public function getByCountry( string $country, bool $includeUnavailable = false ): array {
$entities = [];
$countryCarriers = $this->repository->getByCountry( $country );
$countryCarriers = $this->repository->getByCountry( $country, $includeUnavailable );

foreach ( $countryCarriers as $carrierData ) {
$entities[] = $this->carrierEntityFactory->fromDbResult( $carrierData );
Expand Down Expand Up @@ -152,16 +153,17 @@ public function getActiveCarriers(): array {
* Gets all active carriers for a country including internal pickup point carriers.
*
* @param string $country ISO code.
* @param bool $includeUnavailable Include unavailable carriers.
*
* @return Entity\Carrier[]
* @return Carrier[]
*/
public function getByCountryIncludingNonFeed( string $country ): array {
public function getByCountryIncludingNonFeed( string $country, bool $includeUnavailable = false ): array {
$nonFeedCarriers = [];
$nonFeedCarriersArrays = $this->pickupPointsConfig->getNonFeedCarriersByCountry( $country );
foreach ( $nonFeedCarriersArrays as $nonFeedCarrierData ) {
$nonFeedCarriers[] = $this->carrierEntityFactory->fromNonFeedCarrierData( $nonFeedCarrierData );
}
$feedCarriers = $this->getByCountry( $country );
$feedCarriers = $this->getByCountry( $country, $includeUnavailable );

return array_merge( $nonFeedCarriers, $feedCarriers );
}
Expand Down Expand Up @@ -204,7 +206,7 @@ public function getAllActiveCarriersList(): array {
$carriers = $this->getAllCarriersIncludingNonFeed();
foreach ( $carriers as $carrier ) {
$carrierOptions = $this->carrierOptionsFactory->createByCarrierId( $carrier->getId() );
if ( $this->carrierActivityBridge->isActive( $carrier->getId(), $carrierOptions ) ) {
if ( $this->carrierActivityBridge->isActive( $carrier, $carrierOptions ) ) {
$activeCarriers[] = [
'option_id' => $carrierOptions->getOptionId(),
'label' => $carrierOptions->getName(),
Expand Down Expand Up @@ -232,13 +234,13 @@ public function isValidForCountry( string $carrierId, string $customerCountry ):
}

$carrier = $this->getById( (int) $carrierId );
if ( $carrier === null || $carrier->isDeleted() || $customerCountry !== $carrier->getCountry() ) {
if ( $carrier === null || $carrier->isDeleted() || ! $carrier->isAvailable() || $customerCountry !== $carrier->getCountry() ) {
return false;
}

$carrierOptions = $this->carrierOptionsFactory->createByCarrierId( $carrier->getId() );

return $this->carrierActivityBridge->isActive( $carrier->getId(), $carrierOptions );
return $this->carrierActivityBridge->isActive( $carrier, $carrierOptions );
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Packetery/Module/Carrier/OptionsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ private function renderCarrierDetail( string $carrierId ): void {
}

private function renderCountryCarriers( string $countryIso ): void {
$countryCarriers = $this->carrierRepository->getByCountryIncludingNonFeed( $countryIso );
$countryCarriers = $this->carrierRepository->getByCountryIncludingNonFeed( $countryIso, true );
$carriersData = [];
foreach ( $countryCarriers as $carrier ) {
$carrierTemplateData = $this->getCarrierTemplateData( $carrier );
Expand Down Expand Up @@ -687,6 +687,7 @@ private function getTranslations(): array {
'allowedPickupPointTypes' => $this->wpAdapter->__( 'Pickup point types', 'packeta' ),
'checkAtLeastTwo' => $this->wpAdapter->__( 'Check at least two types of pickup points or use a carrier which delivers to the desired pickup point type.', 'packeta' ),
'lowAvailableVendorsCount' => $this->wpAdapter->__( 'This carrier displays all types of pickup points at the same time in the checkout (retail store pickup points, Z-boxes).', 'packeta' ),
'carrierUnavailable' => $this->wpAdapter->__( 'This carrier is unavailable.', 'packeta' ),
];
}

Expand Down
14 changes: 9 additions & 5 deletions src/Packetery/Module/Carrier/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Repository {
'currency',
'max_weight',
'deleted',
'available',
];

/**
Expand Down Expand Up @@ -74,6 +75,7 @@ public function createOrAlterTable(): bool {
`country` varchar(255) NOT NULL,
`currency` varchar(255) NOT NULL,
`max_weight` float NOT NULL,
`available` tinyint(1) NOT NULL DEFAULT 1,
`deleted` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ' . $this->wpdbAdapter->get_charset_collate();
Expand Down Expand Up @@ -116,14 +118,16 @@ public function getById( int $carrierId ): ?array {
* Gets all active carriers for a country.
*
* @param string $country ISO code.
* @param bool $includeUnavailable Include unavailable carriers.
*
* @return array|null
*/
public function getByCountry( string $country ): ?array {
public function getByCountry( string $country, bool $includeUnavailable = false ): ?array {
return $this->wpdbAdapter->get_results(
$this->wpdbAdapter->prepare(
'SELECT `' . implode( '`, `', self::COLUMN_NAMES ) . '`
FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `country` = %s AND `deleted` = false',
FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `country` = %s AND `deleted` = false' .
( $includeUnavailable ? '' : ' AND `available` = true' ),
$country
),
ARRAY_A
Expand All @@ -138,7 +142,7 @@ public function getByCountry( string $country ): ?array {
public function getActiveCarriers(): ?array {
return $this->wpdbAdapter->get_results(
'SELECT `' . implode( '`, `', self::COLUMN_NAMES ) . '`
FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `deleted` = false',
FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `deleted` = false AND `available` = true',
ARRAY_A
);
}
Expand All @@ -164,7 +168,7 @@ public function getAllRawIndexed(): array {
* @return bool
*/
public function hasAnyActiveFeedCarrier(): bool {
return (bool) $this->wpdbAdapter->get_var( 'SELECT 1 FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `deleted` = false LIMIT 1' );
return (bool) $this->wpdbAdapter->get_var( 'SELECT 1 FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `deleted` = false AND `available` = true LIMIT 1' );
}

/**
Expand All @@ -173,7 +177,7 @@ public function hasAnyActiveFeedCarrier(): bool {
* @return array
*/
public function getCountries(): array {
return $this->wpdbAdapter->get_col( 'SELECT `country` FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `deleted` = false GROUP BY `country` ORDER BY `country`' );
return $this->wpdbAdapter->get_col( 'SELECT `country` FROM `' . $this->wpdbAdapter->packeteryCarrier . '` WHERE `deleted` = false AND `available` = true GROUP BY `country` ORDER BY `country`' );
}

/**
Expand Down
41 changes: 34 additions & 7 deletions src/Packetery/Module/Carrier/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Packetery\Core\Log\ILogger;
use Packetery\Core\Log\Record;
use Packetery\Module\Framework\WpAdapter;
use Packetery\Module\Transients;

/**
Expand Down Expand Up @@ -42,14 +43,25 @@ class Updater {
private $logger;

/**
* CarrierUpdater constructor.
*
* @param Repository $carrierRepository Carrier repository.
* @param ILogger $logger Logger.
* @var CarrierOptionsFactory
*/
public function __construct( Repository $carrierRepository, ILogger $logger ) {
$this->carrierRepository = $carrierRepository;
$this->logger = $logger;
private $carrierOptionsFactory;

/**
* @var WpAdapter
*/
private $wpAdapter;

public function __construct(
Repository $carrierRepository,
ILogger $logger,
CarrierOptionsFactory $carrierOptionsFactory,
WpAdapter $wpAdapter
) {
$this->carrierRepository = $carrierRepository;
$this->logger = $logger;
$this->carrierOptionsFactory = $carrierOptionsFactory;
$this->wpAdapter = $wpAdapter;
}

/**
Expand Down Expand Up @@ -102,6 +114,7 @@ private function carriers_mapper( array $carriers ): array {
'requires_phone' => 'requiresPhone',
'requires_size' => 'requiresSize',
'disallows_cod' => 'disallowsCod',
'available' => 'available',
);

foreach ( $carriers as $carrier ) {
Expand Down Expand Up @@ -134,6 +147,16 @@ public function save( array $carriers ): void {
if ( isset( $carriersInDb[ $carrierId ] ) ) {
$this->carrierRepository->update( $carrier, $carrierId );
$differences = $this->getArrayDifferences( $carriersInDb[ $carrierId ], $carrier );
if ( isset( $differences['available'] ) ) {
$carrierOptions = $this->carrierOptionsFactory->createByCarrierId( (string) $carrierId );
if ( $carrierOptions->isActive() ) {
$this->wpAdapter->updateOption(
$carrierOptions->getOptionId(),
array_merge( $carrierOptions->toArray(), [ 'active' => false ] )
);
}
}

if ( count( $differences ) > 0 ) {
$this->addLogEntry(
// translators: %s is carrier name.
Expand Down Expand Up @@ -289,6 +312,10 @@ private function getColumnSettings(): array {
'label' => __( 'maximum weight (kg)', 'packeta' ),
'isBoolean' => false,
],
'available' => [
'label' => __( 'available', 'packeta' ),
'isBoolean' => true,
],
];
}
}
2 changes: 1 addition & 1 deletion src/Packetery/Module/Checkout/ShippingRateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private function canCreateShippingRate(
$containsOversizedProduct = $this->cartService->cartContainsProductOversizedForCarrier( $carrierOptions );
$isRestrictedByCategory = $this->cartService->isShippingRateRestrictedByProductsCategory( $optionId, $cartProducts );

return ! ( $isCarrierOptionInactive || $isCarDeliveryDisabled || $isOptionDisallowed || $containsOversizedProduct || $isRestrictedByCategory );
return $carrier->isAvailable() && ! ( $isCarrierOptionInactive || $isCarDeliveryDisabled || $isOptionDisallowed || $containsOversizedProduct || $isRestrictedByCategory );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Packetery/Module/DashboardWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function render(): void {
$country = $carrier->getCountry();
$carrierOptions = $this->carrierOptionsFactory->createByCarrierId( $carrier->getId() );

if ( $this->carrierActivityBridge->isActive( $carrier->getId(), $carrierOptions ) === false ) {
if ( $this->carrierActivityBridge->isActive( $carrier, $carrierOptions ) === false ) {
continue;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Packetery/Module/EntityFactory/Carrier.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function fromDbResult( array $dbResult ): Entity\Carrier {
$dbResult['country'],
$dbResult['currency'],
(float) $dbResult['max_weight'],
(bool) $dbResult['available'],
(bool) $dbResult['deleted'],
$ageVerified
);
Expand Down Expand Up @@ -77,6 +78,7 @@ public function fromNonFeedCarrierData( BaseProvider $nonFeedCarrierProvider ):
$nonFeedCarrierProvider->getCountry(),
$nonFeedCarrierProvider->getCurrency(),
10,
true,
false,
$nonFeedCarrierProvider->supportsAgeVerification()
);
Expand Down
8 changes: 7 additions & 1 deletion template/carrier/carrier.latte
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
</div>
{/if}

{if !$carrier_data['carrier']->isAvailable()}
<div class="notice notice-info">
<p>{$translations['carrierUnavailable']}</p>
</div>
{/if}

{define pricingRule}
<tr n:class="$class ?? ''" n:formContainer="$container[$id]" data-replication-item>
<td>
Expand Down Expand Up @@ -158,7 +164,7 @@
{var $form = $carrier_data['form']}
<form n:name=$form>
<table class="form-table" role="presentation">
<tr n:ifset="$form['active']">
<tr n:ifset="$form['active']" n:if="$carrier_data['carrier']->isAvailable()">
<th scope="row">
<label n:name=active />
</th>
Expand Down
Loading

0 comments on commit 5b53903

Please sign in to comment.