cancel
Showing results for 
Search instead for 
Did you mean: 

Relation between website & stock

Relation between website & stock

Hello,

 

I found 2 tables in Magento 2 database:

  • cataloginventory_stock
  • store_website

There is "website_id" column in the "cataloginventory_stock" table but I see no relation between cataloginventory_stock.website_id & store_website.website_id.

 

Is this relation missed or cataloginventory_stock.website_id is not the same as store_website.website_id?

 

Thanks,

 

Alex Gusev.

3 REPLIES

Re: Relation between website & stock

Table "cataloginventory_stock_item" has unique index on (product_id, stock_id) fields in Magento 1 and unique index on (product_id, website_id) in Magento 2. I suppose second variant is not right.

Re: Relation between website & stock

\Magento\CatalogInventory\Api\Data\StockInterface has no 'website_id' attribute (just 'stock_id' & 'stock_name'), but related table (cataloginventory_stock) has 3 fields:

 

CREATE TABLE cataloginventory_stock (
stock_id smallint(5) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Stock Id',
website_id smallint(5) UNSIGNED NOT NULL COMMENT 'Website Id',
stock_name varchar(255) DEFAULT NULL COMMENT 'Stock Name',
PRIMARY KEY (stock_id),
INDEX CATALOGINVENTORY_STOCK_WEBSITE_ID (website_id)
)

\Magento\CatalogInventory\Api\Data\StockItemInterface also has not 'website_id' attribute, but 'cataloginventory_stock_item' table has.

 

Comment for 'cataloginventory_stock_item.website_id' is "Is Divided into Multiple Boxes for Shipping". The same comment is for 'is_decimal_divided' field (the one field up from 'website_id'). Is "website_id" field a "copy-paste" error?

 

 

Re: Relation between website & stock

There is one only stock in the fresh Magento 2 installation - with stock_id=1 and website_id=0. I have created product and I see that stock item with default values is created for this product. By default stock_id=1 and website_id=1 in this case.

 

I suppose this is inconsistency when stock item has attribute website_id=1 but stock itself has website_id=0:

 

// get Object Manager and repositories to handle data in DB
$obm = \Magento\Framework\App\ObjectManager::getInstance();
/** @var  $repoStock \Magento\CatalogInventory\Api\StockRepositoryInterface */
$repoStock = $obm->get(\Magento\CatalogInventory\Api\StockRepositoryInterface::class);
/** @var  $repoProduct \Magento\Catalog\Api\ProductRepositoryInterface */
$repoProduct = $obm->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
/** @var  $repoStockItem \Magento\CatalogInventory\Api\StockItemRepositoryInterface */
$repoStockItem = $obm->get(\Magento\CatalogInventory\Api\StockItemRepositoryInterface::class);

// Get data for the stock that is default for empty Magento 2 app (stock_id=1)
$stockIdDef = 1;
$stock = $repoStock->get($stockIdDef);
$stockIdStock = $stock->getStockId();
$websiteIdStock = $stock->getData('website_id');
echo $stockIdStock; // 1
echo $websiteIdStock; // 0

// Create product
/** @var  $product \Magento\Catalog\Api\Data\ProductInterface */
$product = $obm->create(\Magento\Catalog\Api\Data\ProductInterface::class);
$product->setSku('SKU');
$product->setName('Product Name');
$product->setPrice(12.34);
$product->setAttributeSetId(4); // 4 - is default attribute set for the product entity
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE);
$savedProd = $repoProduct->save($product);
$prodId = $savedProd->getId();

// Get stock inventory item for the new product
/** @var  $criteria \Magento\CatalogInventory\Api\StockItemCriteriaInterface */
$criteria = $obm->create(\Magento\CatalogInventory\Api\StockItemCriteriaInterface::class);
$criteria->addFilter(
	'byId',
	\Magento\CatalogInventory\Api\Data\StockItemInterface::PRODUCT_ID,
	$prodId
);
$coll = $repoStockItem->getList($criteria);
$items = $coll->getItems();
/** @var  $stockItem \Magento\CatalogInventory\Model\Stock\Item */
$stockItem = reset($items);
$stockIdItem = $stockItem->getStockId();
$websiteIdStockItem = $stockItem->getWebsiteId();
echo $stockIdItem; // 1
echo $websiteIdStockItem; // 1
$equal = ($stockIdStock == $stockIdItem);
$notEqual = ($websiteIdStock == $websiteIdStockItem);