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

Commit ab9a0b9

Browse files
author
Jens Schulze
committed
feat(Products): add getBySku method
1 parent ece9d87 commit ab9a0b9

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/Model/Product/ProductData.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,17 @@ public function getVariantById($variantId)
6464

6565
return $this->getVariants()->getById($variantId);
6666
}
67+
68+
/**
69+
* @param $sku
70+
* @return ProductVariant
71+
*/
72+
public function getVariantBySku($sku)
73+
{
74+
if ($sku == $this->getMasterVariant()->getSku()) {
75+
return $this->getMasterVariant();
76+
}
77+
78+
return $this->getVariants()->getBySku($sku);
79+
}
6780
}

src/Model/Product/ProductProjection.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,17 @@ public function getVariantById($variantId)
9797

9898
return $this->getVariants()->getById($variantId);
9999
}
100+
101+
/**
102+
* @param $sku
103+
* @return ProductVariant
104+
*/
105+
public function getVariantBySku($sku)
106+
{
107+
if ($sku == $this->getMasterVariant()->getSku()) {
108+
return $this->getMasterVariant();
109+
}
110+
111+
return $this->getVariants()->getBySku($sku);
112+
}
100113
}

src/Model/Product/ProductVariantCollection.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@
1616
class ProductVariantCollection extends Collection
1717
{
1818
const ID = 'id';
19+
const SKU = 'sku';
1920
protected $type = '\Commercetools\Core\Model\Product\ProductVariant';
2021

2122
protected function indexRow($offset, $row)
2223
{
2324
if ($row instanceof ProductVariant) {
2425
$id = $row->getId();
26+
$sku = $row->getSku();
2527
} else {
2628
$id = $row[static::ID];
29+
$sku = $row[static::SKU];
2730
}
2831
$this->addToIndex(static::ID, $offset, $id);
32+
$this->addToIndex(static::SKU, $offset, $sku);
2933
}
3034

3135
/**
@@ -36,4 +40,13 @@ public function getById($id)
3640
{
3741
return $this->getBy(static::ID, $id);
3842
}
43+
44+
/**
45+
* @param $id
46+
* @return ProductVariant
47+
*/
48+
public function getBySku($id)
49+
{
50+
return $this->getBy(static::SKU, $id);
51+
}
3952
}

0 commit comments

Comments
 (0)