![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/extmag/shiplab/Model/Rule/Condition/Product/ |
<?php /** * Copyright © Extmag. All rights reserved. */ namespace Extmag\Shiplab\Model\Rule\Condition\Product; use Extmag\Shiplab\Model\Rule\Condition\Product; use Magento\Catalog\Model\Product\Type; use Magento\Framework\Model\AbstractModel; use Magento\Rule\Model\Condition\Context; /** * Subselect conditions for product. */ class Subselect extends Combine { /** * @param Context $context * @param Product $ruleConditionProduct * @param array $data */ public function __construct( Context $context, Product $ruleConditionProduct, array $data = [] ) { parent::__construct($context, $ruleConditionProduct, $data); $this->setType(Subselect::class)->setValue(null); } /** * Load array * * @param array $arr * @param string $key * * @return Subselect */ public function loadArray($arr, $key = 'conditions') { $this->setAttribute($arr['attribute']); $this->setOperator($arr['operator']); parent::loadArray($arr, $key); return $this; } /** * Return as xml * * @param string $containerKey * @param string $itemKey * * @return string */ public function asXml($containerKey = 'conditions', $itemKey = 'condition') { return '<attribute>' . $this->getAttribute() . '</attribute>' . '<operator>' . $this->getOperator() . '</operator>' . parent::asXml( $containerKey, $itemKey ); } /** * Load attribute options * * @return $this */ public function loadAttributeOptions() { $this->setAttributeOption(['qty' => __('total quantity'), 'base_row_total' => __('total amount')]); return $this; } /** * Load value options * * @return $this */ public function loadValueOptions() { return $this; } /** * Load operator options * * @return $this */ public function loadOperatorOptions() { $this->setOperatorOption( [ '==' => __('is'), '!=' => __('is not'), '>=' => __('equals or greater than'), '<=' => __('equals or less than'), '>' => __('greater than'), '<' => __('less than'), '()' => __('is one of'), '!()' => __('is not one of'), ] ); return $this; } /** * @return string */ public function getValueElementType() { return 'text'; } /** * Return as html * * @return string */ public function asHtml() { $html = $this->getTypeElement()->getHtml() . __( "If %1 %2 %3 for a subselection of items in cart matching %4 of these conditions:", $this->getAttributeElement()->getHtml(), $this->getOperatorElement()->getHtml(), $this->getValueElement()->getHtml(), $this->getAggregatorElement()->getHtml() ); if ($this->getId() != '1') { $html .= $this->getRemoveLinkHtml(); } return $html; } /** * Validate * * @param AbstractModel $model * * @return bool * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function validate(AbstractModel $model) { if (!$this->getConditions()) { return false; } $attr = $this->getAttribute(); $total = 0; $quote = $model->getQuote(); if (!$quote) { $quote = $model; } foreach ($quote->getAllVisibleItems() as $item) { $hasValidChild = false; $useChildrenTotal = ($item->getProductType() == Type::TYPE_BUNDLE); $childrenAttrTotal = 0; $children = $item->getChildren(); if (!empty($children)) { foreach ($children as $child) { if (parent::validate($child)) { $hasValidChild = true; if ($useChildrenTotal) { $childrenAttrTotal += $child->getData($attr); } } } } if ($hasValidChild || parent::validate($item)) { $total += ($hasValidChild && $useChildrenTotal) ? $childrenAttrTotal * $item->getQty() : $item->getData($attr); } } return $this->validateAttribute($total); } }