From 3c60ecf4d5f75c6df1a6d5488082ee99eea5906d Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Thu, 17 Jun 2021 17:53:28 +0530 Subject: [PATCH] Backend Completed. Now Wiring To Shop Pending. --- .../Webkul/Product/src/Type/AbstractType.php | 2 +- .../Webkul/Product/src/Type/Configurable.php | 49 +++++++++++++++++-- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/packages/Webkul/Product/src/Type/AbstractType.php b/packages/Webkul/Product/src/Type/AbstractType.php index adb4f19e3..4f7aa17a5 100644 --- a/packages/Webkul/Product/src/Type/AbstractType.php +++ b/packages/Webkul/Product/src/Type/AbstractType.php @@ -67,7 +67,7 @@ abstract class AbstractType /** * Product instance * - * @var \Webkul\Product\Contracts\Product + * @var \Webkul\Product\Models\Product */ protected $product; diff --git a/packages/Webkul/Product/src/Type/Configurable.php b/packages/Webkul/Product/src/Type/Configurable.php index b228ed344..dae260f05 100644 --- a/packages/Webkul/Product/src/Type/Configurable.php +++ b/packages/Webkul/Product/src/Type/Configurable.php @@ -66,6 +66,46 @@ class Configurable extends AbstractType */ protected $productOptions = []; + + /** + * Get default variant id. + * + * @return int + */ + public function getDefaultVariantId() + { + return $this->product->additional['default_variant_id'] ?? null; + } + + /** + * Set default variant id. + * + * @param int $defaultVariantId + * @return bool + */ + public function setDefaultVariantId($defaultVariantId) + { + $this->product->additional = array_merge($this->product->additional ?? [], [ + 'default_variant_id' => $defaultVariantId + ]); + + return $this->product->save(); + } + + /** + * Update default variant id if present in request. + * + * @return void + */ + public function updateDefaultVariantId() + { + $defaultVariantId = request()->get('default_variant_id'); + + if ($defaultVariantId) { + $this->setDefaultVariantId($defaultVariantId); + } + } + /** * Create configurable product. * @@ -106,6 +146,9 @@ class Configurable extends AbstractType public function update(array $data, $id, $attribute = "id") { $product = parent::update($data, $id, $attribute); + + $this->updateDefaultVariantId(); + $route = request()->route() ? request()->route()->getName() : ''; if ($route != 'admin.catalog.products.massupdate') { @@ -670,8 +713,7 @@ class Configurable extends AbstractType /** * Validate cart item product price. * - * @param \Webkul\Product\Type\CartItem $item - * + * @param \Webkul\Product\Type\CartItem $item * @return \Webkul\Product\Datatypes\CartItemValidationResult */ public function validateCartItem(CartItemModel $item): CartItemValidationResult @@ -704,8 +746,7 @@ class Configurable extends AbstractType /** * Get product options. * - * @param string $product - * + * @param string $product * @return array */ public function getProductOptions($product = "")