Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
feat(Products): add getBySku method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jens Schulze committed Sep 9, 2015
1 parent ece9d87 commit ab9a0b9
Showing 3 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Model/Product/ProductData.php
Original file line number Diff line number Diff line change
@@ -64,4 +64,17 @@ public function getVariantById($variantId)

return $this->getVariants()->getById($variantId);
}

/**
* @param $sku
* @return ProductVariant
*/
public function getVariantBySku($sku)
{
if ($sku == $this->getMasterVariant()->getSku()) {
return $this->getMasterVariant();
}

return $this->getVariants()->getBySku($sku);
}
}
13 changes: 13 additions & 0 deletions src/Model/Product/ProductProjection.php
Original file line number Diff line number Diff line change
@@ -97,4 +97,17 @@ public function getVariantById($variantId)

return $this->getVariants()->getById($variantId);
}

/**
* @param $sku
* @return ProductVariant
*/
public function getVariantBySku($sku)
{
if ($sku == $this->getMasterVariant()->getSku()) {
return $this->getMasterVariant();
}

return $this->getVariants()->getBySku($sku);
}
}
13 changes: 13 additions & 0 deletions src/Model/Product/ProductVariantCollection.php
Original file line number Diff line number Diff line change
@@ -16,16 +16,20 @@
class ProductVariantCollection extends Collection
{
const ID = 'id';
const SKU = 'sku';
protected $type = '\Commercetools\Core\Model\Product\ProductVariant';

protected function indexRow($offset, $row)
{
if ($row instanceof ProductVariant) {
$id = $row->getId();
$sku = $row->getSku();
} else {
$id = $row[static::ID];
$sku = $row[static::SKU];
}
$this->addToIndex(static::ID, $offset, $id);
$this->addToIndex(static::SKU, $offset, $sku);
}

/**
@@ -36,4 +40,13 @@ public function getById($id)
{
return $this->getBy(static::ID, $id);
}

/**
* @param $id
* @return ProductVariant
*/
public function getBySku($id)
{
return $this->getBy(static::SKU, $id);
}
}

0 comments on commit ab9a0b9

Please # to comment.