Magento:通过url_key找到产品,然后将其与属性匹配


Magento: find product by url_key and then match it to attributes

我的应用程序需要能够通过url和选项以编程方式将特定产品添加到客户购物车中。我在这里要求的领域是看看是否有一种更快(更容易(的方法可以在数据库中找到需要按属性筛选的匹配产品。

例如:我有url_key"男式衬衫",我想要"白色"、"中号"。到目前为止,我一直在循环浏览产品,寻找与给定过滤器相似的选项,并使用最匹配的产品。但是有更好的方法吗。

一个简单的例子:

$products = Mage::getModel('catalog/product')->
            getCollection()->
            addAttributeToFilter('url_key', $filters['url_key']);
// psuedo code:
        // Check if product has options
        // Loop each option and check the matching label to the desired filters
        // if matching, then use product
        // continue with program

如果我可以使用一些过滤器来更有效地完成工作,我认为循环所有内容有点浪费。

感谢

如果您谈论的是Custom Options,那么您可以运行自定义查询

  1. 如果您想检查单个选项标签,请使用以下查询:

    $q="从catalog_product_option中选择*作为cpoleft作为电视上的电视加入catalog_product_option_type_value。option_id=cpo.option_idleft在ot.option_id=cpo.option_id上作为ot加入catalog_product_option_title在tt.option_type_id=tv.option''ype_id上以tt的身份左加入catalog_product_option_type_title其中cpo.product_id=".$product->getId((."和tt.title='White'和tt.store_id=0和ot.title='您的选项标题'";

2。或者,如果你想检查多个选项,那么:

$q="从catalog_product_option中选择tt.title作为cpoleft作为电视上的电视加入catalog_product_option_type_value。option_id=cpo.option_idleft在ot.option_id=cpo.option_id上作为ot加入catalog_product_option_title在tt.option_type_id=tv.option''ype_id上将catalog_product_option_type_title作为tt加入其中cpo.product_id=1和tt.store_id=0"以及ot.title='Your option title';

您将获得一系列选项标题。您可以简单地使用in_array函数来检查您想要的选项是否存在。像

if(in_array('White') || in_array('Black')){
   do something.
}

相应地更新where条件,并根据Magento方式修改这些查询。我很着急:(