1

I'm trying to get the products that are matched by a certain Catalog Price Rule (ID: 1) by using getMatchingProductIds(). The function returns all products, with their IDs as indices of the 1st level array. The second level array will have values either 0 or 1 depending on whether that product matches the Catalog Price Rule.

For example, my code:

Mage::getModel('catalogrule/rule')->load(1)->getMatchingProductIds();

returns something like:

Array (

    [1348] => Array (
        [0] => 1
        [1] => 1
        [2] => 1
        [4] => 1
    )
)

where 1348 is a product ID.

What do the indices in the 2nd level array (0, 1, 2, 4) represent? Why are there 4 of them? Why does it skip 3? Can their values NOT be all "1" even if the product matches the Catalog Price Rule?

(I do know that since they are all equal to 1, it means the product matches the Catalog Price Rule. But I don't know why there are 4 elements in that array, and what the indices mean.)

EDIT

My rule:

  • Status - Active

  • Websites - 3/3 selected (main website, example1.com, example2.com)

  • Customer Groups - 4/4 selected (not logged in, general, wholesale, retailer)

  • From Date - 1/10/13

  • To Date - 9/12/13

  • Priority - 1

  • Conditions - if any of the conditions are true: category is 160

  • Apply - To Fixed Amount

  • Discount Amount - 10

  • Enable Discounts to Subproducts - No

  • Stop Further Rules Processing - No

  • Related Banners - none

  • Maybe you should add your rule in the question. I only get an array of product ids. Just one level. – Marius Dec 9 '13 at 7:50
  • Hi @Marius, I added the rule. – Obay Dec 9 '13 at 8:04
  • Strange. I still get the same result. A one dimension array with product ids. – Marius Dec 9 '13 at 8:16
  • @Marius are you using catalogrule or the enterpise targetrule as that will return a different format. Not sure what the target rule is used for though. – David Manners Jan 16 '14 at 8:20
  • @DavidManners I tested with CE 1.7 (so no target rule) and I only had one single website. Maybe that's why I was getting a different result – Marius Jan 16 '14 at 8:24
2

The indices on the second level are the website id. It appears to loop through each website and checks if that product and rule combination is valid for that website.

The function that does this is the callback from the results walk.

/**
 * Callback function for product matching
 *
 * @param $args
 * @return void
 */
public function callbackValidateProduct($args)
{
    $product = clone $args['product'];
    $product->setData($args['row']);

    $results = array();
    foreach ($this->_getWebsitesMap() as $websiteId => $defaultStoreId) {
        $product->setStoreId($defaultStoreId);
        $results[$websiteId] = (int)$this->getConditions()->validate($product);
    }
    $this->_productIds[$product->getId()] = $results;
}

So that array is of format.

Array (
    [product_id] => Array (
        [website_id] => is_valid,
    )
)

NOTE: In older versions of Magento this was not the case. It used to just return an array or productIds. Versions before 1.8 had the following code.

/**
 * Callback function for product matching
 *
 * @param $args
 * @return void
 */
public function callbackValidateProduct($args)
{
    $product = clone $args['product'];
    $product->setData($args['row']);

    if ($this->getConditions()->validate($product)) {
        $this->_productIds[] = $product->getId();
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.