0

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

例如:我有 url_key "mens-shirt",我希望它是 "White"、"Medium"。到目前为止,我一直在遍历产品,寻找与给定过滤器相似的选项,并使用最匹配的产品。但是有没有更好的方法来做到这一点。

一个简单的例子:

$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

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

谢谢

4

1 回答 1

0

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

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

    $q = "select * from catalog_product_optionas cpo left join catalog_product_option_type_valueas tv on tv.option_id= cpo.option_id left join catalog_product_option_titleas ot on ot.option_id = cpo.option_id left join catalog_product_option_type_titleas tt on tt.option_type_id = tv.option_type_id where cpo.product_id= ".$product->getId()." and tt.title='White' and and tt.store_id=0 and ot.title='Your option title'";

2. 或者,如果您想检查多个选项,则:

$q="select tt.title from catalog_product_optionas cpo left join catalog_product_option_type_valueas tv on tv.option_id= cpo.option_id left join catalog_product_option_titleas ot on ot.option_id = cpo.option_id left join catalog_product_option_type_titleas tt on tt.option_type_id = tv.option_type_id where cpo. product_id=1 and tt.store_id=0" and ot.title='你的选项标题';

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

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

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

于 2014-07-04T07:56:31.647 回答