0

On a custom page I receive an array of data from another db. This has three or less product attributes set.

  $products = 
  [0] => Array
    (
        [field_1] => dsvx
        [field_2] => 
        [field_3] => 
    )

[1] => Array
    (
        [field_1] => dssdf
        [field_2] => 
        [field_3] => 
    )

[2] => Array
    (
        [field_1] => dsfeebe
        [field_2] => GBURG
        [field_3] => M
    )

[3] => Array
    (
        [field_1] => nionj
        [field_2] => blue
        [field_3] => S
    )

Each part of the above array may or may not represent a product in my magento db. In cases where it does represent a product in my magento db it is possible that not all of the fields are applicable. For example in part [2] of the array all three fields are populated. But it could be a case that I only have a product in my magento db that matches field_1. This would be ok as long as the corresponding field_2 and field_3 in my magento db are null.

The other point to note is that I only want to load one magento product per product in the array I'm checking against. This obviously needs to be the most closely matched product.

Due to the nature of this I'm inclined to think that using the collection method for getting all matching products may not be suitable. (I'm totally open to being corrected on this).

The method I was planning to choose would look something like the following

  foreach ($products as $key => $value) {
        if( product exists with matching field1, field2, field3){
             record id of magento product with attribute matching field1, field2 and field3
        }elseif(product exists with matching field1, field2,){
             record id of magento product with attribute matching field1 and field2
        }else{
             record id of magento product with attribute matching field1  
        }

    }

Using that method and building on a call like Mage::getModel('catalog/product')->loadByAttribute('field1', 'somevalue'); how would it look like syntax wise? Can i just put an array in so it would look like this Mage::getModel('catalog/product')->loadByAttribute(array('field1'= 'somevalue','field2'='anotherval','field3'=>'morevals'));

Alternatively can this be done using a collection?

1

The loadByAttribute function uses a collection to search for the matched product, but the problem with this would be that you have no order control over the returned product. So I think one way you could do this would be by using a product collection and some filters for the attributes:

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter(array(
        array('attribute' => 'field_1', 'eq' => 'field_1_value'),
        array('attribute' => 'field_1', 'null' => true),
    ))
    ->addAttributeToFilter(array(
        array('attribute' => 'field_2', 'eq' => 'field_2_value'),
        array('attribute' => 'field_2', 'null' => true),
    ))
    ->addAttributeToFilter(array(
        array('attribute' => 'field_3', 'eq' => 'field_3_value'),
        array('attribute' => 'field_3', 'null' => true),
    ))
    ->setOrder('field_1', 'desc')
    ->setOrder('field_2', 'desc')
    ->setOrder('field_3', 'desc')
    ->setPage(1,1);
$product = $collection->getFirstItem();

This will query the database for products that have field_1 equal to the desired value or null and field_2 equal to the desired value or null and field_3 equal to the desired value or null. The products will also be order based on the attributes matched values so that the actual values will be preferred to null values. The only downside to this will be that you might get some products that only match field_3 and I'm not sure if you want that or not.

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.