本文整理汇总了PHP中Magento\CatalogInventory\Api\StockRegistryInterface::getStockItem方法的典型用法代码示例。如果您正苦于以下问题:PHP StockRegistryInterface::getStockItem方法的具体用法?PHP StockRegistryInterface::getStockItem怎么用?PHP StockRegistryInterface::getStockItem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\CatalogInventory\Api\StockRegistryInterface
的用法示例。
在下文中一共展示了StockRegistryInterface::getStockItem方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: afterLoad
/**
* Set inventory data to custom attribute
*
* @param Product $object
* @return $this
*/
public function afterLoad($object)
{
$stockItem = $this->stockRegistry->getStockItem($object->getId(), $object->getStore()->getWebsiteId());
$object->setData($this->getAttribute()->getAttributeCode(), ['is_in_stock' => $stockItem->getIsInStock(), 'qty' => $stockItem->getQty()]);
return parent::afterLoad($object);
}
示例2: 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;
}
示例3: 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);
}
示例4: 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;
}
示例5: 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;
}
示例6: 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());
}
}
示例7: 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;
}
示例8: 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;
}
示例9: 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);
}
示例10: 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;
}
示例11: isItemAvailableForReorder
/**
* Check item product availability for reorder
*
* @param \Magento\Sales\Model\Order\Item $orderItem
* @return boolean
*/
protected function isItemAvailableForReorder(\Magento\Sales\Model\Order\Item $orderItem)
{
try {
$stockItem = $this->stockRegistry->getStockItem($orderItem->getProduct()->getId(), $orderItem->getStore()->getWebsiteId());
return $stockItem->getIsInStock();
} catch (\Magento\Framework\Exception\NoSuchEntityException $noEntityException) {
return false;
}
}
示例12: getStockItemToBeUpdated
/**
* Return the stock item that needs to be updated.
* If the stock item does not need to be updated, return null.
*
* @param \Magento\Catalog\Api\Data\ProductInterface $product
* @return \Magento\CatalogInventory\Api\Data\StockItemInterface|null
*/
protected function getStockItemToBeUpdated($product)
{
// from the API, all the data we care about will exist as extension attributes of the original product
$extendedAttributes = $product->getExtensionAttributes();
if ($extendedAttributes !== null) {
$stockItem = $extendedAttributes->getStockItem();
if ($stockItem != null) {
return $stockItem;
}
}
// we have no new stock item information to update, however we need to ensure that the product does have some
// stock item information present. On a newly created product, it will not have any stock item info.
$stockItem = $this->stockRegistry->getStockItem($product->getId());
if ($stockItem->getItemId() != null) {
// we already have stock item info, so we return null since nothing more needs to be updated
return null;
}
return $stockItem;
}
示例13: canReturnItemToStock
/**
* Whether to show 'Return to stock' checkbox for item
*
* @param Item $item
* @return bool
*/
public function canReturnItemToStock($item = null)
{
if (null !== $item) {
if (!$item->hasCanReturnToStock()) {
$stockItem = $this->stockRegistry->getStockItem($item->getOrderItem()->getProductId(), $item->getOrderItem()->getStore()->getWebsiteId());
$item->setCanReturnToStock($stockItem->getManageStock());
}
return $item->getCanReturnToStock();
}
return $this->canReturnToStock();
}
示例14: proccessAdditionalValidation
/**
* Processing additional validation to check if carrier applicable.
*
* @param \Magento\Framework\Object $request
* @return $this|bool|\Magento\Framework\Object
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function proccessAdditionalValidation(\Magento\Framework\Object $request)
{
//Skip by item validation if there is no items in request
if (!count($this->getAllItems($request))) {
return $this;
}
$maxAllowedWeight = (double) $this->getConfigData('max_package_weight');
$errorMsg = '';
$configErrorMsg = $this->getConfigData('specificerrmsg');
$defaultErrorMsg = __('The shipping module is not available.');
$showMethod = $this->getConfigData('showmethod');
/** @var $item \Magento\Quote\Model\Quote\Item */
foreach ($this->getAllItems($request) as $item) {
$product = $item->getProduct();
if ($product && $product->getId()) {
$weight = $product->getWeight();
$stockItemData = $this->stockRegistry->getStockItem($product->getId(), $item->getStore()->getWebsiteId());
$doValidation = true;
if ($stockItemData->getIsQtyDecimal() && $stockItemData->getIsDecimalDivided()) {
if ($stockItemData->getEnableQtyIncrements() && $stockItemData->getQtyIncrements()) {
$weight = $weight * $stockItemData->getQtyIncrements();
} else {
$doValidation = false;
}
} elseif ($stockItemData->getIsQtyDecimal() && !$stockItemData->getIsDecimalDivided()) {
$weight = $weight * $item->getQty();
}
if ($doValidation && $weight > $maxAllowedWeight) {
$errorMsg = $configErrorMsg ? $configErrorMsg : $defaultErrorMsg;
break;
}
}
}
if (!$errorMsg && !$request->getDestPostcode() && $this->isZipCodeRequired($request->getDestCountryId())) {
$errorMsg = __('This shipping method is not available. Please specify the zip code.');
}
if ($errorMsg && $showMethod) {
$error = $this->_rateErrorFactory->create();
$error->setCarrier($this->_code);
$error->setCarrierTitle($this->getConfigData('title'));
$error->setErrorMessage($errorMsg);
return $error;
} elseif ($errorMsg) {
return false;
}
return $this;
}
示例15: validateStockItem
/**
* @param ProductInterface $product
* @param StockItemInterface $stockItem
* @throws LocalizedException
* @return void
*/
private function validateStockItem(ProductInterface $product, StockItemInterface $stockItem)
{
$defaultScopeId = $this->stockConfiguration->getDefaultScopeId();
$defaultStockId = $this->stockRegistry->getStock($defaultScopeId)->getStockId();
$stockId = $stockItem->getStockId();
if ($stockId !== null && $stockId != $defaultStockId) {
throw new LocalizedException(__('Invalid stock id: %1. Only default stock with id %2 allowed', $stockId, $defaultStockId));
}
$stockItemId = $stockItem->getItemId();
if ($stockItemId !== null && (!is_numeric($stockItemId) || $stockItemId <= 0)) {
throw new LocalizedException(__('Invalid stock item id: %1. Should be null or numeric value greater than 0', $stockItemId));
}
$defaultStockItemId = $this->stockRegistry->getStockItem($product->getId())->getItemId();
if ($defaultStockItemId && $stockItemId !== null && $defaultStockItemId != $stockItemId) {
throw new LocalizedException(__('Invalid stock item id: %1. Assigned stock item id is %2', $stockItemId, $defaultStockItemId));
}
}