I have a scenario where I would like to charge one price for the first node created, then a different price for following ones per user.

I've created two "Create Pay to Publish" products, one for each price. I now have both of these displaying in the "Listing option" select when adding a node.

The problem I have here is the customer can choose between the prices. I would like the site to choose for them.

Is there a way to override the "Listing option" and set the value dynamically? With a hook_form_alter perhaps?

Any guidance on this would be much appreciated.

Comments

larowlan’s picture

Hey
You can use hook_form_alter to modify the #options on the field.
You can even change it to #type => value if there is only one option (and remove the choice).

Lee

iancawthorne’s picture

Status: Active » Fixed

Thanks Lee.

I've managed to come up with a solution. Posting my module code here in case it us any use for anyone else.

<?php
function form_overrides_form_alter(&$form, &$form_state, $form_id) {

  if ($form_id == 'page_node_form') {
    
    global $user;
    
    //Query how many nodes the logged in user is an author of
    $sql = "SELECT node.nid AS nid FROM {node} node WHERE (( (node.uid = '".$user->uid."' ) ))";
    $result = db_query($sql);
    
    $i = 0;
    foreach ($result as $row) {
      $i++;
    }
    
    //Taken from commerce_node_checkout.module in commerce_node_checkout module
    $node = $form['#node'];
    $enabled_products = commerce_node_checkout_get_products_by_type($node->type);
    $products = commerce_node_checkout_get_product_list($enabled_products);
    
    if ($i < 1) {
      $products = array(1 => $products[1]); //Hard code the array to only contain product id 1
    } else {
      $products = array(2 => $products[2]); //Hard code the array to only contain product id 2
    }
    
    $form['commerce_node_checkout_product'] = array (
      '#type' => 'value',
      '#value' => array_shift(array_keys($products))
    );
  }

}
?>

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.