显示购物车规则和与每个规则相关的产品类别的集合 [英] Display collection of Shopping cart rules and products categories associated to each rule

查看:101
本文介绍了显示购物车规则和与每个规则相关的产品类别的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查产品上是否有促销活动,然后在类别列表页面上在该产品上粘贴促销标签.但是我不知道如何遍历所有购物车规则以及如何检索与每个规则相关的产品/类别.

I want to check if there is a sales promotion on the product then stick the promotion label on that product on category list page. But I don't know how to loop through all the shopping cart rules and retrieve the products/categories associated to each rule.

已编辑

感谢seanbreeden,但我无法从$conditions中抽出灵气. var_dump($conditions);显示如下:

Thanks seanbreeden, but I can't pull the skus from $conditions. var_dump($conditions); shows this:

{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}a:7:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";s:10:"conditions";a:1:{i:0;a:7:{s:4:"type";s:42:"salesrule/rule_condition_product_subselect";s:9:"attribute";s:3:"qty";s:8:"operator";s:2:">=";s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";s:10:"conditions";a:1:{i:0;a:5:{s:4:"type";s:32:"salesrule/rule_condition_product";s:9:"attribute";s:12:"category_ids";s:8:"operator";s:2:"==";s:5:"value";s:2:"23";s:18:"is_value_processed";b:0;}}}}}a:7:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";s:10:"conditions";a:2:{i:0;a:5:{s:4:"type";s:32:"salesrule/rule_condition_address";s:9:"attribute";s:13:"base_subtotal";s:8:"operator";s:2:">=";s:5:"value";s:2:"45";s:18:"is_value_processed";b:0;}i:1;a:7:{s:4:"type";s:42:"salesrule/rule_condition_product_subselect";s:9:"attribute";s:3:"qty";s:8:"operator";s:2:">=";s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";s:10:"conditions";a:1:{i:0;a:5:{s:4:"type";s:32:"salesrule/rule_condition_product";s:9:"attribute";s:3:"sku";s:8:"operator";s:2:"==";s:5:"value";s:46:"test-config, BLFA0968C-BK001, BLFA0968C-CR033X";s:18:"is_value_processed";b:0;}}}}}a:6:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}a:6:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";}a:7:{s:4:"type";s:32:"salesrule/rule_condition_combine";s:9:"attribute";N;s:8:"operator";N;s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";s:10:"conditions";a:1:{i:0;a:7:{s:4:"type";s:42:"salesrule/rule_condition_product_subselect";s:9:"attribute";s:3:"qty";s:8:"operator";s:2:">=";s:5:"value";s:1:"1";s:18:"is_value_processed";N;s:10:"aggregator";s:3:"all";s:10:"conditions";a:1:{i:0;a:5:{s:4:"type";s:32:"salesrule/rule_condition_product";s:9:"attribute";s:3:"sku";s:8:"operator";s:2:"==";s:5:"value";s:16:"BLFA0968C-CR033X";s:18:"is_value_processed";b:0;}}}}}

但是当我遍历$ conditions时

but when I loop through $conditions i.e.

$rules = Mage::getResourceModel('salesrule/rule_collection')->load();

foreach ($rules as $rule) {
    $conditions = $rule->getConditionsSerialized();
    foreach ($conditions as $condition) {
        var_dump($condition);
    }
}

它什么也没显示,所以在这里真的不知道如何拉动skus.

it doesn't show anything so don't really know how to pull skus here.

EDIT2 正如Alaxandre所建议的那样,我没有使用非序列化方法.我现在是这样的:

EDIT2 As Alaxandre suggested, I'm not using unserialized approach. I'm doing it like this now:

$rules = Mage::getResourceModel('salesrule/rule_collection')->load();

foreach ($rules as $rule) {
    if ($rule->getIsActive()) {
        //print_r($rule->getData());
        $rule = Mage::getModel('salesrule/rule')->load($rule->getId()); 

        $conditions = $rule->getConditions();
        $conditions = $rule->getConditions()->asArray();

        foreach( $conditions['conditions'] as $_conditions ):
            foreach( $_conditions['conditions'] as $_condition ):
                $string = explode(',', $_condition['value']);
                for ($i=0; $i<count($string); $i++) {
                    $skus[] = trim($string[$i]);
                }
            endforeach;
        endforeach;
    }
}
return $skus;

然后在列表页面中检查sku是否在$ skus数组中匹配,然后显示标签.但同样,这种方法也有局限性.我想到了另一种方法(我不确定那是否可行). 考虑创建一个新表(以保存销售规则产品).每次保存销售规则,捕获保存规则事件,并使用规则名称和所有相关产品更新表.然后在列表页面上检查该表,如果表中存在产品,请显示适当的标签.现在我认为该事件是adminhtml_controller_salesrule_prepare_save(不是100%肯定),但是我不知道如何从观察者的规则条件获取sku并将其保存在新表中.

And then checking in list page if sku matches within $skus array then show the label. But again there are limitation with this approach as well. I'm think of another approach (I'm not sure if thats is possible). Thinking of creating a new table (to save the sales rules products).Everytime save the sales rule, catch the save rule event and update the table with Rule name and all the associated products. Then on the list page check that table, if products exist in the table, show the appropriate label. Now I think the event is adminhtml_controller_salesrule_prepare_save (not 100% sure) but I don't know how to get the sku from the rule condition in the observer to save in the new table.

推荐答案

我建议您这样做.当您有产品要购物时,将检查每个规则以计算最终价格和减价.您可以知道哪些规则适用于购物车的每个项目.在表sales_flat_quote_item中,您具有列applied_rule_ids.我认为您可以通过函数getAllItemsInCart或类似的东西在php中访问此文件(您必须找出答案).完成$item->getAppliedRuleIds()之后,最终您可以获取适用于某项(产品)的规则的名称.

I would suggest you to do it like this. When you had a product to cart, each rules are checked to calculate the final price and reduction. You can know which rules are applied to each item of your cart. In the table sales_flat_quote_item you have the column applied_rule_ids. I think you can access to this in php, by a function getAllItemsInCart or something like this (you have to find out). After you do $item->getAppliedRuleIds() and finally you can get the name of the rule apply to an item (product).

祝你好运:)

我再次阅读了您的请求,我认为我的回答与您的请求不符. 您的情况更加复杂.对于目录页面上的每种产品,您必须应用网站的所有规则.但是Mage_SalesRule_Model_Validator流程期望项目而不是产品... 如果您有很多规则,此任务将减慢您的目录速度,这确实不好!最好是将规则标签的结果缓存在数据库中,可能在表catalog_category_product或...中(甚至更好的是自动生成此缓存).

I read again your request and I think my answer doesn't fit with your request. Your case is even more complicated. For each product on your catalog page you have to apply all the rules of your website. But Mage_SalesRule_Model_Validator process expect item and not product... If you have lot of rules this task will slow down your catalog and this is really not good! The best would be to cache this result of the rules label in the database, may be in the table catalog_category_product or... (and even better to generate this cache automatically).

Edit2:

其他可能性是在规则创建中有一个新字段,您可以在其中手动设置相关产品(sku).您可以将此数据保存在表salesrule或新表salesrule_related_sku中. 然后,当您显示目录时,请检查sku以及规则是否仍处于活动状态. 此解决方案将是最简单的解决方案:-)

Other possibility would be to have a new field in rule creation where you set manually the related products (sku). You save this data in the table salesrule or in a new table salesrule_related_sku. Then when you display the catalog you check for the sku and if the rule still active. This solution would be the easiest one :-)

这篇关于显示购物车规则和与每个规则相关的产品类别的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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