With the newest changes in Ubercart (7.x-3.0-rc2), Custom Pricing is not displayed when adding products to the cart. Additionally, it is not displayed in the "Cart contents" panel of the review order screen, or the "Cart contents" panel of the checkout screen.

My guess is that the new ubercart field, display_price, being used in place of sell_price in places like the cart, is affecting this.

The node page's price display, Order total/subtotal fields still use the correct custom price.

Comments

Dylanotron’s picture

I found that setting $item->display_price along with $item->price in the custom price calculation fixes this issue, like so:

$item->price = $item->display_price = 100; //100 is an exemplar

So, instead of writing a script to update all of my custom price values in the database, I went to uc_custom_price.module and modified it like so:

function uc_custom_price_node_view($node) {
  if (isset($node->custom_code) &&
      $node->custom_code != '$item->price = $item->price;') {
    $node->custom_code = str_replace('$item->price =','$item->display_price = $item->price =',$node->custom_code);

Adding the last line. This may not be the proper solution, though, because the user may not intend to set display_price, or the user may have already set display_price. Perhaps it would be better to add a checkbox asking the user whether to add this line of code ("Have $item->price update $display->price?")

Changes are still currently being made in Ubercart to change other areas (e.g. catalog view) over to display_price, but I expect this solution will work despite the continuing changes to Ubercart.

Dylanotron’s picture

The fix above only corrects the shopping cart block when viewing the node page that of the item that is in the cart. Regular price is still being shown for:

Cart page
Cart panel on checkout page

Dylanotron’s picture

Here is the entire fix to correct for custom price display for all pages. As noted in #1, this may not be the proper solution, though, because the user may not intend to set display_price, or the user may have already set display_price. Perhaps it would be better to add a checkbox asking the user whether to add this line of code ("Have $item->price update $display->price?")

Changes are still currently being made in Ubercart to change other areas (e.g. catalog view) over to display_price, but I expect this solution will work despite the continuing changes to Ubercart.

uc_custom_price.module:

/**
 * Implements hook_uc_cart_item().
 *
 * Price calculation takes place here.
 */
function uc_custom_price_uc_cart_item($op, $item) {
  switch ($op) {
    case 'load':
      $product = node_load($item->nid);
      // should really initialize this on load so we don't have to check later
      $code = isset($product->custom_code) ? $product->custom_code : '';
+      $code = str_replace('$item->price =','$item->display_price = $item->price =',$code);
      if (!empty($code)) {
        $eval_code = token_replace($code, array('product' => $product, 'uc_cart_item' => $item));
        eval($eval_code);
      }
      break;
  }
}

/**
 * Implements hook_node_view().
 */
function uc_custom_price_node_view($node) {
  if (isset($node->custom_code) &&
      $node->custom_code != '$item->price = $item->price;') {
+    $node->custom_code = str_replace('$item->price =','$item->display_price = $item->price =',$node->custom_code);
    $item = clone $node;
    $item->price = $node->sell_price;
    $eval_code = token_replace($node->custom_code, array('product' => $node));
    eval($eval_code);
    // $eval_code is normally written to operate on the cart context, which is
    // different than the product view.  For example, $item->qty is defined
    // in the cart, but not on the product page (where quantity has not yet
    // been entered by the customer!).  So it's easily possible to write PHP
    // $eval_code that works in the cart but not on the product page.  We should
    // try to protect against that, and only set sell_price and display_price
    // when the eval() succeeds.
    $node->content['sell_price']['#value']    = $item->price;
    $node->content['display_price']['#value'] = $item->price;
  }
}