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

109 lines
2.7 KiB
PHP
Raw Normal View History

2019-08-30 11:39:15 +00:00
<?php
namespace Webkul\Product\Helpers;
/**
* Bundle Option Helper
*
* @author Jitendra Singh <jitendra@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class BundleOption extends AbstractProduct
{
/**
* Product
*
* @var Product
*/
protected $product;
/**
* Returns bundle option config
*
* @param Product $product
* @return array
*/
public function getBundleConfig($product)
{
$this->product = $product;
return [
'options' => $this->getOptions()
];
}
/**
* Returns bundle options
*
* @return array
*/
public function getOptions()
{
$options = [];
foreach ($this->product->bundle_options as $option) {
$options[$option->id] = $this->getOptionItemData($option);
}
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
*
* @param ProductBundleOption $option
* @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),
'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
*
2019-08-30 11:39:15 +00:00
* @param ProductBundleOption $option
* @return array
*/
private function getOptionProducts($option)
{
$products = [];
foreach ($option->bundle_option_products as $index => $bundleOptionProduct) {
$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,
'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;
}
}