Magento 2 一些库存状态的一些疑问与异同
问题起源
{"message": "Product that you are trying to add is not available."}
可以看出不正常的产品“isSaleable”,”isSalable”,”isAvaliable”均为false,那么是什么导致的产品这些信息为false呢?找到这些方法一探究竟。
a. isSaleable() 只是调用了isSalable()
/*** Alias for isSalable()** @return bool*/public function isSaleable(){return $this->isSalable();}
b. isSalable()看着很复杂的样子,但是其实它是简单的检查产品是否可售,调用了isAvailable() 方法
/*** Check is product available for sale** @return bool*/public function isSalable(){if ($this->hasData('salable') && !$this->_catalogProduct->getSkipSaleableCheck()) {return $this->getData('salable');}$this->_eventManager->dispatch('catalog_product_is_salable_before', ['product' => $this]);$salable = $this->isAvailable();$object = new \Magento\Framework\DataObject(['product' => $this, 'is_salable' => $salable]);$this->_eventManager->dispatch('catalog_product_is_salable_after',['product' => $this, 'salable' => $object]);$this->setData('salable', $object->getIsSalable());return $object->getIsSalable();}
c. isAvailable()方法则是判断product type或者库存是否允许售卖
public function isAvailable(){return $this->getTypeInstance()->isSalable($this) || $this->_catalogProduct->getSkipSaleableCheck();}public function isSalable($product){$salable = $product->getStatus() == \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED;if ($salable && $product->hasData('is_salable')) {$salable = $product->getData('is_salable');}return (bool)(int)$salable;}
d. isInStock()方法仅仅检查产品的有无库存
public function isInStock(){return $this->getStatus() == \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED;}
综上
isInStock() —— 检查产品有无库存(检查产品Stock Status)
isAvailable() —— 根据产品的状态,库存状态,可视和网站等信息返回true或false
isSaleable()和isSalable() —— 与isAvailable()类似,但此方法在返回值之前添加了eventcatalog_product_is_salable_after,用户可以根据此事件加入自己的逻辑,从而使产品可售与否。
既然找到了问题,那我们就在后台正确配置产品信息,解决当前问题。
- 1 Magento ver. 2.2.0-dev - A technical prob... 16546
- 2 ECS git pull 代码很慢的问题 14687
- 3 玩客云监工客户端 13174
- 4 Magento 2 UI Form Validation 13095
- 5 Codeigniter SSL 强制重定向至https访问 12984
- 6 magento2 完整卸载模块的正确姿势 12613
- 7 Magento fix 500 12255
- 8 Magento index is locked by another reinde... 10713
- 9 Ubuntu删除日志文件 8986
- 10 Magento 2安装ElasticSearch 6.x搜索引擎-Ub... 8596


