Skip to content

Instantly share code, notes, and snippets.

@magevision
Created May 11, 2023 13:54
Show Gist options
  • Save magevision/bd6ef1d73c37e16c9b70ae5cc07fb7f2 to your computer and use it in GitHub Desktop.
Save magevision/bd6ef1d73c37e16c9b70ae5cc07fb7f2 to your computer and use it in GitHub Desktop.
CreateAnInventorySourceProgrammatically
<?php
declare(strict_types=1);
namespace MageVision\Blog87\Setup\Patch\Data;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Setup\ModuleDataSetupInterface as ModuleDataSetupInterfaceAlias;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\Data\SourceInterfaceFactory;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
class CreateInventorySource implements DataPatchInterface
{
private ModuleDataSetupInterfaceAlias $moduleDataSetup;
private SourceInterfaceFactory $sourceInterfaceFactory;
private SourceRepositoryInterface $sourceRepositoryInterface;
private DataObjectHelper $dataObjectHelper;
/**
* @param ModuleDataSetupInterfaceAlias $moduleDataSetup
* @param SourceInterfaceFactory $sourceInterfaceFactory
* @param SourceRepositoryInterface $sourceRepositoryInterface
* @param DataObjectHelper $dataObjectHelper
*/
public function __construct(
ModuleDataSetupInterfaceAlias $moduleDataSetup,
SourceInterfaceFactory $sourceInterfaceFactory,
SourceRepositoryInterface $sourceRepositoryInterface,
DataObjectHelper $dataObjectHelper
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->sourceInterfaceFactory = $sourceInterfaceFactory;
$this->sourceRepositoryInterface = $sourceRepositoryInterface;
$this->dataObjectHelper = $dataObjectHelper;
}
/**
* @inheritDoc
*/
public function apply()
{
$this->moduleDataSetup->startSetup();
$sourceData = [
SourceInterface::SOURCE_CODE => 'custom_source',
SourceInterface::NAME => 'Custom Source',
SourceInterface::ENABLED => 1,
SourceInterface::DESCRIPTION => 'Custom Source',
SourceInterface::LATITUDE => 0,
SourceInterface::LONGITUDE => 0,
SourceInterface::COUNTRY_ID => 'DE',
SourceInterface::POSTCODE => '00000',
];
$source = $this->sourceInterfaceFactory->create();
$this->dataObjectHelper->populateWithArray($source, $sourceData, SourceInterface::class);
$this->sourceRepositoryInterface->save($source);
$this->moduleDataSetup->endSetup();
}
/**
* @inheritDoc
*/
public static function getDependencies()
{
return [];
}
/**
* @inheritDoc
*/
public function getAliases()
{
return [];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment