Skip to content

Commit

Permalink
Fixed #926
Browse files Browse the repository at this point in the history
- Variant status is no longer overriden by the product status.
- product status and variant status together define if the variant is available for sale
  • Loading branch information
lukeholder committed Aug 29, 2019
1 parent cb5da01 commit 8c7cd57
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/elements/Variant.php
Original file line number Diff line number Diff line change
Expand Up @@ -814,30 +814,28 @@ public function afterOrderComplete(Order $order, LineItem $lineItem)
*/
public function getIsAvailable(): bool
{
if ($this->getProduct() && !$this->getProduct()->availableForPurchase) {
$product = $this->getProduct();

if (!$product) {
return false;
}

if ($this->getStatus() !== Element::STATUS_ENABLED) {
// is the parent product available for sale?
if (!$product->availableForPurchase) {
return false;
}

return $this->stock >= 1 || $this->hasUnlimitedStock;
}

/**
* @inheritdoc
*/
public function getStatus()
{
$status = parent::getStatus();
// is the variant enabled?
if ($this->getStatus() !== Element::STATUS_ENABLED) {
return false;
}

$productStatus = $this->getProduct()->getStatus();
if ($productStatus != Product::STATUS_LIVE) {
return Element::STATUS_DISABLED;
// is parent product enabled?
if ($product->getStatus() !== Element::STATUS_ENABLED) {
return false;
}

return $status;
return $this->stock >= 1 || $this->hasUnlimitedStock;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/elements/db/VariantQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @replace {myElement} myVariant
* @replace {element-class} \craft\commerce\elements\Variant
* @supports-site-params
* @supports-status-param
* @supports-title-param
*/
class VariantQuery extends ElementQuery
Expand Down

0 comments on commit 8c7cd57

Please sign in to comment.