Skip to content

Instantly share code, notes, and snippets.

@magevision
Created May 11, 2023 08:44
Show Gist options
  • Save magevision/c054fec5d71f5b0248f61e3c812d04a7 to your computer and use it in GitHub Desktop.
Save magevision/c054fec5d71f5b0248f61e3c812d04a7 to your computer and use it in GitHub Desktop.
AddCustomInventoryAttributeProgrammatically
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="cataloginventory_stock_item" resource="default" comment="Cataloginventory Stock Item">
<column xsi:type="smallint" name="coming_soon" padding="5" unsigned="true" nullable="false"
identity="false" default="0" comment="Coming Soon"/>
</table>
</schema>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<modal name="advanced_inventory_modal" provider="product_form.product_form_data_source">
<fieldset name="stock_data">
<container name="magevision_container_manage_stock" component="Magento_Ui/js/form/components/group" sortOrder="2000">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="formElement" xsi:type="string">container</item>
<item name="dataScope" xsi:type="string">stock_data</item>
</item>
</argument>
<field name="coming_soon" sortOrder="10" formElement="select">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="rawOptions" xsi:type="boolean">true</item>
<item name="value" xsi:type="number">0</item>
</item>
</argument>
<settings>
<scopeLabel>[GLOBAL]</scopeLabel>
<label translate="true">Coming Soon</label>
<dataScope>coming_soon</dataScope>
</settings>
<formElements>
<select>
<settings>
<options class="Magento\Config\Model\Config\Source\Yesno"/>
</settings>
</select>
</formElements>
</field>
</container>
</fieldset>
</modal>
</form>
<?php
namespace MageVision\Blog86\Model;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
class StockManagement
{
private StockRegistryInterface $stockRegistry;
/**
* @param StockRegistryInterface $stockRegistry
*/
public function __construct(
StockRegistryInterface $stockRegistry
) {
$this->stockRegistry = $stockRegistry;
}
/**
* @param string $sku
* @param bool $comingSoon
* @return void
* @throws NoSuchEntityException
*/
public function update(string $sku, bool $comingSoon): void
{
$stockItem = $this->stockRegistry->getStockItemBySku($sku);
$stockItem->setData('coming_soon', $comingSoon);
$this->stockRegistry->updateStockItemBySku($sku, $stockItem);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment