-1

Issue

Need to convert an Array Deference that works in PHP 5.4 to a version that works with PHP 5.3. I'm unable to update my live site PHP so I'm a bit stuck. What I'm trying to accomplish is create a coupon code for Drupal 7 as a form is submitted.

Where I've looked:

PHP syntax for dereferencing function result Good discussion. Almost looks like it isn't possible at all. There are several good solution examples that I haven't been able to convert to my issue.

5.4 dereferencing to valid 5.3 array call Very similar to my issue but I haven't been able to figure out a way using it.

Original that works in PHP 5.4:

$coupon->store_discount_reference = ['und'=>[['target_id'=>"57"]]];
$coupon->store_coupon_exclusive = ['und'=>[['value'=>"0"]]];
$coupon->store_coupon_conditions = ['und' => [
    [
        'condition_name'=>'store_coupon_usage_evaluate_usage',
        'condition_settings'=> ['max_usage'=>'1'],
        'conditions_negative'=>0,
        'remove_condition'=>'Remove'
    ],
]];

What I Have Tried

$coupon->store_discount_reference = array('und'=> array('target_id'=>"57"));
$coupon->store_coupon_exclusive = array('und'=> array('value'=>"0"));
$coupon->store_coupon_conditions = array('und' => array('condition_name'=>'store_coupon_usage_evaluate_usage','condition_settings'=> array('max_usage'=>'1'),'conditions_negative'=>0,'remove_condition'=>'Remove'));

Full Chunk

$coupon = new stdClass();
$coupon->type = "discount_coupon";
$coupon->coupon_id = '';
$coupon->code = $code;
$coupon->bulk = false;
$coupon->created = '';
$coupon->status = 1;
$coupon->uid = 0;
$coupon->is_new = true;
$coupon->commerce_discount_reference = ['und'=>[['target_id'=>"57"]]];
$coupon->commerce_coupon_exclusive = ['und'=>[['value'=>"0"]]];
$coupon->commerce_coupon_conditions = ['und' => [
    [
        'condition_name'=>'commerce_coupon_usage_evaluate_usage',
        'condition_settings'=> ['max_usage'=>'1'],
        'conditions_negative'=>0,
        'remove_condition'=>'Remove'
    ],
]];
commerce_coupon_save($coupon);
Community
  • 1
  • 1
CJdriver
  • 458
  • 7
  • 20
  • Do you mean you want to convert `[]` to `array()` or is there something you have not said here, or I have missed – RiggsFolly Apr 21 '16 at 23:11
  • I guess I'm not sure what I would be changing to array(). I tried wrapping them all in array() instead of [] and it didn't work. I'll add an example of what I had tried. – CJdriver Apr 21 '16 at 23:15
  • You should get new hosting then and make sure they are using a modern PHP. You're opening yourself up to security risks by not upgrading. – Machavity Apr 21 '16 at 23:31

1 Answers1

2

You appear to be missing an array level everywhere

$coupon->store_discount_reference = array('und'=> array(array('target_id'=>"57")));

$coupon->store_coupon_exclusive = array('und'=> array( array('value'=>"0")));

$coupon->store_coupon_conditions = 
   array('und' => 
           array( 
                  array('condition_name'=>'store_coupon_usage_evaluate_usage',
                        'condition_settings'=> array('max_usage'=>'1'),
                        'conditions_negative'=>0,
                        'remove_condition'=>'Remove'
                   )
                )
  );
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • I tried that but it's not saving any of the information from those 3 items. It does submit though. Not sure if that would be a drupal related issue or not though. – CJdriver Apr 21 '16 at 23:28
  • Cannot help with Drupal, sorry – RiggsFolly Apr 21 '16 at 23:29
  • @CJdriver The field names seem to change between your 2 examples, that might be the problem. If you're trying to write to the built-in fields (which you probably are) they should be `commerce_*` rather than `store_*`. Other than that this answer's spot on and the code should work – Clive Apr 22 '16 at 17:40
  • Thats what you needed, someone who knows Drupal – RiggsFolly Apr 22 '16 at 19:13
  • Sorry, I had manually changed them to store for the first example and forgot to on the second. I asked a buddy who is a drupal guru and basically the way that commerce_coupon_save works you cannot do it without array deferences. Was able to force them to upgrade to PHP 5.5 over this though so all problems solved. Thanks for your help folks. – CJdriver Apr 22 '16 at 19:16