0

So I have a custom block module that displays three links, which then when clicked, reload the page and redirect to checkout based on a GET variable.

function innovista_donate_contents() {
  if(isset($_GET['d'])){
    switch ($_GET['d']) {
      case 1:
        $product_id='6';
        break;
      case 2:
        $product_id='8';
        break;
    }       
    commerce_cart_product_add_by_id($product_id, $quantity = 1, $combine = TRUE);
    drupal_goto($path = 'checkout', $options = NULL, $http_response_code = 302);
  }
  else {
    return '<div class="col8wrap donate">
      <a class="dhover" href="?d=1">Link 1</a>
      <a class="dhover" href="?d=2">Link 2</a></div>';
  }
}

The issue is that if you are not logged in then often the top half of this function that uses the GET var doesn't work. I thought it might be a cache issue so cleared the cache but this hasn't resolved the issue. Any idea what is going on here?

2
  • try doing echo "<pre>".print_r($line_item)."</pre>"; inside the code and post back the output.
    – AjitS
    Oct 11, 2012 at 5:28
  • you can add exit() after echo just to check the value of $line_item.
    – AjitS
    Oct 11, 2012 at 5:57

1 Answer 1

3
+100

You block is build on the first page request. The else flow is followed. Depending on you settings the page or block is cached. Logged in users don't have cached blocks.

That it worked now and then for anonymous was probably due to your cache clears.

You should create a callback using hook_menu() like

function innovista_donate_menu() {
  return array(
    'innovista/addtocart' => array(
      'title' => 'Innovisata add to cart',
      'page callback' => 'innovista_donate_add_to_cart',
      'access callback' => TRUE, // Accessible for everybody
      'type' => MENU_CALLBACK,
    ),
  );
}

function innovista_donate_add_to_cart() {
  if(isset($_GET['d'])){
    switch ($_GET['d']) {
      case 1:
        $product_id='6';
        break;
      case 2:
        $product_id='8';
        break;
    }       
    commerce_cart_product_add_by_id($product_id, 1, TRUE);
    drupal_goto('checkout');
  }
}

Your block can now contain just the links. You can now disabled PHP input filter probably too.

(Note I changed your drupal_goto and removed tabs)

5
  • Hey @clemens-tolboom, it succesffuly calls the function but I'm now getting this error on the checkout page Warning: Invalid argument supplied for foreach() in menu_unserialize() (line 400 of /home/sites/innovista.org/public_html/beta/includes/menu.inc).Any ideas why? Oct 14, 2012 at 16:19
  • I think it is to do with permissions because when not logged in it says access denied Oct 14, 2012 at 16:50
  • I've used this 'page arguments'=>array(0), 'access callback' => TRUE, instead of access arguments and has fixed the issue Oct 14, 2012 at 16:55
  • One more issue...I'm trying to use ` drupal_goto($path = 'checkout', $options = array( 'query' => array( 'foo' => bar)), $http_response_code = 302);`` to get to /checkout?foo=bar but when it redirect it goes to /checkout/order_id Any ideas? Oct 14, 2012 at 16:58
  • There is another destination active I guess: checkout drupal_goto() and url() api.drupal.org/api/drupal/includes!common.inc/function/… Oct 19, 2012 at 14:07

Your Answer

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

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