<?php declare(strict_types=1);
namespace Uvala\uvalaBadgeManagerVsix\Subscriber;
use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\EntityAggregation;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Uvala\uvalaBadgeManagerVsix\uvalaBadgeManagerVsix;
class ProductListingSub implements EventSubscriberInterface {
public $uv_plugin_global_settings;
private $systemConfigService;
private $productRepository;
public function __construct(SystemConfigService $systemConfigService, EntityRepositoryInterface $productRepository) {
$this->systemConfigService = $systemConfigService;
$this->productRepository = $productRepository;
}
public static function getSubscribedEvents() {
return [
ProductSearchResultEvent::class => 'onProductSearchResult',
ProductListingResultEvent::class => 'onProductSearchResult',
ProductPageLoadedEvent::class => 'onProductPageLoaded'
];
}
public function onProductSearchResult(ProductListingResultEvent $event) {
global $uv_plugin_global_settings;
// load plugin settings for global use
$uv_saleschannel_id = $event->getSalesChannelContext()->getSalesChannel()->getId();
$this->uv_load_plugin_settings($uv_saleschannel_id);
$uv_current_currencyid = $event->getSalesChannelContext()->getSalesChannel()->get('currencyId');
$uv_allow_output_badges = 0;
$uv_allowed_products = [];
$uv_product_options_load = 0;
$uv_product_options_needed_array = [];
$uv_product_options_array = [];
$uv_product_properties_load = 0;
$uv_product_properties_needed_array = [];
$uv_product_properties_array = [];
$uv_plugin_setting_name = 'uvalaBadgeManagerVsix.config.';
// first run: check badges if anyone activated
for ($uvbgct = 1; $uvbgct<=8; $uvbgct++) {
if (isset($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstStatePart'.$uvbgct])) {
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstStatePart'.$uvbgct] == 'activated') {
$uv_allow_output_badges = 1;
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstVariantHandlingPart'.$uvbgct] != 'only_master') {
$uv_product_options_load = 1;
}
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstActivatedOnFuncPart'.$uvbgct] == 'prod_prop') {
$uv_product_properties_load = 1;
}
}
}
}
// second run: check the products with uvala_bdmg_plugin_disable if allow to run and options are needed
if ($uv_allow_output_badges == 1) {
foreach ($event->getResult() as $uvtmp_products) {
$uvtmpcheck_product_allowed = 0;
if (isset($uvtmp_products->get('customFields')['uvala_bdmg_plugin_disable'])) {
if ($uvtmp_products->get('customFields')['uvala_bdmg_plugin_disable'] == true) {
$uvtmpcheck_product_allowed = 0;
} else {
$uvtmpcheck_product_allowed = 1;
}
} else {
$uvtmpcheck_product_allowed = 1;
}
if ($uvtmpcheck_product_allowed == 1) {
$uv_allowed_products[] = $uvtmp_products;
if ($uv_product_options_load == 1) {
if ($uvtmp_products->get('parentId') != '') {
$uv_product_options_needed_array[$uvtmp_products->get('parentId')] = $uvtmp_products->get('parentId');
}
}
if ($uv_product_properties_load == 1) {
if (!empty($uvtmp_products->get('propertyIds'))) {
$uv_product_properties_needed_array[$uvtmp_products->get('id')] = $uvtmp_products->get('id');
}
}
}
}
}
// if allow to run the output?
if (($uv_allow_output_badges == 1) && (!empty($uv_allowed_products))) {
// load properties from products if needed
if (!empty($uv_product_properties_needed_array)) {
$search_prop_criteria = new Criteria();
$search_prop_criteria->addFilter(new EqualsAnyFilter('id', $uv_product_properties_needed_array),
new EqualsFilter('active', true))->addAssociations(['properties', 'properties.group']);
$uvresult_products_only_prop = $this->productRepository->search($search_prop_criteria, $event->getContext());
foreach ($uvresult_products_only_prop as $uvtmp_products_op) {
foreach ($uvtmp_products_op->get('properties') as $uvtmp_properties_groups) {
if (!isset($uv_product_properties_array[$uvtmp_products_op->get('id')]['properties_groups'][$uvtmp_properties_groups->get('groupId')])) {
$uv_product_properties_array[$uvtmp_products_op->get('id')]['properties_groups'][$uvtmp_properties_groups->get('groupId')] =
['name' => $uvtmp_properties_groups->get('group')->get('translated')['name'], 'excluded' => $this->addExcludedForPropertyGroup($uvtmp_properties_groups->getGroup()->getCustomFields())];
}
$uv_product_properties_array[$uvtmp_products_op->get('id')]['properties_groups'][$uvtmp_properties_groups->get('groupId')]['properties'][$uvtmp_properties_groups->get('id')] =
['name' => $uvtmp_properties_groups->get('translated')['name'], 'excluded' => $this->addExcludedForPropertyGroup($uvtmp_properties_groups->getGroup()->getCustomFields())];
}
}
}
// load options with properties from products if needed
if (!empty($uv_product_options_needed_array)) {
if ($uv_product_properties_load == 1) {
$uvtmp_load_associations = ['options', 'options.group', 'properties', 'properties.group'];
} else {
$uvtmp_load_associations = ['options', 'options.group'];
}
$search_options_criteria = new Criteria();
$search_options_criteria->addFilter(new EqualsAnyFilter('parentId', $uv_product_options_needed_array), new EqualsFilter('active', true))->addAssociations($uvtmp_load_associations);
$uvresult_products_only_options = $this->productRepository->search($search_options_criteria, $event->getContext());
foreach ($uvresult_products_only_options as $uvtmp_products_oo) {
$uvtmp_option_parentid = $uvtmp_products_oo->get('parentId');
if ($uv_product_properties_load == 1) {
foreach ($uvtmp_products_oo->get('properties') as $uvtmp_properties_groups) {
if (!$uvtmp_properties_groups->getGroup()->getCustomFields() || (array_key_exists('uvala_bdmg_property_group_excluded', (array)$uvtmp_properties_groups->getGroup()->getCustomFields()) && !$uvtmp_properties_groups->getGroup()->getCustomFields()['uvala_bdmg_property_group_excluded']) ) {
if (!isset($uv_product_options_array[$uvtmp_option_parentid]['properties_groups'][$uvtmp_properties_groups->get('groupId')])) {
$uv_product_properties_array[$uvtmp_products_op->get('id')]['properties_groups'][$uvtmp_properties_groups->get('groupId')] =
['name' => $uvtmp_properties_groups->get('group')->get('translated')['name'], 'excluded' => $this->addExcludedForPropertyGroup($uvtmp_properties_groups->getGroup()->getCustomFields())];
}
$uv_product_properties_array[$uvtmp_products_op->get('id')]['properties_groups'][$uvtmp_properties_groups->get('groupId')]['properties'][$uvtmp_properties_groups->get('id')] =
['name' => $uvtmp_properties_groups->get('translated')['name'], 'excluded' => $this->addExcludedForPropertyGroup($uvtmp_properties_groups->getGroup()->getCustomFields())];
}
}
}
foreach ($uvtmp_products_oo->get('options') as $uvtmp_option_groups) {
if (!isset($uv_product_options_array[$uvtmp_option_parentid]['option_groups'][$uvtmp_option_groups->get('groupId')])) {
$uv_product_options_array[$uvtmp_option_parentid]['option_groups'][$uvtmp_option_groups->get('groupId')] = ['name' => $uvtmp_option_groups->get('group')->get('translated')['name']];
}
$uv_product_options_array[$uvtmp_option_parentid]['option_groups'][$uvtmp_option_groups->get('groupId')]['options'][$uvtmp_option_groups->get('id')] = ['name' => $uvtmp_option_groups->get('translated')['name']];
}
$uvtmp_option_stock = $uvtmp_products_oo->get('availableStock');
if (isset($uv_product_options_array[$uvtmp_option_parentid]['stock_calc'])) {
$uv_product_options_array[$uvtmp_option_parentid]['stock_calc'] = ($uv_product_options_array[$uvtmp_option_parentid]['stock_calc']+$uvtmp_option_stock);
if ($uvtmp_option_stock < $uv_product_options_array[$uvtmp_option_parentid]['stock_min']) {
$uv_product_options_array[$uvtmp_option_parentid]['stock_min'] = $uvtmp_option_stock;
}
if ($uvtmp_option_stock > $uv_product_options_array[$uvtmp_option_parentid]['stock_max']) {
$uv_product_options_array[$uvtmp_option_parentid]['stock_max'] = $uvtmp_option_stock;
}
} else {
$uv_product_options_array[$uvtmp_option_parentid]['stock_calc'] = $uvtmp_option_stock;
$uv_product_options_array[$uvtmp_option_parentid]['stock_min'] = $uvtmp_option_stock;
$uv_product_options_array[$uvtmp_option_parentid]['stock_max'] = $uvtmp_option_stock;
}
$uvtmp_discount_val = '0';
$uvtmp_discount_checking = $uvtmp_products_oo->getPrice()->getElements()[$uv_current_currencyid];
if ($uvtmp_discount_checking->getListPrice() != '') {
if ($uvtmp_discount_checking->getListPrice()->getGross() != '0') {
$uvtmp_discount_val = round(((($uvtmp_discount_checking->getListPrice()->getGross())-($uvtmp_discount_checking->getGross()))/($uvtmp_discount_checking->getListPrice()->getGross()))*100, 2);
}
}
if (isset($uv_product_options_array[$uvtmp_option_parentid]['discount_calc'])) {
$uv_product_options_array[$uvtmp_option_parentid]['discount_calc'] = ($uv_product_options_array[$uvtmp_option_parentid]['discount_calc']+$uvtmp_discount_val);
if ($uvtmp_discount_val < $uv_product_options_array[$uvtmp_option_parentid]['discount_min']) {
$uv_product_options_array[$uvtmp_option_parentid]['discount_min'] = $uvtmp_discount_val;
}
if ($uvtmp_discount_val > $uv_product_options_array[$uvtmp_option_parentid]['discount_max']) {
$uv_product_options_array[$uvtmp_option_parentid]['discount_max'] = $uvtmp_discount_val;
}
} else {
$uv_product_options_array[$uvtmp_option_parentid]['discount_calc'] = $uvtmp_discount_val;
$uv_product_options_array[$uvtmp_option_parentid]['discount_min'] = $uvtmp_discount_val;
$uv_product_options_array[$uvtmp_option_parentid]['discount_max'] = $uvtmp_discount_val;
}
$uvtmp_deliverytime_min = '0';
$uvtmp_deliverytime_max = '0';
if ($uvtmp_products_oo->get('deliveryTime') != '') {
$uvtmp_deliverytime_min = $this->uv_recalculate_delivery_time($uvtmp_products_oo->get('deliveryTime')->get('min'), $uvtmp_products_oo->get('deliveryTime')->get('unit'));
$uvtmp_deliverytime_max = $this->uv_recalculate_delivery_time($uvtmp_products_oo->get('deliveryTime')->get('max'), $uvtmp_products_oo->get('deliveryTime')->get('unit'));
}
if (isset($uv_product_options_array[$uvtmp_option_parentid]['delivery_min'])) {
if ($uvtmp_deliverytime_min < $uv_product_options_array[$uvtmp_option_parentid]['delivery_min']) {
$uv_product_options_array[$uvtmp_option_parentid]['delivery_min'] = $uvtmp_deliverytime_min;
}
if ($uvtmp_deliverytime_max > $uv_product_options_array[$uvtmp_option_parentid]['delivery_max']) {
$uv_product_options_array[$uvtmp_option_parentid]['delivery_max'] = $uvtmp_deliverytime_max;
}
} else {
$uv_product_options_array[$uvtmp_option_parentid]['delivery_min'] = $uvtmp_deliverytime_min;
$uv_product_options_array[$uvtmp_option_parentid]['delivery_max'] = $uvtmp_deliverytime_max;
}
if (isset($uv_product_options_array[$uvtmp_option_parentid]['shippingfree_check'])) {
if ($uvtmp_products_oo->get('shippingFree') == '1') {
$uv_product_options_array[$uvtmp_option_parentid]['shippingfree_check'] = $uvtmp_products_oo->get('shippingFree');
}
} else {
$uv_product_options_array[$uvtmp_option_parentid]['shippingfree_check'] = $uvtmp_products_oo->get('shippingFree');
}
}
}
// last run: collect data and write output to product
foreach($uv_allowed_products as $uvtmp_products) {
$uv_product_data_array = [];
$uvtmp_variants_data = '';
if ($uv_product_options_load == 1) {
if (isset($uv_product_options_array[$uvtmp_products->get('parentId')])) {
$uvtmp_variants_data = $uv_product_options_array[$uvtmp_products->get('parentId')];
}
}
$uvtmp_properties_data = '';
if ($uv_product_properties_load == 1) {
if (isset($uv_product_properties_array[$uvtmp_products->get('id')])) {
$uvtmp_properties_data = $uv_product_properties_array[$uvtmp_products->get('id')];
}
}
$uvtmp_discount_value = '0';
if ($uvtmp_products->get('calculatedPrice')->getListPrice() != '') {
$uvtmp_discount_value = $uvtmp_products->get('calculatedPrice')->getListPrice()->getPercentage();
}
$uvtmp_delivery_time_min = '0';
$uvtmp_delivery_time_max = '0';
if ($uvtmp_products->get('deliveryTime') != '') {
$uvtmp_delivery_time_min = $this->uv_recalculate_delivery_time($uvtmp_products->get('deliveryTime')->get('min'), $uvtmp_products->get('deliveryTime')->get('unit'));
$uvtmp_delivery_time_max = $this->uv_recalculate_delivery_time($uvtmp_products->get('deliveryTime')->get('max'), $uvtmp_products->get('deliveryTime')->get('unit'));
}
$uv_product_data_array = ['variants' => $uvtmp_variants_data,
'properties' => $uvtmp_properties_data,
'stock_current' => $uvtmp_products->get('availableStock'),
'discount_value' => $uvtmp_discount_value,
'delivery_time_min' => $uvtmp_delivery_time_min,
'delivery_time_max' => $uvtmp_delivery_time_max,
'shippingfree_check' => $uvtmp_products->get('shippingFree'),
'topseller_check' => $uvtmp_products->get('markAsTopseller'),
'newproduct_check' => $uvtmp_products->get('isNew')
];
$uvtmp_products->addExtension('badge_manager', new ArrayEntity(['badges_array' => $this->uv_badge_manager_output($uv_product_data_array)]));
}
}
}
public function onProductPageLoaded(ProductPageLoadedEvent $event) {
global $uv_plugin_global_settings;
// load plugin settings for global use
$uv_saleschannel_id = $event->getSalesChannelContext()->getSalesChannel()->getId();
$this->uv_load_plugin_settings($uv_saleschannel_id);
$uv_current_page = $event->getPage();
$uv_current_product = $uv_current_page->getProduct();
$uv_current_currencyid = $event->getSalesChannelContext()->getSalesChannel()->get('currencyId');
$uv_allow_output_badges = 0;
$uv_allow_output_product = 0;
$uv_product_options_load = 0;
$uv_product_options_needed_load = '';
$uv_product_options_array = [];
$uv_product_properties_load = 0;
$uv_product_properties_array = [];
$uv_plugin_setting_name = 'uvalaBadgeManagerVsix.config.';
// first run: check badges if anyone activated
for ($uvbgct = 1; $uvbgct<=8; $uvbgct++) {
if (isset($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstStatePart'.$uvbgct])) {
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstStatePart'.$uvbgct] == 'activated') {
$uv_allow_output_badges = 1;
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstVariantHandlingPart'.$uvbgct] != 'only_master') {
$uv_product_options_load = 1;
}
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstActivatedOnFuncPart'.$uvbgct] == 'prod_prop') {
$uv_product_properties_load = 1;
}
}
}
}
// second run: check the product with uvala_bdmg_plugin_disable if allow to run and options are needed
if ($uv_allow_output_badges == 1) {
$uvtmpcheck_product_allowed = 0;
if (isset($uv_current_product->get('customFields')['uvala_bdmg_plugin_disable'])) {
if ($uv_current_product->get('customFields')['uvala_bdmg_plugin_disable'] == true) {
$uvtmpcheck_product_allowed = 0;
} else {
$uvtmpcheck_product_allowed = 1;
}
} else {
$uvtmpcheck_product_allowed = 1;
}
if ($uvtmpcheck_product_allowed == 1) {
$uv_allow_output_product = 1;
if ($uv_product_options_load == 1) {
if ($uv_current_product->get('parentId') != '') {
$uv_product_options_needed_load = $uv_current_product->get('parentId');
}
}
if ($uv_product_properties_load == 1) {
if (!empty($uv_current_product->get('propertyIds'))) {
foreach ($uv_current_product->getSortedProperties()->getElements() as $uvtmp_properties_groups) {
$uv_product_properties_array['properties_groups'][$uvtmp_properties_groups->get('id')] = ['name' => $uvtmp_properties_groups->get('translated')['name'], 'excluded' => $this->addExcludedForPropertyGroup($uvtmp_properties_groups->getCustomFields())];
foreach ($uvtmp_properties_groups->getOptions()->getElements() as $uvtmp_properties_groups_values) {
$uv_product_properties_array['properties_groups'][$uvtmp_properties_groups->get('id')]['properties'][$uvtmp_properties_groups_values->get('id')] = ['name' => $uvtmp_properties_groups_values->get('translated')['name']];
}
}
}
}
}
}
// if allow to run the output?
if (($uv_allow_output_badges == 1) && ($uv_allow_output_product == 1)) {
// load options with properties from product if needed
if (($uv_product_options_needed_load != '') && ($uv_product_options_load == 1)) {
if ($uv_product_properties_load == 1) {
$uvtmp_load_associations = ['options', 'options.group', 'properties', 'properties.group'];
} else {
$uvtmp_load_associations = ['options', 'options.group'];
}
$search_options_criteria = new Criteria();
$search_options_criteria->addFilter(new EqualsFilter('parentId', $uv_product_options_needed_load), new EqualsFilter('active', true))->addAssociations($uvtmp_load_associations);
$uvresult_products_only_options = $this->productRepository->search($search_options_criteria, $event->getContext());
foreach ($uvresult_products_only_options->getEntities()->getElements() as $uvtmp_products_oo) {
$uvtmp_option_parentid = $uvtmp_products_oo->get('parentId');
if ($uv_product_properties_load == 1) {
foreach ($uvtmp_products_oo->get('properties') as $uvtmp_properties_groups) {
if (!isset($uv_product_options_array[$uvtmp_option_parentid]['properties_groups'][$uvtmp_properties_groups->get('groupId')])) {
$uv_product_options_array[$uvtmp_option_parentid]['properties_groups'][$uvtmp_properties_groups->get('groupId')] = ['name' => $uvtmp_properties_groups->get('group')->get('translated')['name'], 'excluded' => $this->addExcludedForPropertyGroup($uvtmp_properties_groups->getGroup()->getCustomFields())];
}
$uv_product_options_array[$uvtmp_option_parentid]['properties_groups'][$uvtmp_properties_groups->get('groupId')]['properties'][$uvtmp_properties_groups->get('id')] = ['name' => $uvtmp_properties_groups->get('translated')['name'], 'excluded' => $this->addExcludedForPropertyGroup($uvtmp_properties_groups->getGroup()->getCustomFields())];
}
}
foreach ($uvtmp_products_oo->get('options') as $uvtmp_option_groups) {
if (!isset($uv_product_options_array[$uvtmp_option_parentid]['option_groups'][$uvtmp_option_groups->get('groupId')])) {
$uv_product_options_array[$uvtmp_option_parentid]['option_groups'][$uvtmp_option_groups->get('groupId')] = ['name' => $uvtmp_option_groups->get('group')->get('translated')['name']];
}
$uv_product_options_array[$uvtmp_option_parentid]['option_groups'][$uvtmp_option_groups->get('groupId')]['options'][$uvtmp_option_groups->get('id')] = ['name' => $uvtmp_option_groups->get('translated')['name']];
}
$uvtmp_option_stock = $uvtmp_products_oo->get('availableStock');
if (isset($uv_product_options_array[$uvtmp_option_parentid]['stock_calc'])) {
$uv_product_options_array[$uvtmp_option_parentid]['stock_calc'] = ($uv_product_options_array[$uvtmp_option_parentid]['stock_calc']+$uvtmp_option_stock);
if ($uvtmp_option_stock < $uv_product_options_array[$uvtmp_option_parentid]['stock_min']) {
$uv_product_options_array[$uvtmp_option_parentid]['stock_min'] = $uvtmp_option_stock;
}
if ($uvtmp_option_stock > $uv_product_options_array[$uvtmp_option_parentid]['stock_max']) {
$uv_product_options_array[$uvtmp_option_parentid]['stock_max'] = $uvtmp_option_stock;
}
} else {
$uv_product_options_array[$uvtmp_option_parentid]['stock_calc'] = $uvtmp_option_stock;
$uv_product_options_array[$uvtmp_option_parentid]['stock_min'] = $uvtmp_option_stock;
$uv_product_options_array[$uvtmp_option_parentid]['stock_max'] = $uvtmp_option_stock;
}
$uvtmp_discount_val = '0';
$uvtmp_discount_checking = $uvtmp_products_oo->getPrice()->getElements()[$uv_current_currencyid];
if ($uvtmp_discount_checking->getListPrice() != '') {
if ($uvtmp_discount_checking->getListPrice()->getGross() != '0') {
$uvtmp_discount_val = round(((($uvtmp_discount_checking->getListPrice()->getGross())-($uvtmp_discount_checking->getGross()))/($uvtmp_discount_checking->getListPrice()->getGross()))*100, 2);
}
}
if (isset($uv_product_options_array[$uvtmp_option_parentid]['discount_calc'])) {
$uv_product_options_array[$uvtmp_option_parentid]['discount_calc'] = ($uv_product_options_array[$uvtmp_option_parentid]['discount_calc']+$uvtmp_discount_val);
if ($uvtmp_discount_val < $uv_product_options_array[$uvtmp_option_parentid]['discount_min']) {
$uv_product_options_array[$uvtmp_option_parentid]['discount_min'] = $uvtmp_discount_val;
}
if ($uvtmp_discount_val > $uv_product_options_array[$uvtmp_option_parentid]['discount_max']) {
$uv_product_options_array[$uvtmp_option_parentid]['discount_max'] = $uvtmp_discount_val;
}
} else {
$uv_product_options_array[$uvtmp_option_parentid]['discount_calc'] = $uvtmp_discount_val;
$uv_product_options_array[$uvtmp_option_parentid]['discount_min'] = $uvtmp_discount_val;
$uv_product_options_array[$uvtmp_option_parentid]['discount_max'] = $uvtmp_discount_val;
}
$uvtmp_deliverytime_min = '0';
$uvtmp_deliverytime_max = '0';
if ($uvtmp_products_oo->get('deliveryTime') != '') {
$uvtmp_deliverytime_min = $this->uv_recalculate_delivery_time($uvtmp_products_oo->get('deliveryTime')->get('min'), $uvtmp_products_oo->get('deliveryTime')->get('unit'));
$uvtmp_deliverytime_max = $this->uv_recalculate_delivery_time($uvtmp_products_oo->get('deliveryTime')->get('max'), $uvtmp_products_oo->get('deliveryTime')->get('unit'));
}
if (isset($uv_product_options_array[$uvtmp_option_parentid]['delivery_min'])) {
if ($uvtmp_deliverytime_min < $uv_product_options_array[$uvtmp_option_parentid]['delivery_min']) {
$uv_product_options_array[$uvtmp_option_parentid]['delivery_min'] = $uvtmp_deliverytime_min;
}
if ($uvtmp_deliverytime_max > $uv_product_options_array[$uvtmp_option_parentid]['delivery_max']) {
$uv_product_options_array[$uvtmp_option_parentid]['delivery_max'] = $uvtmp_deliverytime_max;
}
} else {
$uv_product_options_array[$uvtmp_option_parentid]['delivery_min'] = $uvtmp_deliverytime_min;
$uv_product_options_array[$uvtmp_option_parentid]['delivery_max'] = $uvtmp_deliverytime_max;
}
if (isset($uv_product_options_array[$uvtmp_option_parentid]['shippingfree_check'])) {
if ($uvtmp_products_oo->get('shippingFree') == '1') {
$uv_product_options_array[$uvtmp_option_parentid]['shippingfree_check'] = $uvtmp_products_oo->get('shippingFree');
}
} else {
$uv_product_options_array[$uvtmp_option_parentid]['shippingfree_check'] = $uvtmp_products_oo->get('shippingFree');
}
}
}
// last run: collect data and write output to product
$uv_product_data_array = [];
$uvtmp_variants_data = '';
if ($uv_product_options_load == 1) {
if (isset($uv_product_options_array[$uv_current_product->get('parentId')])) {
$uvtmp_variants_data = $uv_product_options_array[$uv_current_product->get('parentId')];
}
}
$uvtmp_properties_data = '';
if ($uv_product_properties_load == 1) {
if (!empty($uv_product_properties_array)) {
$uvtmp_properties_data = $uv_product_properties_array;
}
}
$uvtmp_discount_value = '0';
if ($uv_current_product->get('calculatedPrice')->getListPrice() != '') {
$uvtmp_discount_value = $uv_current_product->get('calculatedPrice')->getListPrice()->getPercentage();
}
$uvtmp_delivery_time_min = '0';
$uvtmp_delivery_time_max = '0';
if ($uv_current_product->get('deliveryTime') != '') {
$uvtmp_delivery_time_min = $this->uv_recalculate_delivery_time($uv_current_product->get('deliveryTime')->get('min'), $uv_current_product->get('deliveryTime')->get('unit'));
$uvtmp_delivery_time_max = $this->uv_recalculate_delivery_time($uv_current_product->get('deliveryTime')->get('max'), $uv_current_product->get('deliveryTime')->get('unit'));
}
$uv_product_data_array = ['variants' => $uvtmp_variants_data,
'properties' => $uvtmp_properties_data,
'stock_current' => $uv_current_product->get('availableStock'),
'discount_value' => $uvtmp_discount_value,
'delivery_time_min' => $uvtmp_delivery_time_min,
'delivery_time_max' => $uvtmp_delivery_time_max,
'shippingfree_check' => $uv_current_product->get('shippingFree'),
'topseller_check' => $uv_current_product->get('markAsTopseller'),
'newproduct_check' => $uv_current_product->get('isNew')
];
$uv_current_page->addExtension('badge_manager', new ArrayEntity(['badges_array' => $this->uv_badge_manager_output($uv_product_data_array)]));
}
}
public function uv_badge_manager_output($uv_product_data) {
global $uv_plugin_global_settings;
$uvoutput_array = [];
$uv_plugin_setting_name = 'uvalaBadgeManagerVsix.config.';
for ($uvbgct = 1; $uvbgct<=8; $uvbgct++) {
if (isset($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstStatePart'.$uvbgct])) {
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstStatePart'.$uvbgct] == 'activated') {
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstActivatedOnFuncPart'.$uvbgct] == 'prod_prop') {
// Only deal with product properties
$uvtmp_prop_array = [];
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstVariantHandlingPart'.$uvbgct] == 'only_master') {
if (isset($uv_product_data['properties']['properties_groups'])) {
if (!empty($uv_product_data['properties']['properties_groups'])) {
$uvtmp_prop_array = $uv_product_data['properties']['properties_groups'];
}
}
} else {
if (isset($uv_product_data['variants']['properties_groups'])) {
if (!empty($uv_product_data['variants']['properties_groups'])) {
$uvtmp_prop_array = $uv_product_data['variants']['properties_groups'];
}
} else {
if (isset($uv_product_data['properties']['properties_groups'])) {
if (!empty($uv_product_data['properties']['properties_groups'])) {
$uvtmp_prop_array = $uv_product_data['properties']['properties_groups'];
}
}
}
}
if (!empty($uvtmp_prop_array)) {
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstOutputFormating'.$uvbgct] == 'each_options') {
foreach ($uvtmp_prop_array as $uvtmp_prop) {
if (!array_key_exists('excluded', $uvtmp_prop) || !$uvtmp_prop['excluded'] || $uvtmp_prop['excluded'][$uvbgct] == false) {
foreach ($uvtmp_prop['properties'] as $uvtmp_value) {
$uvoutput_array[] = ['bg_nr' => $uvbgct,
'bg_sorting' => $uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstSorting'.$uvbgct],
'bg_replace_initialval' => '',
'bg_replace_prop' => $uvtmp_prop['name'],
'bg_replace_prop_val' => $uvtmp_value['name'],
'bg_css' => $uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstBadgeCSSAddendCode'.$uvbgct],
'bg_color' => $uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstColorPart'.$uvbgct],
'bg_background' => $uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstBackgroundPart'.$uvbgct]
];
}
}
}
} else {
foreach ($uvtmp_prop_array as $uvtmp_prop) {
$uvtmp_bgpos_property_string = '';
if (!array_key_exists('excluded', $uvtmp_prop) || !$uvtmp_prop['excluded'] || $uvtmp_prop['excluded'][$uvbgct] == false) {
foreach ($uvtmp_prop['properties'] as $uvtmp_value) {
$uvtmp_bgpos_property_string .= $uvtmp_value['name'] . ', ';
}
$uvoutput_array[] = ['bg_nr' => $uvbgct,
'bg_sorting' => $uv_plugin_global_settings[$uv_plugin_setting_name . 'uvbdmgrstSorting' . $uvbgct],
'bg_replace_initialval' => '',
'bg_replace_prop' => $uvtmp_prop['name'],
'bg_replace_prop_val' => rtrim($uvtmp_bgpos_property_string, ', '),
'bg_css' => $uv_plugin_global_settings[$uv_plugin_setting_name . 'uvbdmgrstBadgeCSSAddendCode' . $uvbgct],
'bg_color' => $uv_plugin_global_settings[$uv_plugin_setting_name . 'uvbdmgrstColorPart' . $uvbgct],
'bg_background' => $uv_plugin_global_settings[$uv_plugin_setting_name . 'uvbdmgrstBackgroundPart' . $uvbgct],
];
}
}
}
}
} else {
// Deal with output values
$uvtmp_bgpos_initial_value = 0;
$uvtmp_bgpos_initial_value_max = 0;
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstActivatedOnFuncPart'.$uvbgct] == 'stock') {
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstVariantHandlingPart'.$uvbgct] == 'only_master') {
$uvtmp_bgpos_initial_value = $uv_product_data['stock_current'];
} elseif ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstVariantHandlingPart'.$uvbgct] == 'biggest') {
if (isset($uv_product_data['variants']['stock_max'])) {
$uvtmp_bgpos_initial_value = $uv_product_data['variants']['stock_max'];
} else {
$uvtmp_bgpos_initial_value = $uv_product_data['stock_current'];
}
} elseif ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstVariantHandlingPart'.$uvbgct] == 'smallest') {
if (isset($uv_product_data['variants']['stock_min'])) {
$uvtmp_bgpos_initial_value = $uv_product_data['variants']['stock_min'];
} else {
$uvtmp_bgpos_initial_value = $uv_product_data['stock_current'];
}
} elseif ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstVariantHandlingPart'.$uvbgct] == 'combine') {
if (isset($uv_product_data['variants']['stock_calc'])) {
$uvtmp_bgpos_initial_value = $uv_product_data['variants']['stock_calc'];
} else {
$uvtmp_bgpos_initial_value = $uv_product_data['stock_current'];
}
}
} elseif ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstActivatedOnFuncPart'.$uvbgct] == 'discount') {
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstVariantHandlingPart'.$uvbgct] == 'only_master') {
$uvtmp_bgpos_initial_value = $uv_product_data['discount_value'];
} elseif ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstVariantHandlingPart'.$uvbgct] == 'biggest') {
if (isset($uv_product_data['variants']['discount_max'])) {
$uvtmp_bgpos_initial_value = $uv_product_data['variants']['discount_max'];
} else {
$uvtmp_bgpos_initial_value = $uv_product_data['discount_value'];
}
} elseif ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstVariantHandlingPart'.$uvbgct] == 'smallest') {
if (isset($uv_product_data['variants']['discount_min'])) {
$uvtmp_bgpos_initial_value = $uv_product_data['variants']['discount_min'];
} else {
$uvtmp_bgpos_initial_value = $uv_product_data['discount_value'];
}
} elseif ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstVariantHandlingPart'.$uvbgct] == 'combine') {
if (isset($uv_product_data['variants']['discount_calc'])) {
$uvtmp_bgpos_initial_value = $uv_product_data['variants']['discount_calc'];
} else {
$uvtmp_bgpos_initial_value = $uv_product_data['discount_value'];
}
}
} elseif ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstActivatedOnFuncPart'.$uvbgct] == 'delivery') {
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstVariantHandlingPart'.$uvbgct] == 'only_master') {
$uvtmp_bgpos_initial_value = $uv_product_data['delivery_time_min'];
$uvtmp_bgpos_initial_value_max = $uv_product_data['delivery_time_max'];
} else {
if (isset($uv_product_data['variants']['delivery_max']) && isset($uv_product_data['variants']['delivery_min'])) {
if (($uv_product_data['variants']['delivery_max'] == '0') && ($uv_product_data['variants']['delivery_min'] == '0')) {
$uvtmp_bgpos_initial_value = $uv_product_data['delivery_time_min'];
$uvtmp_bgpos_initial_value_max = $uv_product_data['delivery_time_max'];
} else {
$uvtmp_bgpos_initial_value = $uv_product_data['variants']['delivery_min'];
$uvtmp_bgpos_initial_value_max = $uv_product_data['variants']['delivery_max'];
}
} else {
$uvtmp_bgpos_initial_value = $uv_product_data['delivery_time_min'];
$uvtmp_bgpos_initial_value_max = $uv_product_data['delivery_time_max'];
}
}
} elseif ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstActivatedOnFuncPart'.$uvbgct] == 'check_freeshipping') {
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstVariantHandlingPart'.$uvbgct] == 'only_master') {
if ($uv_product_data['shippingfree_check'] == '1') {
$uvtmp_bgpos_initial_value = 1;
}
} else {
if (isset($uv_product_data['variants']['shippingfree_check'])) {
if ($uv_product_data['variants']['shippingfree_check'] == '1') {
$uvtmp_bgpos_initial_value = 1;
}
}
}
} elseif ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstActivatedOnFuncPart'.$uvbgct] == 'check_variants') {
if (!empty($uv_product_data['variants'])) {
$uvtmp_bgpos_initial_value = 1;
}
} elseif ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstActivatedOnFuncPart'.$uvbgct] == 'check_topseller') {
if ($uv_product_data['topseller_check'] == '1') {
$uvtmp_bgpos_initial_value = 1;
}
} elseif ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstActivatedOnFuncPart'.$uvbgct] == 'check_new') {
if ($uv_product_data['newproduct_check'] == '1') {
$uvtmp_bgpos_initial_value = 1;
}
}
// done, check if allow to output
$uvtmp_badge_activate = 0;
$uvtmp_reference_check = $uvtmp_bgpos_initial_value;
$uvtmp_reference_check_max = $uvtmp_bgpos_initial_value;
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstActivatedOnFuncPart'.$uvbgct] == 'delivery') {
$uvtmp_reference_check = $uvtmp_bgpos_initial_value;
$uvtmp_reference_check_max = $uvtmp_bgpos_initial_value_max;
}
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstOperatorPart'.$uvbgct] == 'equal') {
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstValuePart'.$uvbgct] == $uvtmp_reference_check) {
$uvtmp_badge_activate = 1;
}
} elseif ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstOperatorPart'.$uvbgct] == 'smaller') {
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstValuePart'.$uvbgct] > $uvtmp_reference_check) {
$uvtmp_badge_activate = 1;
}
} elseif ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstOperatorPart'.$uvbgct] == 'bigger') {
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstValuePart'.$uvbgct] < $uvtmp_reference_check) {
$uvtmp_badge_activate = 1;
}
} elseif ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstOperatorPart'.$uvbgct] == 'reference') {
if (($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstValuePart'.$uvbgct] < $uvtmp_reference_check) && ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstMaxValuePart'.$uvbgct] > $uvtmp_reference_check_max)) {
$uvtmp_badge_activate = 1;
}
}
if ($uvtmp_badge_activate == 1) {
if ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstOutputFormating'.$uvbgct] == 'cut_decimal') {
$uvtmp_bgpos_initial_value = explode('.', strval($uvtmp_bgpos_initial_value));
$uvtmp_bgpos_initial_value = $uvtmp_bgpos_initial_value[0];
} elseif ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstOutputFormating'.$uvbgct] == 'round_up') {
$uvtmp_bgpos_initial_value = number_format($uvtmp_bgpos_initial_value, 0);
} elseif ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstOutputFormating'.$uvbgct] == 'round_two') {
if (strpos(strval($uvtmp_bgpos_initial_value), '.') === false) {
} else {
$uvtmp_bgpos_initial_value = number_format($uvtmp_bgpos_initial_value, 2, ',', '');
}
} elseif ($uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstOutputFormating'.$uvbgct] == 'round_one') {
if (strpos(strval($uvtmp_bgpos_initial_value), '.') === false) {
} else {
$uvtmp_bgpos_initial_value = number_format($uvtmp_bgpos_initial_value, 1, ',', '');
}
} else {
$uvtmp_bgpos_initial_value = str_replace('.', ',', $uvtmp_bgpos_initial_value);
}
$uvoutput_array[] = ['bg_nr' => $uvbgct,
'bg_sorting' => $uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstSorting'.$uvbgct],
'bg_replace_initialval' => $uvtmp_bgpos_initial_value,
'bg_replace_prop' => '',
'bg_replace_prop_val' => '',
'bg_css' => $uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstBadgeCSSAddendCode'.$uvbgct],
'bg_color' => $uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstColorPart'.$uvbgct],
'bg_background' => $uv_plugin_global_settings[$uv_plugin_setting_name.'uvbdmgrstBackgroundPart'.$uvbgct]
];
}
}
}
}
}
if (count($uvoutput_array) > 0) {
$uvtmp_array_position = array();
foreach ($uvoutput_array as $key => $row) {
$uvtmp_array_position[$key] = $row['bg_sorting'];
}
array_multisort($uvtmp_array_position, SORT_ASC, $uvoutput_array);
}
return $uvoutput_array;
}
public function uv_load_plugin_settings($uv_saleschannel_id) {
global $uv_plugin_global_settings;
$uv_settings = $this->systemConfigService->getDomain('uvalaBadgeManagerVsix');
$uv_settings_saleschannel = $this->systemConfigService->getDomain('uvalaBadgeManagerVsix', $uv_saleschannel_id);
$uv_plugin_global_settings = array_merge($uv_settings, $uv_settings_saleschannel);
return true;
}
private function addExcludedForPropertyGroup($customFields) {
$filtered = [];
if ($customFields) {
foreach($customFields as $key => $val) {
if(false !== strpos($key, uvalaBadgeManagerVsix::PROPERTY_GROUP_EXCLUDED_CF_NAME)) {
$filtered[substr($key, -1)] = $val;
}
}
}
return $filtered;
}
public function uv_recalculate_delivery_time($uv_delivery_time, $uv_delivery_unit) {
$uv_output_calc = 0;
if ($uv_delivery_unit == 'week') {
$uv_output_calc = ($uv_delivery_time*7);
} elseif ($uv_delivery_unit == 'month') {
$uv_output_calc = ($uv_delivery_time*30);
} elseif ($uv_delivery_unit == 'year') {
$uv_output_calc = ($uv_delivery_time*365);
} else {
// unit: day
$uv_output_calc = $uv_delivery_time;
}
return $uv_output_calc;
}
}