1

Basically This is my Array

 array( 
        [product_id] => Array
        ( 
         [#type] => hidden
         [#value] => 39 
        )         
      )

and I am trying to get the value which is '39'

$cartId = $form['product_id']['#value'];

and yes I get the value without any issues, but in my site it gives me a notice saying "Notice: Undefined index: product_id in ..."

This is my entire code

function MyModule_form_alter( &$form, $form_state, $form_id )
{
    $cartId = $form['product_id']['#value'];
    print_r ($form['#value']);
    if ($form_id == 'commerce_cart_add_to_cart_form_'.$cartId)
    { 
        $form['add_to_wishlist']['#weight']= 49;
    }   
}

any ideas how to fix this? because I couldn't figure the cause of it as it works well!

1 Answer 1

4

Your function is called for all forms in your Drupal. $cartId = $form['product_id']['#value']; is called outside if, so it's also executed on all forms. And most of them does not have $form['product_id'] defined at all. Move that part inside if block.

1
  • Thanks Molot, You gave me an Idea where I added another if condition to check if it's a commerce form and then added the rest of my code inside it. Aug 21, 2013 at 8:20

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.