This repository was archived by the owner on Oct 24, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 1616class 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}
You can’t perform that action at this time.
0 commit comments