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

71 lines
2.3 KiB
PHP
Executable File

<?php
namespace Webkul\Product\Helpers;
class View extends AbstractProduct
{
/**
* Returns the visible custom attributes
*
* @param \Webkul\Product\Contracts\Product|\Webkul\Product\Contracts\ProductFlat $product
* @return void|array
*/
public function getAdditionalData($product)
{
$data = [];
$attributes = $product->attribute_family->custom_attributes()->where('attributes.is_visible_on_front', 1)->get();
$attributeOptionReposotory = app('Webkul\Attribute\Repositories\AttributeOptionRepository');
foreach ($attributes as $attribute) {
if ($product instanceof \Webkul\Product\Models\ProductFlat) {
$value = $product->product->{$attribute->code};
} else {
$value = $product->{$attribute->code};
}
if ($attribute->type == 'boolean') {
$value = $value ? 'Yes' : 'No';
} elseif($value) {
if ($attribute->type == 'select') {
$attributeOption = $attributeOptionReposotory->find($value);
if ($attributeOption) {
$value = $attributeOption->label ?? null;
if (! $value) {
continue;
}
}
} elseif (
$attribute->type == 'multiselect'
|| $attribute->type == 'checkbox'
) {
$labels = [];
$attributeOptions = $attributeOptionReposotory->findWhereIn('id', explode(",", $value));
foreach ($attributeOptions as $attributeOption) {
if ($label = $attributeOption->label) {
$labels[] = $label;
}
}
$value = implode(", ", $labels);
}
}
$data[] = [
'id' => $attribute->id,
'code' => $attribute->code,
'label' => $attribute->name,
'value' => $value,
'admin_name' => $attribute->admin_name,
'type' => $attribute->type,
];
}
return $data;
}
}