Skip to content

Commit

Permalink
Merge pull request #231 from magento-firedrakes/MAGETWO-46472
Browse files Browse the repository at this point in the history
[Firedrakes] Sprint 53
  • Loading branch information
mlogvin committed Dec 22, 2015
2 parents 77988b6 + c78a700 commit 9b08b0b
Show file tree
Hide file tree
Showing 454 changed files with 17,810 additions and 7,395 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ class AdvancedPricing extends \Magento\CatalogImportExport\Model\Export\Product
* @param \Magento\CatalogImportExport\Model\Export\Product\Type\Factory $_typeFactory
* @param \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider
* @param \Magento\CatalogImportExport\Model\Export\RowCustomizerInterface $rowCustomizer
* @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool
* @param ImportProduct\StoreResolver $storeResolver
* @param \Magento\Customer\Api\GroupRepositoryInterface $groupRepository
* @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool
* @throws \Magento\Framework\Exception\LocalizedException
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand All @@ -117,6 +119,7 @@ public function __construct(
\Magento\CatalogImportExport\Model\Export\Product\Type\Factory $_typeFactory,
\Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider,
\Magento\CatalogImportExport\Model\Export\RowCustomizerInterface $rowCustomizer,
\Magento\Framework\Model\Entity\MetadataPool $metadataPool,
\Magento\CatalogImportExport\Model\Import\Product\StoreResolver $storeResolver,
\Magento\Customer\Api\GroupRepositoryInterface $groupRepository
) {
Expand All @@ -139,7 +142,8 @@ public function __construct(
$attributeColFactory,
$_typeFactory,
$linkTypeProvider,
$rowCustomizer
$rowCustomizer,
$metadataPool
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ class AdvancedPricingTest extends \PHPUnit_Framework_TestCase
*/
protected $groupRepository;

/**
* @var \Magento\Framework\Model\Entity\MetadataPool|\PHPUnit_Framework_MockObject_MockObject
*/
protected $metadataPool;

/**
* @var \Magento\ImportExport\Model\Export\Adapter\AbstractAdapter| \PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -285,6 +290,13 @@ protected function setUp()
'',
false
);
$this->metadataPool = $this->getMock(
'\Magento\Framework\Model\Entity\MetadataPool',
[],
[],
'',
false
);
$this->writer = $this->getMock(
'Magento\ImportExport\Model\Export\Adapter\AbstractAdapter',
[
Expand Down Expand Up @@ -343,6 +355,7 @@ protected function setUp()
$this->typeFactory,
$this->linkTypeProvider,
$this->rowCustomizer,
$this->metadataPool,
$this->storeResolver,
$this->groupRepository
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,71 @@
*/
namespace Magento\Bundle\Controller\Adminhtml\Product\Initialization\Helper\Plugin;

use Magento\Bundle\Api\Data\OptionInterfaceFactory as OptionFactory;
use Magento\Bundle\Api\Data\LinkInterfaceFactory as LinkFactory;
use Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory;
use Magento\Catalog\Api\ProductRepositoryInterface as ProductRepository;
use Magento\Store\Model\StoreManagerInterface as StoreManager;
use Magento\Framework\App\RequestInterface;

/**
* Class Bundle
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Bundle
{
/**
* @var \Magento\Framework\App\RequestInterface
* @var ProductCustomOptionInterfaceFactory
*/
protected $customOptionFactory;

/**
* @var RequestInterface
*/
protected $request;

/**
* @param \Magento\Framework\App\RequestInterface $request
* @var OptionFactory
*/
public function __construct(\Magento\Framework\App\RequestInterface $request)
{
protected $optionFactory;

/**
* @var LinkFactory
*/
protected $linkFactory;

/**
* @var ProductRepository
*/
protected $productRepository;

/**
* @var StoreManager
*/
protected $storeManager;

/**
* @param RequestInterface $request
* @param OptionFactory $optionFactory
* @param LinkFactory $linkFactory
* @param ProductRepository $productRepository
* @param StoreManager $storeManager
* @param ProductCustomOptionInterfaceFactory $customOptionFactory
*/
public function __construct(
RequestInterface $request,
OptionFactory $optionFactory,
LinkFactory $linkFactory,
ProductRepository $productRepository,
StoreManager $storeManager,
ProductCustomOptionInterfaceFactory $customOptionFactory
) {
$this->request = $request;
$this->optionFactory = $optionFactory;
$this->linkFactory = $linkFactory;
$this->productRepository = $productRepository;
$this->storeManager = $storeManager;
$this->customOptionFactory = $customOptionFactory;
}

/**
Expand All @@ -29,33 +81,122 @@ public function __construct(\Magento\Framework\App\RequestInterface $request)
* @return \Magento\Catalog\Model\Product
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function afterInitialize(
\Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject,
\Magento\Catalog\Model\Product $product
) {
if (($items = $this->request->getPost('bundle_options')) && !$product->getCompositeReadonly()) {
$compositeReadonly = $product->getCompositeReadonly();
$selections = $this->request->getPost('bundle_selections');
if ($selections && !$compositeReadonly) {
$product->setBundleSelectionsData($selections);
}

$items = $this->request->getPost('bundle_options');
if ($items && !$compositeReadonly) {
$product->setBundleOptionsData($items);
}

if (($selections = $this->request->getPost('bundle_selections')) && !$product->getCompositeReadonly()) {
$product->setBundleSelectionsData($selections);
$this->processBundleOptionsData($product);

$this->processDynamicOptionsData($product);

$affectProductSelections = (bool)$this->request->getPost('affect_bundle_product_selections');
$product->setCanSaveBundleSelections($affectProductSelections && !$compositeReadonly);
return $product;
}

/**
* @param \Magento\Catalog\Model\Product $product
* @return void
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function processBundleOptionsData(\Magento\Catalog\Model\Product $product)
{
$bundleOptionsData = $product->getBundleOptionsData();
if (!$bundleOptionsData) {
return;
}
$options = [];
foreach ($bundleOptionsData as $key => $optionData) {
if ((bool)$optionData['delete']) {
continue;
}
$option = $this->optionFactory->create(['data' => $optionData]);
$option->setSku($product->getSku());
$option->setOptionId(null);

$links = [];
$bundleLinks = $product->getBundleSelectionsData();
if (empty($bundleLinks[$key])) {
continue;
}

foreach ($bundleLinks[$key] as $linkData) {
if ((bool)$linkData['delete']) {
continue;
}
$link = $this->linkFactory->create(['data' => $linkData]);

if (array_key_exists('selection_price_value', $linkData)) {
$link->setPrice($linkData['selection_price_value']);
}

if (array_key_exists('selection_price_type', $linkData)) {
$link->setPriceType($linkData['selection_price_type']);
}

if ($product->getPriceType() == '0' && !$product->getOptionsReadonly()) {
$product->setCanSaveCustomOptions(true);
if ($customOptions = $product->getProductOptions()) {
foreach (array_keys($customOptions) as $key) {
$customOptions[$key]['is_delete'] = 1;
$linkProduct = $this->productRepository->getById($linkData['product_id']);
$link->setSku($linkProduct->getSku());
$link->setQty($linkData['selection_qty']);

if (array_key_exists('selection_can_change_qty', $linkData)) {
$link->setCanChangeQuantity($linkData['selection_can_change_qty']);
}
$product->setProductOptions($customOptions);
$links[] = $link;
}
$option->setProductLinks($links);
$options[] = $option;
}

$product->setCanSaveBundleSelections(
(bool)$this->request->getPost('affect_bundle_product_selections') && !$product->getCompositeReadonly()
);
$extension = $product->getExtensionAttributes();
$extension->setBundleProductOptions($options);
$product->setExtensionAttributes($extension);
return;
}

return $product;
/**
* @param \Magento\Catalog\Model\Product $product
* @return void
*/
protected function processDynamicOptionsData(\Magento\Catalog\Model\Product $product)
{
if ($product->getPriceType() !== \Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC) {
return;
}

if ($product->getOptionsReadonly()) {
return;
}
$product->setCanSaveCustomOptions(true);
$customOptions = $product->getProductOptions();
if (!$customOptions) {
return;
}
foreach (array_keys($customOptions) as $key) {
$customOptions[$key]['is_delete'] = 1;
}
$newOptions = $product->getOptions();
foreach ($customOptions as $customOptionData) {
if ((bool)$customOptionData['is_delete']) {
continue;
}
$customOption = $this->customOptionFactory->create(['data' => $customOptionData]);
$customOption->setProductSku($product->getSku());
$customOption->setOptionId(null);
$newOptions[] = $customOption;
}
$product->setOptions($newOptions);
}
}
3 changes: 2 additions & 1 deletion app/code/Magento/Bundle/Model/LinkManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public function addChild(

try {
$selectionModel->save();
$resource->addProductRelations($product->getId(), [$linkProductModel->getId()]);
$resource->addProductRelation($product->getId(), $linkProductModel->getId());
} catch (\Exception $e) {
throw new CouldNotSaveException(__('Could not save child: "%1"', $e->getMessage()), $e);
}
Expand All @@ -281,6 +281,7 @@ public function removeChild($sku, $optionId, $childSku)
foreach ($option->getSelections() as $selection) {
if ((strcasecmp($selection->getSku(), $childSku) == 0) && ($selection->getOptionId() == $optionId)) {
$removeSelectionIds[] = $selection->getSelectionId();
$usedProductIds[] = $selection->getProductId();
continue;
}
$excludeSelectionIds[] = $selection->getSelectionId();
Expand Down
Loading

0 comments on commit 9b08b0b

Please sign in to comment.