You are here

function uc_varprice_feature_form_submit in UC Variable Price 7

Same name and namespace in other branches
  1. 6 uc_varprice.module \uc_varprice_feature_form_submit()

Creates the varprice feature in the database.

1 call to uc_varprice_feature_form_submit()
uc_varprice_node_insert in ./uc_varprice.module
Implements hook_node_insert().

File

./uc_varprice.module, line 370
Defines a product feature to turn any product into a variable priced product.

Code

function uc_varprice_feature_form_submit($form, &$form_state) {

  // Build an array of Variable Price data from the form submission.
  $vp_data = array(
    'pfid' => $form_state['values']['pfid'],
    'price_default' => $form_state['values']['price_default'],
    'price_minimum' => $form_state['values']['price_minimum'],
    'price_maximum' => $form_state['values']['price_maximum'],
    'add_to_cart_title' => $form_state['values']['override_add_to_cart_title'] ? $form_state['values']['add_to_cart_title'] : '',
    'amount_title' => $form_state['values']['override_amount_title'] ? $form_state['values']['amount_title'] : '',
  );

  // Build the product feature description.
  $description = array(
    t('Customers can specify a price for this product.'),
    t('<b>Default price:</b> @price', array(
      '@price' => uc_currency_format($vp_data['price_default']),
    )),
  );
  if (!empty($vp_data['price_minimum'])) {
    $description[] = t('<b>Minimum price:</b> @price', array(
      '@price' => uc_currency_format($vp_data['price_minimum']),
    ));
  }
  if (!empty($vp_data['price_maximum'])) {
    $description[] = t('<b>Maximum price:</b> @price', array(
      '@price' => uc_currency_format($vp_data['price_maximum']),
    ));
  }
  if (!empty($vp_data['add_to_cart_title'])) {
    $description[] = t('<b>Add to cart title:</b> @title', array(
      '@title' => $vp_data['add_to_cart_title'],
    ));
  }
  if (!empty($vp_data['amount_title'])) {
    $description[] = t('<b>Amount field title:</b> @title', array(
      '@title' => $vp_data['amount_title'],
    ));
  }

  // Save the basic product feature data.
  $data = array(
    'pfid' => $vp_data['pfid'],
    'nid' => $form_state['values']['nid'],
    'fid' => 'varprice',
    'description' => implode('<br />', $description),
  );
  $form_state['redirect'] = uc_product_feature_save($data);
  $vp_data['pfid'] = $data['pfid'];

  // Insert or update the data in the Variable Price products table.
  $key = array();
  if ($vpid = _uc_varprice_get_vpid($vp_data['pfid'])) {
    $key = 'vpid';
    $vp_data['vpid'] = $vpid;
  }
  drupal_write_record('uc_varprice_products', $vp_data, $key);
}