Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I want to load coupons that have commerce_discount_reference=4. I find that can load them by commerce_coupon_load_multiple but I dont know how I add condition commerce_discout_reference=4.
maybe something like

  commerce_coupon_load_multiple(array(), array('commerce_discount_reference'=>4));

what is correct way?

enter image description here

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

Sounds like the perfect job for an EntityFieldQuery:

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'commerce_coupon')
  ->fieldCondition('commerce_discount_reference', 'target_id', 4);

$results = $query->execute();
if (!empty($results['commerce_coupon'])) {
  $coupons = commerce_coupon_load_multiple(array_keys($results['commerce_coupon']));
}
share|improve this answer
    
always great :). tnx @Clive –  zhilevan Feb 24 at 9:23
add comment

commerce_coupon_load_multiple function in turn uses entity_load function.

 if (empty($commerce_coupon_ids) && empty($conditions)) {
    return array();
  }

  return entity_load('commerce_coupon', $commerce_coupon_ids, $conditions, $reset);

As above documentation says

$conditions : An array of conditions to match against the {commerce_coupon} table.

Below is the commerce_coupon table schema

function commerce_coupon_schema() {
  $schema['commerce_coupon'] = array(
    'description' => 'The base table for coupons.',
    'fields' => array(
      'coupon_id' => array(
        'description' => 'The primary identifier for the coupon.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'uid' => array(
        'description' => 'An identifier for the user who owns this coupon',
        'type' => 'int',      
        'not null' => TRUE,
        'default' => 0,          
      ),
      'type' => array(
        'description' => 'The type of this coupon.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'code' => array(
        'description' => 'The code of this coupon.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'bulk' => array(
        'description' => 'Whether or not this coupon code was generated in bulk.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the coupon was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'status' => array(
        'description' => 'Whether or not this coupon is enabled.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0
      )
    ),
    'indexes' => array(
      'uid' => array('uid')        
    ),
    'primary key' => array('coupon_id'),
    'foreign keys' => array(
      'uid' => array(
        'table' => 'users',
        'column' => array('uid' => 'uid')
      ),        
    ),
    'unique keys' => array(
      'code' => array('code')
    )
  );

  return $schema;
}

As you see in above schema there exists no field called "commerce_discount_reference" which you can query on .. I believe "commerce_discount_reference" field is a custom field... So you need to use Entity Field Query as explained above by Clive...

share|improve this answer
    
yes, I also conclude this, thanks for your attention and your answer –  zhilevan Feb 24 at 9:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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