diff --git a/packages/Webkul/Product/src/Helpers/BundleOption.php b/packages/Webkul/Product/src/Helpers/BundleOption.php index da383ce41..ca1d06543 100644 --- a/packages/Webkul/Product/src/Helpers/BundleOption.php +++ b/packages/Webkul/Product/src/Helpers/BundleOption.php @@ -79,7 +79,7 @@ class BundleOption extends AbstractProduct $products[$bundleOptionProduct->id] = [ 'id' => $bundleOptionProduct->id, 'qty' => $bundleOptionProduct->qty, - 'price' => $this->getProductPrices($bundleOptionProduct->product), + 'price' => $bundleOptionProduct->product->getTypeInstance()->getProductPrices(), 'name' => $bundleOptionProduct->product->name, 'product_id' => $bundleOptionProduct->product_id, 'is_default' => $bundleOptionProduct->is_default @@ -88,24 +88,4 @@ class BundleOption extends AbstractProduct return $products; } - - /** - * Returns bundle product prices - * - * @param Product $product - * @return array - */ - public function getProductPrices($product) - { - return [ - 'regular_price' => [ - 'price' => core()->convertPrice($product->price), - 'formated_price' => core()->currency($product->price) - ], - 'final_price' => [ - 'price' => core()->convertPrice($product->getTypeInstance()->getMinimalPrice()), - 'formated_price' => core()->currency($product->getTypeInstance()->getMinimalPrice()) - ] - ]; - } } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Helpers/ConfigurableOption.php b/packages/Webkul/Product/src/Helpers/ConfigurableOption.php index 5831cac13..2f25463ce 100755 --- a/packages/Webkul/Product/src/Helpers/ConfigurableOption.php +++ b/packages/Webkul/Product/src/Helpers/ConfigurableOption.php @@ -218,16 +218,7 @@ class ConfigurableOption extends AbstractProduct $variantId = $variant->id; } - $prices[$variantId] = [ - 'regular_price' => [ - 'formated_price' => core()->currency($variant->price), - 'price' => $variant->price - ], - 'final_price' => [ - 'formated_price' => core()->currency($variant->getTypeInstance()->getMinimalPrice()), - 'price' => $variant->getTypeInstance()->getMinimalPrice() - ] - ]; + $prices[$variantId] = $variant->getTypeInstance()->getProductPrices(); } return $prices; diff --git a/packages/Webkul/Product/src/Type/AbstractType.php b/packages/Webkul/Product/src/Type/AbstractType.php index 0b1588aea..793616b6c 100644 --- a/packages/Webkul/Product/src/Type/AbstractType.php +++ b/packages/Webkul/Product/src/Type/AbstractType.php @@ -394,6 +394,25 @@ abstract class AbstractType return false; } + /** + * Returns product prices + * + * @return array + */ + public function getProductPrices() + { + return [ + 'regular_price' => [ + 'price' => core()->convertPrice($this->product->price), + 'formated_price' => core()->currency($this->product->price) + ], + 'final_price' => [ + 'price' => core()->convertPrice($this->getMinimalPrice()), + 'formated_price' => core()->currency($this->getMinimalPrice()) + ] + ]; + } + /** * Get product minimal price * diff --git a/packages/Webkul/Product/src/Type/Bundle.php b/packages/Webkul/Product/src/Type/Bundle.php index 6fea5307b..3268187db 100644 --- a/packages/Webkul/Product/src/Type/Bundle.php +++ b/packages/Webkul/Product/src/Type/Bundle.php @@ -10,6 +10,7 @@ use Webkul\Product\Repositories\ProductImageRepository; use Webkul\Product\Repositories\ProductBundleOptionRepository; use Webkul\Product\Repositories\ProductBundleOptionProductRepository; use Webkul\Product\Helpers\ProductImage; +use Webkul\Product\Helpers\BundleOption; /** * Class Bundle. @@ -19,41 +20,6 @@ use Webkul\Product\Helpers\ProductImage; */ class Bundle extends AbstractType { - /** - * AttributeRepository instance - * - * @var AttributeRepository - */ - protected $attributeRepository; - - /** - * ProductRepository instance - * - * @var ProductRepository - */ - protected $productRepository; - - /** - * ProductAttributeValueRepository instance - * - * @var ProductAttributeValueRepository - */ - protected $attributeValueRepository; - - /** - * ProductInventoryRepository instance - * - * @var ProductInventoryRepository - */ - protected $productInventoryRepository; - - /** - * ProductImageRepository instance - * - * @var ProductImageRepository - */ - protected $productImageRepository; - /** * ProductBundleOptionRepository instance * @@ -69,11 +35,11 @@ class Bundle extends AbstractType protected $productBundleOptionProductRepository; /** - * Product Image helper instance + * Bundle Option helper instance * - * @var ProductImage + * @var BundleOption */ - protected $productImageHelper; + protected $bundleOptionHelper; /** * Skip attribute for Bundle product type @@ -112,6 +78,7 @@ class Bundle extends AbstractType * @param Webkul\Product\Repositories\ProductBundleOptionRepository $productBundleOptionRepository * @param Webkul\Product\Repositories\ProductBundleOptionProductRepository $productBundleOptionProductRepository * @param Webkul\Product\Helpers\ProductImage $productImageHelper + * @param Webkul\Product\Helpers\BundleOption $bundleOptionHelper * @return void */ public function __construct( @@ -122,7 +89,8 @@ class Bundle extends AbstractType productImageRepository $productImageRepository, ProductBundleOptionRepository $productBundleOptionRepository, ProductBundleOptionProductRepository $productBundleOptionProductRepository, - ProductImage $productImageHelper + ProductImage $productImageHelper, + BundleOption $bundleOptionHelper ) { parent::__construct( @@ -137,6 +105,8 @@ class Bundle extends AbstractType $this->productBundleOptionRepository = $productBundleOptionRepository; $this->productBundleOptionProductRepository = $productBundleOptionProductRepository; + + $this->bundleOptionHelper = $bundleOptionHelper; } /** @@ -162,7 +132,45 @@ class Bundle extends AbstractType */ public function getMinimalPrice() { - return 0; + $optionPrices = []; + + foreach ($this->product->bundle_options as $option) { + foreach ($option->bundle_option_products as $index => $bundleOptionProduct) { + $optionPrices[$option->id][] = $bundleOptionProduct->qty * $bundleOptionProduct->product->getTypeInstance()->getMinimalPrice(); + } + } + + $minPrice = 0; + + foreach ($optionPrices as $key => $optionPrice) { + $minPrice += min($optionPrice); + } + + return $minPrice; + } + + /** + * Get product regular minimal price + * + * @return float + */ + public function getRegularMinimalPrice() + { + $optionPrices = []; + + foreach ($this->product->bundle_options as $option) { + foreach ($option->bundle_option_products as $index => $bundleOptionProduct) { + $optionPrices[$option->id][] = $bundleOptionProduct->qty * $bundleOptionProduct->product->price; + } + } + + $minPrice = 0; + + foreach ($optionPrices as $key => $optionPrice) { + $minPrice += min($optionPrice); + } + + return $minPrice; } /** @@ -172,7 +180,61 @@ class Bundle extends AbstractType */ public function getMaximamPrice() { - return 0; + $optionPrices = []; + + foreach ($this->product->bundle_options as $option) { + foreach ($option->bundle_option_products as $index => $bundleOptionProduct) { + if (in_array($option->type, ['multiselect', 'checkbox'])) { + if (! isset($optionPrices[$option->id][0])) + $optionPrices[$option->id][0] = 0; + + $optionPrices[$option->id][0] += $bundleOptionProduct->qty * $bundleOptionProduct->product->getTypeInstance()->getMinimalPrice(); + } else { + $optionPrices[$option->id][] = $bundleOptionProduct->qty * $bundleOptionProduct->product->getTypeInstance()->getMinimalPrice(); + } + + } + } + + $maxPrice = 0; + + foreach ($optionPrices as $key => $optionPrice) { + $maxPrice += max($optionPrice); + } + + return $maxPrice; + } + + /** + * Get product regular maximam price + * + * @return float + */ + public function getRegularMaximamPrice() + { + $optionPrices = []; + + foreach ($this->product->bundle_options as $option) { + foreach ($option->bundle_option_products as $index => $bundleOptionProduct) { + if (in_array($option->type, ['multiselect', 'checkbox'])) { + if (! isset($optionPrices[$option->id][0])) + $optionPrices[$option->id][0] = 0; + + $optionPrices[$option->id][0] += $bundleOptionProduct->qty * $bundleOptionProduct->product->price; + } else { + $optionPrices[$option->id][] = $bundleOptionProduct->qty * $bundleOptionProduct->product->price; + } + + } + } + + $maxPrice = 0; + + foreach ($optionPrices as $key => $optionPrice) { + $maxPrice += max($optionPrice); + } + + return $maxPrice; } /** @@ -185,6 +247,37 @@ class Bundle extends AbstractType return 0; } + /** + * Returns product prices + * + * @return array + */ + public function getProductPrices() + { + return [ + 'from' => [ + 'regular_price' => [ + 'price' => core()->convertPrice($this->getRegularMinimalPrice()), + 'formated_price' => core()->currency($this->getRegularMinimalPrice()) + ], + 'final_price' => [ + 'price' => core()->convertPrice($this->getMinimalPrice()), + 'formated_price' => core()->currency($this->getMinimalPrice()) + ] + ], + 'to' => [ + 'regular_price' => [ + 'price' => core()->convertPrice($this->getRegularMaximamPrice()), + 'formated_price' => core()->currency($this->getRegularMaximamPrice()) + ], + 'final_price' => [ + 'price' => core()->convertPrice($this->getMaximamPrice()), + 'formated_price' => core()->currency($this->getMaximamPrice()) + ] + ] + ]; + } + /** * Get product minimal price * @@ -192,6 +285,29 @@ class Bundle extends AbstractType */ public function getPriceHtml() { + $prices = $this->getProductPrices(); + + $priceHtml = '
'; + + if ($prices['from']['regular_price']['price'] != $prices['from']['final_price']['price']) { + $priceHtml .= '' . $prices['from']['regular_price']['formated_price'] . '' + . '' . $prices['from']['final_price']['formated_price'] . ''; + } else { + $priceHtml .= '' . $prices['from']['regular_price']['formated_price'] . ''; + } + + $priceHtml .= 'To'; + + if ($prices['to']['regular_price']['price'] != $prices['to']['final_price']['price']) { + $priceHtml .= '' . $prices['to']['regular_price']['formated_price'] . '' + . '' . $prices['to']['final_price']['formated_price'] . ''; + } else { + $priceHtml .= '' . $prices['to']['regular_price']['formated_price'] . ''; + } + + $priceHtml .= '
'; + + return $priceHtml; } /** diff --git a/packages/Webkul/Product/src/Type/Downloadable.php b/packages/Webkul/Product/src/Type/Downloadable.php index 3d77152ad..ded76c88f 100644 --- a/packages/Webkul/Product/src/Type/Downloadable.php +++ b/packages/Webkul/Product/src/Type/Downloadable.php @@ -20,41 +20,6 @@ use Webkul\Checkout\Models\CartItem; */ class Downloadable extends AbstractType { - /** - * AttributeRepository instance - * - * @var AttributeRepository - */ - protected $attributeRepository; - - /** - * ProductRepository instance - * - * @var ProductRepository - */ - protected $productRepository; - - /** - * ProductAttributeValueRepository instance - * - * @var ProductAttributeValueRepository - */ - protected $attributeValueRepository; - - /** - * ProductInventoryRepository instance - * - * @var ProductInventoryRepository - */ - protected $productInventoryRepository; - - /** - * ProductImageRepository instance - * - * @var ProductImageRepository - */ - protected $productImageRepository; - /** * ProductDownloadableLinkRepository instance * @@ -69,13 +34,6 @@ class Downloadable extends AbstractType */ protected $productDownloadableSampleRepository; - /** - * Product Image helper instance - * - * @var ProductImage - */ - protected $productImageHelper; - /** * Skip attribute for downloadable product type * diff --git a/packages/Webkul/Product/src/Type/Grouped.php b/packages/Webkul/Product/src/Type/Grouped.php index f32a13ae6..a43a44099 100644 --- a/packages/Webkul/Product/src/Type/Grouped.php +++ b/packages/Webkul/Product/src/Type/Grouped.php @@ -20,54 +20,12 @@ use Webkul\Product\Models\ProductFlat; */ class Grouped extends AbstractType { - /** - * AttributeRepository instance - * - * @var AttributeRepository - */ - protected $attributeRepository; - - /** - * ProductRepository instance - * - * @var ProductRepository - */ - protected $productRepository; - - /** - * ProductAttributeValueRepository instance - * - * @var ProductAttributeValueRepository - */ - protected $attributeValueRepository; - - /** - * ProductInventoryRepository instance - * - * @var ProductInventoryRepository - */ - protected $productInventoryRepository; - - /** - * ProductImageRepository instance - * - * @var ProductImageRepository - */ - protected $productImageRepository; - /** * ProductGroupedProductRepository instance * * @var ProductGroupedProductRepository */ protected $productGroupedProductRepository; - - /** - * Product Image helper instance - * - * @var ProductImage - */ - protected $productImageHelper; /** * Skip attribute for downloadable product type