Skip to content

Commit

Permalink
[FEATURE] #23 Add visible checkout attributes to frontend view (cart/…
Browse files Browse the repository at this point in the history
…checkout)
  • Loading branch information
therouv committed Jul 2, 2016
1 parent c51acb4 commit d5c729e
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright © 2015 FireGento e.V. - All rights reserved.
* See LICENSE.md bundled with this module for license details.
*/
namespace FireGento\MageSetup\Plugin\Catalog\Helper\Product\Configuration;

use \FireGento\MageSetup\Service\GetVisibleCheckoutAttributesServiceInterface;

/**
* Class AroundGetCustomOptionsPlugin
*
* @package FireGento\MageSetup\Plugin\Catalog\Helper\Product\Configuration
*/
class AroundGetCustomOptionsPlugin
{
/**
* @var GetVisibleCheckoutAttributesServiceInterface
*/
private $getVisibleCheckoutAttributesService;

/**
* AroundGetCustomOptionsPlugin constructor.
*
* @param GetVisibleCheckoutAttributesServiceInterface $getVisibleCheckoutAttributesService
*/
public function __construct(GetVisibleCheckoutAttributesServiceInterface $getVisibleCheckoutAttributesService)
{
$this->getVisibleCheckoutAttributesService = $getVisibleCheckoutAttributesService;
}

/**
* @param \Magento\Catalog\Helper\Product\Configuration $subject
* @param \Closure $proceed
* @param \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item
* @return array
*/
public function aroundGetCustomOptions(
\Magento\Catalog\Helper\Product\Configuration $subject,
\Closure $proceed,
\Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item
)
{
$options = $proceed($item);

$attributes = $this->getVisibleCheckoutAttributesService->execute();
if (count($attributes) > 0) {
foreach ($attributes as $attributeCode => $attributeLabel) {
$value = $item->getProduct()->getData($attributeCode);
if (!$value) {
continue;
}

$options[] = [
'label' => $attributeLabel,
'value' => $value,
'print_value' => $value
];
}
}

return $options;
}
}
51 changes: 51 additions & 0 deletions Plugin/Catalog/Model/Attribute/AroundGetAttributeNamesPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Copyright © 2015 FireGento e.V. - All rights reserved.
* See LICENSE.md bundled with this module for license details.
*/
namespace FireGento\MageSetup\Plugin\Catalog\Model\Attribute;

use \FireGento\MageSetup\Service\GetVisibleCheckoutAttributesServiceInterface;

/**
* Class AroundGetAttributeNamesPlugin
*
* @package FireGento\MageSetup\Plugin\Catalog\Model\Attribute
*/
class AroundGetAttributeNamesPlugin
{
/**
* @var GetVisibleCheckoutAttributesServiceInterface
*/
private $getVisibleCheckoutAttributesService;

/**
* AroundGetCustomOptionsPlugin constructor.
*
* @param GetVisibleCheckoutAttributesServiceInterface $getVisibleCheckoutAttributesService
*/
public function __construct(GetVisibleCheckoutAttributesServiceInterface $getVisibleCheckoutAttributesService)
{
$this->getVisibleCheckoutAttributesService = $getVisibleCheckoutAttributesService;
}

/**
* @param \Magento\Catalog\Model\Attribute\Config $subject
* @param \Closure $proceed
* @param string $groupName
* @return array
*/
public function aroundGetAttributeNames(\Magento\Catalog\Model\Attribute\Config $subject, \Closure $proceed, $groupName)
{
$attributeNames = $proceed($groupName);

if ($groupName == 'quote_item') {
$attributes = $this->getVisibleCheckoutAttributesService->execute();
foreach ($attributes as $attributeCode => $attributeLabel) {
$attributeNames[] = $attributeCode;
}
}

return $attributeNames;
}
}
57 changes: 57 additions & 0 deletions Service/GetVisibleCheckoutAttributesService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © 2015 FireGento e.V. - All rights reserved.
* See LICENSE.md bundled with this module for license details.
*/
namespace FireGento\MageSetup\Service;

/**
* Class GetVisibleCheckoutAttributesService
*
* @package FireGento\MageSetup\Service
*/
class GetVisibleCheckoutAttributesService implements GetVisibleCheckoutAttributesServiceInterface
{
/**
* @var \Magento\Catalog\Api\ProductAttributeRepository
*/
private $productAttributeRepository;
/**
* @var \Magento\Framework\Api\SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

/**
* GetVisibleCheckoutAttributesService constructor.
*
* @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $productAttributeRepository
* @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
*/
public function __construct(
\Magento\Catalog\Api\ProductAttributeRepositoryInterface $productAttributeRepository,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->productAttributeRepository = $productAttributeRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}

/**
* @return array|bool
*/
public function execute()
{
$searchCriteria = $this->searchCriteriaBuilder->addFilter('is_visible_on_checkout', 1)->create();
$attributes = $this->productAttributeRepository->getList($searchCriteria);

$options = [];
if (count($attributes->getItems()) > 0) {
foreach ($attributes->getItems() as $attribute) {
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $handle */

$options[$attribute->getAttributeCode()] = $attribute->getDefaultFrontendLabel();
}
}

return $options;
}
}
19 changes: 19 additions & 0 deletions Service/GetVisibleCheckoutAttributesServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Copyright © 2015 FireGento e.V. - All rights reserved.
* See LICENSE.md bundled with this module for license details.
*/
namespace FireGento\MageSetup\Service;

/**
* Interface GetVisibleCheckoutAttributesServiceInterface
*
* @package FireGento\MageSetup\Service
*/
interface GetVisibleCheckoutAttributesServiceInterface
{
/**
* @return array|bool
*/
public function execute();
}
2 changes: 2 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="FireGento\MageSetup\Service\GetVisibleCheckoutAttributesServiceInterface" type="FireGento\MageSetup\Service\GetVisibleCheckoutAttributesService"/>

<type name="FireGento\MageSetup\Model\Config\Reader">
<arguments>
<argument name="fileName" xsi:type="string">magesetup.xml</argument>
Expand Down
15 changes: 15 additions & 0 deletions etc/frontend/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 FireGento e.V. - All rights reserved.
* See LICENSE.md bundled with this module for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Helper\Product\Configuration">
<plugin name="mageSetupCustomOptions" type="FireGento\MageSetup\Plugin\Catalog\Helper\Product\Configuration\AroundGetCustomOptionsPlugin" sortOrder="1"/>
</type>
<type name="Magento\Catalog\Model\Attribute\Config">
<plugin name="mageSetupAddCustomQuoteItemAttributes" type="FireGento\MageSetup\Plugin\Catalog\Model\Attribute\AroundGetAttributeNamesPlugin" sortOrder="1"/>
</type>
</config>

0 comments on commit d5c729e

Please sign in to comment.