Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take default value as base for compare and show it too #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions Plugin/ProductEavDataProviderPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace AvS\ScopeHint\Plugin;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Registry;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Catalog\Model\Attribute\ScopeOverriddenValue;

class ProductEavDataProviderPlugin
{
Expand Down Expand Up @@ -43,6 +45,7 @@ public function __construct(
* @param Eav $subject
* @param $result
* @return mixed
* @throws NoSuchEntityException
*/
public function afterSetupAttributeMeta(Eav $subject, $result)
{
Expand All @@ -56,42 +59,59 @@ public function afterSetupAttributeMeta(Eav $subject, $result)
$scopeHints = [];
$attributeCode = $result['arguments']['data']['config']['code'];
$storeViews = $this->getStores();
$product = $this->registry->registry('current_product');
$productId = $this->registry->registry('current_product')->getId();
$product = $this->getProductInStoreView($productId, 0);

foreach ($storeViews as $storeView) {
$productByStoreCode = $this->getProductInStoreView($product->getId(), $storeView->getId());
$currentScopeValueForCode = $value = $productByStoreCode->getData($attributeCode);

if ($result['arguments']['data']['config']['dataType'] == 'select'
&& !is_array($currentScopeValueForCode)
) {
$value = $productByStoreCode->getResource()->getAttribute($attributeCode)->getSource()->getOptionText($currentScopeValueForCode);
}

if ($product->getData($attributeCode) !== $currentScopeValueForCode) {
$scopeHints[] = $storeView->getName() . ': ' . $value;
$productByStoreCode = $this->getProductInStoreView($productId, $storeView->getId());

if ($product->getData($attributeCode) !== $productByStoreCode->getData($attributeCode)) {
$scopeHints[] = sprintf(
'%s: %s: %s',
$storeView->getWebsite()->getName(),
$storeView->getName(),
$this->getAttributeValue($product, $attributeCode)
);
}
}

if (!empty($scopeHints)) {
$result['arguments']['data']['config']['tooltip']['description'] = implode('<br>', $scopeHints);
}
if (!empty($scopeHints)) {
array_unshift($scopeHints, sprintf('%s: %s', __('Default'), $this->getAttributeValue($product, $attributeCode)));
$result['arguments']['data']['config']['tooltip']['description'] = implode("\n ", $scopeHints);
}

return $result;
}

/**
* @param ProductInterface $product
* @param string $attributeCode
* @return mixed
*/
private function getAttributeValue(ProductInterface $product, $attributeCode)
{
return $product
->getResource()
->getAttribute($attributeCode)
->getFrontend()
->getValue($product)
;
}


/**
* @param int $productId
* @param int $storeViewId
* @return mixed
* @throws NoSuchEntityException
*/
private function getProductInStoreView($productId, $storeViewId)
{
return $this->productRepository->getById($productId, false, $storeViewId);
}

/**
* @return array
* @return StoreInterface[]
*/
private function getStores()
{
Expand Down