isSaleable()和isAvailable()有什么区别? [英] What is the difference between isSaleable() and isAvailable()?

查看:177
本文介绍了isSaleable()和isAvailable()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的Magento主题的(单个)产品页面上显示库存量,而我对此并不完全了解.

I'm working on the display of the stock availability on the (individual) product page of my Magento theme, and there's something I don't completely understand about this.

我看到模板中使用了两种方法来检查产品是否可供出售:

I see two methods being used in the templates to check whether a product is available for sale:

Mage_Catalog_Model_Product::isAvailable()
Mage_Catalog_Model_Product::isSaleable()

我自己的发现:
我看到isSalable()(又被isSaleable()调用)调用了isAvailable(),但还分派了两个事件(catalog_product_is_salable_beforecatalog_product_is_salable_after).

My own findings:
I see that isSalable() (which in turn is called by isSaleable()) calls isAvailable() but also dispatches two events (catalog_product_is_salable_before and catalog_product_is_salable_after).

在前面,我注意到在Magento的基本模板isAvailable()中,该模板用于决定将产品显示为有货"还是无货"; isSaleable()用于确定是否显示添加到购物车"按钮之类的内容.

On the fronted I've noticed that in Magento's base template isAvailable() is used to decide whether to display the product as "in stock" or "out of stock"; isSaleable() is used to decide something like whether to show an "Add to Cart" button.

在后端,我注意到当库存数量为零且不允许 进行缺货订货时,产品的库存将变为无货". 当库存数量为零并且允许补货 时,产品的库存可用性a保持不变.

On the backend I've noticed that when the stock quantity becomes zero and backorders are not allowed, the stock availability of a product goes to "out of stock". When the stock quantity becomes zero and backorders are allowed, the stock availability a of product remains unchanged.

问题:
属性库存可用性"和库存数量"显然与所提到的PHP方法相互关联.我想知道:

Question:
The properties "stock availability" and "stock quantity" are obviously linked with each other and the mentioned PHP methods. I would like to know:

  • PHP方法isAvailable()之间的语义区别是什么 isSaleable()是,为什么我要在另一个上使用;

  • what the semantic difference between the PHP methods isAvailable() and isSaleable() is and why I would use one over the other;

我似乎还不知道它们与这些属性的关系以及Magento的行为.

what it is I appear not yet to know about their relation with these properties and Magento's behaviour.

谢谢.


我已经尝试过每种产品的库存数量(-1,0,1),库存可用性(进/出)和缺货订单(开/关)的所有相关组合,结果是:


I've tried every relevant combination of stock quantity (-1,0,1), stock availability (in/out of) and backorders (on/off) for a product, and this is the result:


St.Qu  BckOrd  St.Av  isSalable()  isSaleable()  isAvailable()
   -1       0      0            0             0              0
   -1       0      1          N/A           N/A            N/A
   -1       1      0            0             0              0
   -1       1      1            1             1              1
    0       0      0            0             0              0
    0       0      1          N/A           N/A            N/A
    0       1      0            0             0              0
    0       1      1            1             1              1
    1       0      0            0             0              0
    1       0      1            1             1              1
    1       1      0            0             0              0
    1       1      1            1             1              1

仅出于完整性考虑:


St.Av 0  = out of stock
St.Av 1  = in stock
BckOrd 0 = no backorders allowed
BckOrd 1 = backorders are allowed

Magento中的库存可用性开关控制所有PHP方法的返回值,但是当缺货订单关闭并且库存量降至1以下时,库存可用性将自动重置为无库存"(因此N/A行).

It is the stock availability switch in Magento that controls the return value of all of the PHP methods, but when backorders are off and stock quantity drops below 1, the stock availability will automatically be reset to 'out of stock' (hence the N/A rows).

推荐答案

isSaleable() 在使用Magento模板时,您肯定会偶然发现应用于产品对象的isSalable()方法.该方法实际上存在,但是它仅检查产品是否处于启用状态,并且不应跳过可销售的检查.然后,返回产品对象的is_salable属性.

isSaleable() While working with Magento templates you definitely stumbled upon isSalable() method applied to product object. The method physically exists but it only checks if product has enabled status and salable check should not be skipped. Then the is_salable property of the product object is returned.

一个明显的问题是何时设置此属性.加载产品后,它已经在模型上设置了,但它不是属性,也不是产品平面表中的列.

The obvious question is when this property is set. After product is loaded it is already set on model but it is not an attribute and is not a column in product flat table.

像往常一样,Magento中所有奇怪的事情都是由观察者完成的. Mage_Cataloginventory正在观察catalog_product_load_after事件,并归结为Mage_CatalogInventory_Model_Resource_Stock_Status::getProductStatus和以下查询:

As usual, all the bizarre stuff in Magento is done by observers. Mage_Cataloginventory is observing catalog_product_load_after event and there it comes down to Mage_CatalogInventory_Model_Resource_Stock_Status::getProductStatus and the following query:

SELECT `cataloginventory_stock_status`.`product_id`, 
    `cataloginventory_stock_status`.`stock_status` 
FROM `cataloginventory_stock_status` 
WHERE product_id IN('241319') 
    AND stock_id=1 
    AND website_id=3;

很明显,在重新索引期间做出了产品是否可销售的决定.并且忽略stock_id,这是一种未完成的功能,稍后也会弹出.

It is clearly visible that the decision if product is salable or not is made during reindexing. And disregard stock_id which is sort of unfinished functionality which will also pop out later.

所以我们最终将在一个没有理智的Magento开发人员愿意去索引器的地方.在我们的案例中,目录库存索引器.快速浏览Mage_CatalogInventory_Model_Indexer_Stock::_processEventMage_Index_Model_Indexer_Abstract::reindexAllMage_CatalogInventory_Model_Resource_Indexer_Stock::reindexAll的迷宫之后,我们发现每种产品类型都有其自己的股票索引器,该索引器位于app/code/core/Mage/CatalogInventory/Model/Resource/Indexer/Stock中.

So we are ending up in a place no sane Magento developer will willingly go .. the indexer. Catalog inventory indexer in our case. After quick travel through the maze of Mage_CatalogInventory_Model_Indexer_Stock::_processEvent, Mage_Index_Model_Indexer_Abstract::reindexAll and Mage_CatalogInventory_Model_Resource_Indexer_Stock::reindexAll we discover that each product type has it’s own stock indexer which resides in app/code/core/Mage/CatalogInventory/Model/Resource/Indexer/Stock.

每种类型都有一个_getStockStatusSelect方法,其中SQL查询最终确定产品是否可销售.即使查询看似庞大,其背后的逻辑也并不复杂.

Each type has a _getStockStatusSelect method where an SQL query finally decides if the product is in salable or not. Even though the query may seem massive the logic behind is not complicated.

这里的大部分代码还是这些基本的东西.似乎核心开发人员做了很好的尝试,允许不同的网站具有不同的库存水平,但是由于某些原因,该功能从未完成.

The big part of code here is again this rudimentary stuff. Seems like core developers made a fine attempt to allow having different stock levels for different websites but for some reason this functionality was never finished.

因此,例如,对简单产品库存可用性的检查仅包含验证产品是否已启用以及数量是否为正,并带有库存管理标志.可配置产品和分组产品的查询因产品类型的特定性而有所不同.

So for instance the check of simple products stock availability only contains of verifying that product is enabled and quantity is positive spiced with a stock management flags. Queries for configurable and grouped products varies a bit due to product type specifics.

这篇关于isSaleable()和isAvailable()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆