From 643384eade6ebf2948ec3e206631c7166e012a9c Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Tue, 20 Jul 2021 14:54:22 +0530 Subject: [PATCH] Refresh Method Added --- .../Webkul/Product/src/Models/Product.php | 29 ++++++++++++++----- tests/unit/Checkout/Cart/CartCest.php | 6 ++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/packages/Webkul/Product/src/Models/Product.php b/packages/Webkul/Product/src/Models/Product.php index a5e2211b4..220a5a15c 100755 --- a/packages/Webkul/Product/src/Models/Product.php +++ b/packages/Webkul/Product/src/Models/Product.php @@ -43,6 +43,13 @@ class Product extends Model implements ProductContract */ protected $typeInstance; + /** + * Loaded attribute values. + * + * @var $loadedAttributeValues + */ + public static $loadedAttributeValues = []; + /** * The "booted" method of the model. * @@ -65,6 +72,16 @@ class Product extends Model implements ProductContract }); } + /** + * Refresh the loaded attribute values. + * + * @return void + */ + public function refreshloadedAttributeValues() + { + self::$loadedAttributeValues = []; + } + /** * Get the product attribute family that owns the product. */ @@ -370,9 +387,7 @@ class Product extends Model implements ProductContract */ public function getCustomAttributeValue($attribute) { - static $loadedAttributeValue = []; - - if (!$attribute) { + if (! $attribute) { return; } @@ -380,10 +395,10 @@ class Product extends Model implements ProductContract $channel = core()->getRequestedChannelCode(); if ( - array_key_exists($this->id, $loadedAttributeValue) - && array_key_exists($attribute->id, $loadedAttributeValue[$this->id]) + array_key_exists($this->id, self::$loadedAttributeValues) + && array_key_exists($attribute->id, self::$loadedAttributeValues[$this->id]) ) { - return $loadedAttributeValue[$this->id][$attribute->id]; + return self::$loadedAttributeValues[$this->id][$attribute->id]; } if ($attribute->value_per_channel) { @@ -400,7 +415,7 @@ class Product extends Model implements ProductContract } } - return $loadedAttributeValue[$this->id][$attribute->id] = $attributeValue[ProductAttributeValue::$attributeTypeFields[$attribute->type]] ?? null; + return self::$loadedAttributeValues[$this->id][$attribute->id] = $attributeValue[ProductAttributeValue::$attributeTypeFields[$attribute->type]] ?? null; } /** diff --git a/tests/unit/Checkout/Cart/CartCest.php b/tests/unit/Checkout/Cart/CartCest.php index 0baad7700..dc53dc3cf 100644 --- a/tests/unit/Checkout/Cart/CartCest.php +++ b/tests/unit/Checkout/Cart/CartCest.php @@ -80,6 +80,8 @@ class CartCest ]) ->update(['boolean_value' => 0]); + $this->simpleProduct2->refreshloadedAttributeValues(); + Event::dispatch('catalog.product.update.after', $this->simpleProduct2->refresh()); $I->assertFalse(cart()->hasError()); @@ -107,6 +109,8 @@ class CartCest ]) ->update(['boolean_value' => 0]); + $this->simpleProduct2->refreshloadedAttributeValues(); + Event::dispatch('catalog.product.update.after', $this->downloadableProduct2->refresh()); $I->comment('add dP1 to cart, dP2 should be removed now'); @@ -128,6 +132,8 @@ class CartCest ]) ->update(['boolean_value' => 0]); + $this->simpleProduct2->refreshloadedAttributeValues(); + Event::dispatch('catalog.product.update.after', $this->virtualProduct2->refresh()); $I->comment('change quantity of vP1, vP2 should be removed now');