Display collection of Shopping cart rules and products categories associated to each rule

前端 未结 4 513

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

相关标签:
4条回答
  • 2021-01-06 10:23

    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).

    Good luck :)

    Edit:

    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:

    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 :-)

    0 讨论(0)
  • 2021-01-06 10:26

    The rules are associated to all product for a website. There is no rules set for a specific products/categories from the database point of view. For each product in the cart, Magento will validate all the rules you have for a website. This operation is done in the class Mage_SalesRule_Model_Validator. The only way to solve your request is to extend the function process from this class (at least I think so :p).

    0 讨论(0)
  • 2021-01-06 10:46

    I wanted the same thing as you want. I wanted to get associated SKUS, Category Ids and any other conditions value to generate Google feeds to be used in Google merchant promotions.

    I have used the recursive function to reach to last children of the condition and fetch its value.

    I am checking based on the attribute value of the condition. If an attribute value is blank then go one step down and check if attribute value present and if so then fetch the value of it otherwise continue to go down.

    Here is the code that I used to fetch values. Which will also work for the case, when two conditions are on the same level.

    public function get_value_recursively($value){
    
            foreach($value as $key => $new_value) {
                if(strlen($new_value[attribute]) == 0){
                    $value = $new_value[conditions];
                    return $this->get_value_recursively($value);
                }else{
                    $resultSet = array();
                    if (count($value) > 1){
                        for ($i=0;$i<count($value);$i++) {
                            $resultSet[] = array('attribute' =>  $value[$i][attribute], 'value' => $value[$i][value]);
                        }
                        $result = $resultSet;
                    }else{
                        $result = array('attribute' =>  $new_value[attribute], 'value' => $new_value[value]);
                    }
                    return json_encode($result, JSON_FORCE_OBJECT);
                }
            }
        }
    

    according to @seanbreeden answer you can call this function from first foreach

    It will return the result like this :

    {"0":{"attribute":"category_ids","value":"5, 15"},"1":{"attribute":"sku","value":"msj000, msj001, msj002"}}
    

    P.S. I am not PHP dev. So, Ignore layman style code. :)

    0 讨论(0)
  • 2021-01-06 10:47

    You could pull the getMatchingProductsIds from /app/code/core/Mage/CatalogRule/Model/Rule.php and compare them with the skus displayed on the category list page.

     $catalog_rule = Mage::getModel('catalogrule/rule')->load(1);  // ID of your catalog rule here, or you could leave off ->load(1) and iterate through  ->getCollection() instead
     $catalog_rule_skus = $catalog_rule->getMatchingProductIds();
    

    hth

    EDIT

    Here's a way to get the serialized conditions:

    $rules = Mage::getResourceModel('salesrule/rule_collection')->load();
    
    foreach ($rules as $rule) {
        $conditions = $rule->getConditionsSerialized();
        var_dump($conditions);
    }
    

    EDIT 2

    There would have to be a better way to do this. The only way I could pull that data was to unserialize then iterate with foreach through each layer. Anyone have any better ideas for this? This works but is very sloppy.

    $rules = Mage::getResourceModel('salesrule/rule_collection')->load();
    
    foreach ($rules as $rule) {
    
      if ($rule->getIsActive()) { 
    $conditions = $rule->getConditionsSerialized();
    $unserialized_conditions = unserialize($conditions);
    
    $unserialized_conditions_compact = array();
    
    foreach($unserialized_conditions as $key => $value) {
       $unserialized_conditions_compact[] = compact('key', 'value');
    }
    
    for ($i=0;$i<count($unserialized_conditions_compact);$i++) {
            if (in_array("conditions",$unserialized_conditions_compact[$i])) {
                    foreach($unserialized_conditions_compact[$i] as $key => $value) {
                            foreach($value as $key1 => $value1) {
                                    foreach($value1 as $key2 => $value2) {
                                            foreach($value2 as $key3 => $value3) {
                                                    $skus[] = explode(",",$value3['value']);
                                            }
                                    }
                            }
                    }
            }
    }
     }
    
    }
    
    var_dump($skus);
    
    0 讨论(0)
提交回复
热议问题