0

I am trying to get flag "purchase" and flag it for an user while completing an order in Rules (Commerce). This is how it goes in a PHP code:

$flag = flag_get_flag('purchase');
$nid = $_SERVER ['REQUEST_URI'];
$nid = explode("node/", $nid);
$node = node_load($nid[1]);
global $user;
$flag->flag('purchase', $node->nid, user_load($user->uid), TRUE);

Does anybody see any inaccuracy in what I've done? I just don't know why it is not working.

This is the error I am getting (actually it says nothing to me):

Notice: Undefined offset: 1 in eval() (line 4 of C:\xampp\htdocs\mysite\sites\all\modules\rules\modules\php.eval.inc(146) : eval()'d code). Notice: Trying to get property of non-object in eval() (line 6 of C:\xampp\htdocs\mysite\sites\all\modules\rules\modules\php.eval.inc(146) : eval()'d code). Notice: Trying to get property of non-object in flag_entity->applies_to_entity() (line 130 of C:\xampp\htdocs\mysite\sites\all\modules\flag\includes\flag\flag_entity.inc).

5
  • This suggests that the flag is undefined. Are you sure the machine name of the flag is "purchase"? Check again..
    – pritywiz
    Apr 30, 2014 at 3:37
  • @pritywiz that's incorrect. The error is the code fetching the $nid is invalid, hence $nid[1] is undefined and the node isn't loaded. Apr 30, 2014 at 4:06
  • How is the node id defined at that point? Are you on a node page? If on checkout complete (e.g checkout/12/complete), the nid won't be available that way. Apr 30, 2014 at 7:28
  • Unfortunately you're right @DavidThomas. So what can I do then?
    – Michael D
    Apr 30, 2014 at 9:35
  • You will need to try to get the product display node id from the product reference field on the relevant line item. It's complicated by the fact that an order can contain multiple line items from different product displays and potentially that the same product may be referenced on multiple displays. Good luck! Apr 30, 2014 at 11:21

1 Answer 1

3

Try below code... You can read node arguments using arg function in Drupal...

global $user;
$flag = flag_get_flag('purchase');
$node = node_load(arg(1));
$flag->flag('purchase', $node->nid, user_load($user->uid), TRUE);
4
  • This code will definitely work when I find out how to get $node->nid of bought product. Thank you Anil.
    – Michael D
    Apr 30, 2014 at 9:57
  • Glad it worked.. If one of the answers above fixes your issue, you should accept it (click the check mark next to the appropriate answer). That does two things. It lets everyone know your issue has been resolved, and it gives the person that helps you credit for the assist. See here for a full explanation
    – Anil Sagar
    Apr 30, 2014 at 10:31
  • It would work if the node nid was available, in this case, the node id isn't available that way. Apr 30, 2014 at 11:21
  • I have used the same code, but for me 'uid' column is not getting updated in 'flagging' table. why it is happening?
    – Tharick
    Dec 27, 2017 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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