Implemented bundle product price html

This commit is contained in:
jitendra 2019-09-04 14:58:39 +05:30
parent eafa585cb8
commit 29ba0c28de
6 changed files with 178 additions and 156 deletions

View File

@ -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())
]
];
}
}

View File

@ -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;

View File

@ -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
*

View File

@ -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 = '<div class="price-from">';
if ($prices['from']['regular_price']['price'] != $prices['from']['final_price']['price']) {
$priceHtml .= '<span class="regular-price">' . $prices['from']['regular_price']['formated_price'] . '</span>'
. '<span class="special-price">' . $prices['from']['final_price']['formated_price'] . '</span>';
} else {
$priceHtml .= '<span>' . $prices['from']['regular_price']['formated_price'] . '</span>';
}
$priceHtml .= '<span style="font-weight: 500;margin-top: 1px;margin-bottom: 1px;display: block;">To</span>';
if ($prices['to']['regular_price']['price'] != $prices['to']['final_price']['price']) {
$priceHtml .= '<span class="regular-price">' . $prices['to']['regular_price']['formated_price'] . '</span>'
. '<span class="special-price">' . $prices['to']['final_price']['formated_price'] . '</span>';
} else {
$priceHtml .= '<span>' . $prices['to']['regular_price']['formated_price'] . '</span>';
}
$priceHtml .= '</div>';
return $priceHtml;
}
/**

View File

@ -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
*

View File

@ -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