本文整理汇总了PHP中Magento\CatalogInventory\Api\StockRegistryInterface类的典型用法代码示例。如果您正苦于以下问题:PHP StockRegistryInterface类的具体用法?PHP StockRegistryInterface怎么用?PHP StockRegistryInterface使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StockRegistryInterface类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setUp
protected function setUp()
{
parent::setUp();
$this->stockRegistryMock = $this->getMockBuilder(StockRegistryInterface::class)->setMethods(['getStockItem'])->getMockForAbstractClass();
$this->storeMock = $this->getMockBuilder(Store::class)->disableOriginalConstructor()->getMock();
$this->stockItemMock = $this->getMockBuilder(StockItemInterface::class)->setMethods(['getData'])->getMockForAbstractClass();
$this->stockRegistryMock->expects($this->any())->method('getStockItem')->willReturn($this->stockItemMock);
$this->productMock->expects($this->any())->method('getStore')->willReturn($this->storeMock);
}
示例2: setUp
protected function setUp()
{
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->registryMock = $this->getMock('Magento\\Framework\\Registry', [], [], '', false);
$this->stockItem = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockItemInterface', ['getQtyIncrements'], '', false);
$this->stockItem->expects($this->any())->method('getStockItem')->willReturn(1);
$this->stockRegistry = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\StockRegistryInterface', ['getStockItem'], '', false);
$this->stockRegistry->expects($this->any())->method('getStockItem')->willReturn($this->stockItem);
$this->block = $objectManager->getObject('Magento\\CatalogInventory\\Block\\Qtyincrements', ['registry' => $this->registryMock, 'stockRegistry' => $this->stockRegistry]);
}
示例3: modifyData
/**
* {@inheritdoc}
*/
public function modifyData(array $data)
{
$fieldCode = 'quantity_and_stock_status';
$model = $this->locator->getProduct();
$modelId = $model->getId();
/** @var StockItemInterface $stockItem */
$stockItem = $this->stockRegistry->getStockItem($modelId, $model->getStore()->getWebsiteId());
$stockData = $modelId ? $this->getData($stockItem) : [];
if (!empty($stockData)) {
$data[$modelId][self::DATA_SOURCE_DEFAULT][self::STOCK_DATA_FIELDS] = $stockData;
}
if (isset($stockData['is_in_stock'])) {
$data[$modelId][self::DATA_SOURCE_DEFAULT][$fieldCode]['is_in_stock'] = (int) $stockData['is_in_stock'];
}
if (!empty($this->stockConfiguration->getDefaultConfigValue(StockItemInterface::MIN_SALE_QTY))) {
$minSaleQtyData = null;
$defaultConfigValue = $this->stockConfiguration->getDefaultConfigValue(StockItemInterface::MIN_SALE_QTY);
if (strpos($defaultConfigValue, 'a:') === 0) {
// Set data source for dynamicRows Minimum Qty Allowed in Shopping Cart
$minSaleQtyValue = unserialize($defaultConfigValue);
foreach ($minSaleQtyValue as $group => $qty) {
$minSaleQtyData[] = [StockItemInterface::CUSTOMER_GROUP_ID => $group, StockItemInterface::MIN_SALE_QTY => $qty];
}
} else {
$minSaleQtyData = $defaultConfigValue;
}
$path = $modelId . '/' . self::DATA_SOURCE_DEFAULT . '/stock_data/min_qty_allowed_in_shopping_cart';
$data = $this->arrayManager->set($path, $data, $minSaleQtyData);
}
return $data;
}
示例4: getStockId
/**
* Retrieve stock_id by store
*
* @param int $websiteId The website Id
*
* @return int
*/
private function getStockId($websiteId)
{
if (!isset($this->stockIdByWebsite[$websiteId])) {
$stockId = $this->stockRegistry->getStock($websiteId)->getStockId();
$this->stockIdByWebsite[$websiteId] = $stockId;
}
return $this->stockIdByWebsite[$websiteId];
}
示例5: build
/**
* Copy product inventory data (used for product duplicate functionality)
*
* @param \Magento\Catalog\Model\Product $product
* @param \Magento\Catalog\Model\Product $duplicate
* @return void
*/
public function build(\Magento\Catalog\Model\Product $product, \Magento\Catalog\Model\Product $duplicate)
{
$stockData = ['use_config_min_qty' => 1, 'use_config_min_sale_qty' => 1, 'use_config_max_sale_qty' => 1, 'use_config_backorders' => 1, 'use_config_notify_stock_qty' => 1];
$currentStockItemDo = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());
if ($currentStockItemDo->getItemId()) {
$stockData += ['use_config_enable_qty_inc' => $currentStockItemDo->getUseConfigEnableQtyInc(), 'enable_qty_increments' => $currentStockItemDo->getEnableQtyIncrements(), 'use_config_qty_increments' => $currentStockItemDo->getUseConfigQtyIncrements(), 'qty_increments' => $currentStockItemDo->getQtyIncrements()];
}
$duplicate->setStockData($stockData);
}
示例6: afterLoad
/**
* Add stock item information to the product's extension attributes
*
* @param \Magento\Catalog\Model\Product $product
* @return \Magento\Catalog\Model\Product
*/
public function afterLoad(\Magento\Catalog\Model\Product $product)
{
$productExtension = $product->getExtensionAttributes();
if ($productExtension === null) {
$productExtension = $this->productExtensionFactory->create();
}
// stockItem := \Magento\CatalogInventory\Api\Data\StockItemInterface
$productExtension->setStockItem($this->stockRegistry->getStockItem($product->getId()));
$product->setExtensionAttributes($productExtension);
return $product;
}
示例7: testGenerateSimpleProductsWithPartialData
/**
* @param array $productsData
* @dataProvider generateSimpleProductsWithPartialDataDataProvider
* @magentoDbIsolation enabled
*/
public function testGenerateSimpleProductsWithPartialData($productsData)
{
$this->_product->setNewVariationsAttributeSetId(4);
$generatedProducts = $this->_model->generateSimpleProducts($this->_product, $productsData);
$parentStockItem = $this->stockRegistry->getStockItem($this->_product->getId());
foreach ($generatedProducts as $productId) {
$stockItem = $this->stockRegistry->getStockItem($productId);
$this->assertEquals($parentStockItem->getManageStock(), $stockItem->getManageStock());
$this->assertEquals('1', $stockItem->getIsInStock());
}
}
示例8: addStockStatusToProducts
/**
* Add stock status information to products
*
* @param \Magento\Catalog\Model\Resource\Collection\AbstractCollection $productCollection
* @return void
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function addStockStatusToProducts(\Magento\Catalog\Model\Resource\Collection\AbstractCollection $productCollection)
{
$websiteId = $this->storeManager->getStore($productCollection->getStoreId())->getWebsiteId();
$productIds = [];
foreach ($productCollection as $product) {
$productId = $product->getId();
$stockStatus = $this->stockRegistry->getStockStatus($productId, $websiteId);
$status = $stockStatus->getStockStatus();
$product->setIsSalable($status);
}
}
示例9: getProductQtyIncrements
/**
* Retrieve product qty increments
*
* @return float|false
*/
public function getProductQtyIncrements()
{
if ($this->_qtyIncrements === null) {
$stockItem = $this->stockRegistry->getStockItem($this->getProduct()->getId(), $this->getProduct()->getStore()->getWebsiteId());
$this->_qtyIncrements = $stockItem->getQtyIncrements();
if (!$this->getProduct()->isSaleable()) {
$this->_qtyIncrements = false;
}
}
return $this->_qtyIncrements;
}
示例10: init
/**
* @param \Magento\Quote\Model\Quote $quote
* @param \Magento\Catalog\Model\Product $product
* @param \Magento\Framework\DataObject $config
* @return \Magento\Quote\Model\Quote\Item|string
*/
public function init(\Magento\Quote\Model\Quote $quote, \Magento\Catalog\Model\Product $product, \Magento\Framework\DataObject $config)
{
$stockItem = $this->stockRegistry->getStockItem($product->getId(), $quote->getStore()->getWebsiteId());
if ($stockItem->getIsQtyDecimal()) {
$product->setIsQtyDecimal(1);
} else {
$config->setQty((int) $config->getQty());
}
$product->setCartQty($config->getQty());
$item = $quote->addProduct($product, $config, \Magento\Catalog\Model\Product\Type\AbstractType::PROCESS_MODE_FULL);
return $item;
}
示例11: testBuildWithCurrentProductStockItem
public function testBuildWithCurrentProductStockItem()
{
$expectedData = ['use_config_min_qty' => 1, 'use_config_min_sale_qty' => 1, 'use_config_max_sale_qty' => 1, 'use_config_backorders' => 1, 'use_config_notify_stock_qty' => 1, 'use_config_enable_qty_inc' => 'use_config_enable_qty_inc', 'enable_qty_increments' => 'enable_qty_increments', 'use_config_qty_increments' => 'use_config_qty_increments', 'qty_increments' => 'qty_increments'];
$this->stockRegistry->expects($this->once())->method('getStockItem')->will($this->returnValue($this->stockItemDoMock));
$this->stockItemDoMock->expects($this->any())->method('getItemId')->will($this->returnValue(50));
$this->stockItemDoMock->expects($this->any())->method('getUseConfigEnableQtyInc')->will($this->returnValue('use_config_enable_qty_inc'));
$this->stockItemDoMock->expects($this->any())->method('getEnableQtyIncrements')->will($this->returnValue('enable_qty_increments'));
$this->stockItemDoMock->expects($this->any())->method('getUseConfigQtyIncrements')->will($this->returnValue('use_config_qty_increments'));
$this->stockItemDoMock->expects($this->any())->method('getQtyIncrements')->will($this->returnValue('qty_increments'));
$this->duplicateMock->expects($this->once())->method('setStockData')->with($expectedData);
$this->model->build($this->productMock, $this->duplicateMock);
}
示例12: afterGetQuantityValidators
/**
* @param \Magento\Catalog\Block\Product\View $block
* @param array $validators
* @return array
*/
public function afterGetQuantityValidators(\Magento\Catalog\Block\Product\View $block, array $validators)
{
$stockItem = $this->stockRegistry->getStockItem($block->getProduct()->getId(), $block->getProduct()->getStore()->getWebsiteId());
$params = [];
$params['minAllowed'] = max((double) $stockItem->getQtyMinAllowed(), 1);
if ($stockItem->getQtyMaxAllowed()) {
$params['maxAllowed'] = $stockItem->getQtyMaxAllowed();
}
if ($stockItem->getQtyIncrements() > 0) {
$params['qtyIncrements'] = (double) $stockItem->getQtyIncrements();
}
$validators['validate-item-quantity'] = $params;
return $validators;
}
示例13: testGetProductStockQty
/**
* Run test getProductStockQty method
*
* @return void
*/
public function testGetProductStockQty()
{
$productId = 10;
$websiteId = 99;
$qty = 100.0;
$productMock = $this->getMock('Magento\\Catalog\\Model\\Product', ['getId', 'getStore'], [], '', false);
$storeMock = $this->getMock('Magento\\Store\\Model\\Store', ['getWebsiteId'], [], '', false);
$stockItemMock = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockItemInterface', [], '', false, true, true, ['getQty']);
$productMock->expects($this->once())->method('getId')->will($this->returnValue($productId));
$productMock->expects($this->once())->method('getStore')->will($this->returnValue($storeMock));
$storeMock->expects($this->once())->method('getWebsiteId')->will($this->returnValue($websiteId));
$this->stockRegistryMock->expects($this->once())->method('getStockItem')->with($productId, $websiteId)->will($this->returnValue($stockItemMock));
$stockItemMock->expects($this->once())->method('getQty')->will($this->returnValue($qty));
$this->assertEquals($qty, $this->_block->getProductStockQty($productMock));
}
示例14: modifyData
/**
* {@inheritdoc}
*/
public function modifyData(array $data)
{
$fieldCode = 'quantity_and_stock_status';
$model = $this->locator->getProduct();
$modelId = $model->getId();
$stockItem = $this->stockRegistry->getStockItem($modelId, $model->getStore()->getWebsiteId());
$stockData = $stockItem->getData();
if (!empty($stockData)) {
$data[$modelId][self::DATA_SOURCE_DEFAULT][self::STOCK_DATA_FIELDS] = $stockData;
}
if (isset($stockData['is_in_stock'])) {
$data[$modelId][self::DATA_SOURCE_DEFAULT][$fieldCode]['is_in_stock'] = (int) $stockData['is_in_stock'];
}
return $this->prepareStockData($data);
}
示例15: getStockItem
/**
* Init stock item
*
* @param \Magento\Quote\Model\Quote\Item\Option $option
* @param \Magento\Quote\Model\Quote\Item $quoteItem
*
* @return \Magento\CatalogInventory\Model\Stock\Item
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getStockItem(\Magento\Quote\Model\Quote\Item\Option $option, \Magento\Quote\Model\Quote\Item $quoteItem)
{
$stockItem = $this->stockRegistry->getStockItem($option->getProduct()->getId(), $quoteItem->getStore()->getWebsiteId());
if (!$stockItem->getItemId()) {
throw new \Magento\Framework\Exception\LocalizedException(__('The stock item for Product in option is not valid.'));
}
/**
* define that stock item is child for composite product
*/
$stockItem->setIsChildItem(true);
/**
* don't check qty increments value for option product
*/
$stockItem->setSuppressCheckQtyIncrements(true);
return $stockItem;
}