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

[FEATURE] Added configuration option to define which websites new products get assigned to when created via the api #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions Model/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

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

/**
* Merge data from DB and updates from request
*
Expand All @@ -25,8 +24,13 @@ protected function initializeProductData(array $productData, $createNew)
$product->setWebsiteIds(array_unique($websiteIds));
}
} else {
/** BUGFIX - Solves incorrect auto assign to a website when setting storeview value */
$product->unsetData('website_ids');
if ($product->isObjectNew()) {
/** Objectmanager because extending the whole constructor of core ProductRepository will be too complex to maintain */
$scopeConfig = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\App\Config\ScopeConfigInterface::class);
$websiteIds = $scopeConfig->getValue('webapi/experius_api_extend/product_store_autoassign');
$websiteArray = ( $websiteIds ? explode(',', $websiteIds) : [] );
$product->setWebsiteIds($websiteArray);
}
}
return $product;
}
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Experius Product website ids module for Magento 2
Adds functionality to the API to set and get website ids per product
Adds functionality to the API configure which websites Magento auto-assigns new products to when website_ids are not defined in API-call

This configuration can be found under Services - Magento Web API

### This module uses a preference for the productRepository class which could conflict. Unfortunately there was no other way.
15 changes: 15 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="webapi" translate="label" type="text" sortOrder="102" showInDefault="1" showInWebsite="0" showInStore="0">
<group id="experius_api_extend" translate="label" type="text" sortOrder="300" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Experius - API Extend</label>
<field id="product_store_autoassign" showInDefault="1" showInStore="0" showInWebsite="0" sortOrder="10" translate="label" type="multiselect" canRestore="1">
<label>Auto Assign Products to Stores</label>
<comment>Leave blank to assign products created via the API no to stores</comment>
<source_model>Magento\Config\Model\Config\Source\Website</source_model>
<can_be_empty>1</can_be_empty>
</field>
</group>
</section>
</system>
</config>