Skip to content

Commit

Permalink
[TASK] Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Lubbersen committed Dec 21, 2016
0 parents commit 567397f
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Model/Product/Attribute/Backend/Website.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

/**
* Catalog product categories backend attribute model
*
* @author Magento Core Team <[email protected]>
*/
namespace Experius\ProductWebsiteIdsApi\Model\Product\Attribute\Backend;

class Website extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
{
/**
* Set website ids to product data
*
* @param \Magento\Catalog\Model\Product $object
* @return $this
*/
public function afterLoad($object)
{
$object->setData($this->getAttribute()->getAttributeCode(), $object->getWebsiteIds());
return parent::afterLoad($object);
}
}
30 changes: 30 additions & 0 deletions Model/ProductRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Experius\ProductWebsiteIdsApi\Model;

class ProductRepository extends \Magento\Catalog\Model\ProductRepository
{

/**
* Merge data from DB and updates from request
*
* @param array $productData
* @param bool $createNew
* @return \Magento\Catalog\Api\Data\ProductInterface|Product
* @throws NoSuchEntityException
*/
protected function initializeProductData(array $productData, $createNew)
{
$websiteIds = null;
if (isset($productData['website_ids'])){
$websiteIds = $productData['website_ids'];
}
$product = parent::initializeProductData($productData, $createNew);
if ($websiteIds){
if (!$this->storeManager->hasSingleStore()) {
$product->setWebsiteIds(array_unique($websiteIds));
}
}
return $product;
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Experius Product website ids module for Magento 2
Adds functionality to the API to set and get website ids per product

### This module uses a preference for the productRepository class which could conflict. Unfortunately there was no other way.
47 changes: 47 additions & 0 deletions Setup/InstallData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php


namespace Experius\ProductWebsiteIdsApi\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface {

private $eavSetupFactory;


public function __construct(EavSetupFactory $eavSetupFactory){
$this->eavSetupFactory = $eavSetupFactory;
}


public function install(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
){
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'website_ids',
[
'type' => 'static',
'label' => 'Websites',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'backend' => 'Experius\ProductWebsiteIdsApi\Model\Product\Attribute\Backend\Website',
'required' => false,
'sort_order' => 9,
'visible' => false,
'group' => 'General',
'is_used_in_grid' => false,
'is_visible_in_grid' => false,
'is_filterable_in_grid' => false,
]
);

}
}
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "experius/module-productwebsiteidsapi",
"description": "Adds functionality to the API to set and get website ids per product",
"authors": [
{
"name": "Experius",
"email": "[email protected]"
}
],
"require": {},
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Experius\\ProductWebsiteidsApi\\": ""
}
}
}
10 changes: 10 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Api\ProductRepositoryInterface" type="Experius\ProductWebsiteIdsApi\Model\ProductRepository" />
</config>
8 changes: 8 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Experius_ProductWebsiteIdsApi" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog" />
</sequence>
</module>
</config>
6 changes: 6 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Experius_ProductWebsiteIdsApi',
__DIR__
);

0 comments on commit 567397f

Please sign in to comment.