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

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

问题描述

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

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 中提取 skus.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;

然后检查列表页面如果 $skus 数组中的 sku 匹配,则显示标签.但同样,这种方法也有局限性.我在想另一种方法(我不确定那是否可行).考虑创建一个新表(用于保存销售规则产品).每次保存销售规则时,捕获保存规则事件并使用规则名称和所有关联产品更新表.然后在列表页面上检查该表,如果表中存在产品,则显示相应的标签.现在我认为事件是 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.我认为你可以在 php 中访问它,通过函数 getAllItemsInCart 或类似的东西(你必须找出来).在您执行 $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).

编辑 2:

其他可能性是在规则创建中有一个新字段,您可以在其中手动设置相关产品 (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天全站免登陆