sarga/packages/Webkul/Product/src/Helpers/BundleOption.php

143 lines
3.8 KiB
PHP
Raw Normal View History

2019-08-30 11:39:15 +00:00
<?php
namespace Webkul\Product\Helpers;
class BundleOption extends AbstractProduct
{
/**
* Product
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Product\Contracts\Product|\Webkul\Product\Contracts\ProductFlat
2019-08-30 11:39:15 +00:00
*/
protected $product;
/**
* Returns bundle option config
*
2020-03-05 13:37:08 +00:00
* @param \Webkul\Product\Contracts\Product|\Webkul\Product\Contracts\ProductFlat $product
2019-08-30 11:39:15 +00:00
* @return array
*/
public function getBundleConfig($product)
{
$this->product = $product;
return [
2020-03-04 06:32:53 +00:00
'options' => $this->getOptions(),
2019-08-30 11:39:15 +00:00
];
}
/**
* Returns bundle options
*
* @return array
*/
public function getOptions()
{
$options = [];
foreach ($this->product->bundle_options as $option) {
2020-06-08 10:40:53 +00:00
$data = $this->getOptionItemData($option);
if (! count($data['products'])) {
continue;
}
$options[$option->id] = $data;
2019-08-30 11:39:15 +00:00
}
2020-01-27 07:54:22 +00:00
usort ($options, function($a, $b) {
if ($a['sort_order'] == $b['sort_order']) {
return 0;
}
return ($a['sort_order'] < $b['sort_order']) ? -1 : 1;
});
2019-08-30 11:39:15 +00:00
return $options;
}
/**
* Get formed data from bundle option
*
2020-03-05 13:37:08 +00:00
* @param \Product\Product\Contracts\ProductBundleOption $option
2019-08-30 11:39:15 +00:00
* @return array
*/
private function getOptionItemData($option)
{
return [
2020-02-20 06:12:17 +00:00
'id' => $option->id,
'label' => $option->label,
'type' => $option->type,
'is_required' => $option->is_required,
2020-02-20 06:12:17 +00:00
'products' => $this->getOptionProducts($option),
2020-03-04 06:32:53 +00:00
'sort_order' => $option->sort_order,
2019-08-30 11:39:15 +00:00
];
}
/**
* Get formed data from bundle option product
2020-01-27 07:54:22 +00:00
*
2020-03-05 13:37:08 +00:00
* @param \Product\Product\Contracts\ProductBundleOption $option
2019-08-30 11:39:15 +00:00
* @return array
*/
private function getOptionProducts($option)
{
$products = [];
foreach ($option->bundle_option_products as $index => $bundleOptionProduct) {
2020-06-08 10:40:53 +00:00
if (! $bundleOptionProduct->product->getTypeInstance()->isSaleable()) {
continue;
}
2019-08-30 11:39:15 +00:00
$products[$bundleOptionProduct->id] = [
2020-02-20 06:12:17 +00:00
'id' => $bundleOptionProduct->id,
'qty' => $bundleOptionProduct->qty,
'price' => $bundleOptionProduct->product->getTypeInstance()->getProductPrices(),
'name' => $bundleOptionProduct->product->name,
2019-08-30 11:39:15 +00:00
'product_id' => $bundleOptionProduct->product_id,
2020-01-27 07:54:22 +00:00
'is_default' => $bundleOptionProduct->is_default,
2020-03-04 06:32:53 +00:00
'sort_order' => $bundleOptionProduct->sort_order,
2019-08-30 11:39:15 +00:00
];
}
2020-01-27 07:54:22 +00:00
usort ($products, function($a, $b) {
if ($a['sort_order'] == $b['sort_order']) {
return 0;
}
return ($a['sort_order'] < $b['sort_order']) ? -1 : 1;
});
2019-08-30 11:39:15 +00:00
return $products;
}
2020-04-06 16:17:17 +00:00
/**
* Get formed data from bundle option product
*
* @return array
*/
public function getProductOptions($product)
{
$products = [];
2020-06-08 10:40:53 +00:00
$products[$product->id] = [
'id' => $product->id,
'qty' => $product->qty,
'price' => $product->product->getTypeInstance()->getProductPrices(),
'name' => $product->product->name,
'product_id' => $product->product_id,
'is_default' => $product->is_default,
'sort_order' => $product->sort_order,
];
2020-04-06 16:17:17 +00:00
usort ($products, function($a, $b) {
if ($a['sort_order'] == $b['sort_order']) {
return 0;
}
return ($a['sort_order'] < $b['sort_order']) ? -1 : 1;
});
return $products;
}
2019-08-30 11:39:15 +00:00
}