Support from Acquia helps fund testing for Drupal Acquia logo

Comments

p4trizio’s picture

I found this...
http://views-help.doc.logrus.com/help/views/api

but I'm not an expert. Any advices how to begin?

kevinquillen’s picture

Does this have no views integration? Pretty integral I'd think to have it, so you could have a Price field that feeds off of uc_price_per_role based on a rid argument(s) from the user object.

p4trizio’s picture

I had to find somewhere else. Just solved it using uc_discount framework (patched) and cck, created as many cck field for the prices and assigned them with conditional actions using triggers provided by discount framework

longwave’s picture

There is no Views integration at present. I can see use cases for providing VIews fields for each enabled price role and an extra field that dynamically shows the sell price for the current role, but have no time or inclination to implement this at present. Patches are welcome, however!

Stevo_0’s picture

=(

Ill reiterate what was said above, and say its fairly critical to have views integration.

But I can also say I appreciate how much time and effort can go into building these modules (having done only some basic work in this area).

kevinquillen’s picture

I would, but I don't know how to write views plugins like I used to for Views 1. Wish I had time to learn.

kcyarn’s picture

I need this functionality as well. I plan to start working on the Views integration this weekend. Anyone want to collaborate?

ambereyes’s picture

I am interested in this functionality as well. I am willing to help out, maybe we can start with the existing functionality in uc_views.

How far did you get kcyarn?

ambereyes’s picture

So in scratching my own itch I used views_customfield plus the patch from #911110: Views arguments in PHP code field and borrowed code from all over the place (uc_atrribute, uc_views, uc_price_per_role) to create my Sell Price field as seen in the two attached images.

<?php
  $combination = unserialize($data->uc_product_adjustments_combination);
  $vid = $data->node_revisions_vid;
  $nid = $data->nid;

  $result = db_query("SELECT rid, price 
                      FROM {uc_price_per_role_prices} 
                      WHERE vid = %d", $vid);
  $prices = array();
  while ($row = db_fetch_object($result)) {
     $prices[$row->rid] = $row->price;
  }

  $original_price = $data->sell_price;
        
  $price = uc_price_per_role_find_role_price($prices, $args[0]);
  if ($combination['1'] > 1) {

    $result1 = db_query("SELECT rid, price 
                         FROM {uc_price_per_role_option_prices} 
                         WHERE nid = %d AND oid = %d", 
                         $nid , $combination['1']);
    $options = array();
    while ($row = db_fetch_object($result1)) {
       $options[$row->rid] = $row->price;
    }

    $option = uc_price_per_role_find_role_price($options, $args[0]);
    if ($price) $price += $option;

   }
  if ($price == FALSE ) {    

     $price = db_result(db_query("SELECT sell_price 
                                  FROM {uc_products} 
                                  WHERE nid = %d 
                                  AND vid = %d", 
                                  $nid, $vid));
     
     $offset = db_result(db_query("SELECT price 
                                      FROM {uc_product_options} 
                                      WHERE nid = %d AND oid = %d", 
                                      $nid, $combination['1']));
     $price += $offset;
   }

   setlocale(LC_MONETARY, 'en_US');

   return money_format('%#10n', $price);
  
?>

YMMV ... [edit] and oh yeah .. plus uc_multibuy :-)

adam_b’s picture

subscribe

ColinMctavish’s picture

subscribing

Cablestein’s picture

I'm getting by with the following code, thanks to ambereyes help, using in Views custom PHP field:

$nid = $data->nid;

$result = db_query("SELECT rid, price FROM {uc_price_per_role_prices} WHERE nid = %d", $nid);

while ($row = db_fetch_object($result)) {
     echo t("Member price: ");
     echo sprintf('$%01.2f', $row->price);
} 

Don't know if I need anymore, but this is doing fine for me so far.

kruser’s picture

@Cablestein - Thanks for the nice snippet

I added my own twist to show the role price based on the current user's role.

<?php
$nid = $data->nid;

$result = db_query("SELECT rid, price FROM {uc_price_per_role_prices} WHERE nid = %d", $nid);

while ($row = db_fetch_object($result)) {
     if ((array_key_exists($row->rid, $GLOBALS['user']->roles))):
     	echo t("Member price: ");
		echo sprintf('$%01.2f', $row->price);
	 endif;
} 
?>
jonathan_hunt’s picture

Here is a start at Views integration. It needs a handler(?) to select which Role to show.

Index: modules/uc_price_per_role/uc_price_per_role.module
===================================================================
--- modules/uc_price_per_role/uc_price_per_role.module	(revision 22158)
+++ modules/uc_price_per_role/uc_price_per_role.module	(working copy)
@@ -1,5 +1,14 @@
 <?php
 // $Id: uc_price_per_role.module,v 1.10 2010/10/23 21:34:22 longwave Exp $
+/**
+ * Implements hook_views_api().
+ */
+function uc_price_per_role_views_api() {
+  return array(
+    'api' => 2,
+//    'path' => drupal_get_path('module', 'uc_price_per_role'),
+  );
+}
 
 /**
  * Implementation of hook_perm().
Index: modules/uc_price_per_role/uc_price_per_role.views.inc
===================================================================
--- modules/uc_price_per_role/uc_price_per_role.views.inc	(revision 0)
+++ modules/uc_price_per_role/uc_price_per_role.views.inc	(revision 0)
@@ -0,0 +1,41 @@
+<?php
+/**
+ * @file
+ * Expose UC Price per Role prices to Views.
+ */
+
+ /**
+  * Expose uc_price_per_role_prices to Views.
+  * Implements hook_views_data().
+  */
+function uc_price_per_role_views_data() {
+  $data['uc_price_per_role_prices']['table']['group'] = t('Product');
+
+  $data['uc_price_per_role_prices']['table']['join'] = array(
+    // Index this array by the table name to which this table refers.
+    // 'left_field' is the primary key in the referenced table.
+    // 'field' is the foreign key in this table.
+    'node' => array(
+      'left_field' => 'vid',
+      'field' => 'vid',
+    ),
+  );
+
+  // Example numeric text field.
+  $data['uc_price_per_role_prices']['price'] = array(
+    'title' => t('Price (by role)'),
+    'help' => t('Price for a particular role.'),
+    'field' => array(
+      'handler' => 'views_handler_field_numeric',
+      'click sortable' => TRUE,
+     ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+  );
+
+  return $data;
+}
djdevin’s picture

Maybe a filter that has:

( ) User's current role
( ) Role 1
( ) Role 2
( ) Role 3

djdevin’s picture

Version: 6.x-1.0 » 6.x-1.x-dev
Status: Active » Needs review
FileSize
2.89 KB

Here's a working views integration w/handler, rolled against master...should allow a specified role-specific price to display/filter in views.

scottrigby’s picture

Title: Views integration » Views integration (Ubercart Price Per Role)

Changing title so it doesn't get lost with the other gazillion contrib issues by the same title

SeanA’s picture

Status: Needs review » Needs work

Not quite there yet. This doesn't need to *filter* on role prices, it needs the ability to add *fields* for the various role prices. For example, I'd like to build a single admin view that shows the sell price along with the prices for each role for each product.

Also, instead of the generic 'views_handler_field_numeric', it would be nice have the option to use 'uc_product_handler_field_price' to display the price value with the standard Ubercart formatter.

djdevin’s picture

Not sure if we can do that easily. Views doesn't have very good support for a variable number of columns.

I did actually try it with uc_product_handler_field_price but ended up having a lot of issues using it. I'll try it again...

AllyMediaGroup’s picture

Version: 6.x-1.x-dev » 6.x-1.1
Assigned: Unassigned » AllyMediaGroup
Priority: Normal » Critical
FileSize
680 bytes

Applied patch from djdevin It said failed on hunk #1 and gave a rej file in the folder. I have attached the reject file of the module
that I have tried to patch. And I now realize after reading the post by Sean A. He is correct I don't need to filter by the price per role, I need the fields that show on the product page to be displayed on a custom view for bulk operations to be able to make the change to the price per role fields.

djdevin’s picture

Priority: Critical » Normal

Patch was for 6.x-dev, not 6.x-1.1.

AllyMediaGroup’s picture

Version: 6.x-1.1 » 6.x-1.x-dev

Okay I applied the patch to the 6.x-dev version and I still get the same message. Here is the message from terminal

ubuntu@ubuntu:/media/drive/uc_price_per_role$ patch -p0 < 646724-uc_price_per_role-views.patch
patching file b/includes/uc_price_per_role_vhf_role.inc
patching file b/uc_price_per_role.module
Hunk #1 FAILED at 617.
1 out of 1 hunk FAILED -- saving rejects to file b/uc_price_per_role.module.rej
patching file b/uc_price_per_role.views.inc

SeanA’s picture

@AllyMediaGroup: you can probably just edit the file and add those lines by hand.

Eventually I will likely want the same thing as AllyMediaGroup: after getting this Views integration going, it would be super-duper to have a VBO handler as well. Haven't had time to work on either of these tasks myself yet.

AllyMediaGroup’s picture

I have found that Ubercart Product Actions @ http://drupal.org/project/uc_product_actions does everything I need except I need to add the role pricing to the view that I created for bulk editing of orders that I have. It has the VBO for cost, list, price and weight modify. So I was going to add to it the VBO for the role pricing. But I need to be able to add the fields to the view for the role pricing so I can bulk update the pricing using the Ubercart Product Actions.

SeanA’s picture

Yes, that's my plan as well. But a VBO handler will be need to be written to accomplish that. It should probably be separate from Product Actions though.

AllyMediaGroup’s picture

Your probably right, I just don't know how to do the VBO handler separate from the Product actions. I figured that it could be added to the product actions since it has to do with products. If you don't use the price per role then it has nothing to pull from the database. I believe that you can set it so just not show if it is not there, but I am limited in my coding so, I am not sure. So once we have the views integration done to get the editable fields into views then we can either add it to the existing module set to NULL if not available so it won't create any errors.

Cablestein’s picture

I'm still using this snippet... but now I'm I need to render the price properly with ubercart theme price function. How do I call that? theme_uc_price() doesn't fully work?

SeanA’s picture

Cablestein, I believe you want to use uc_price($value) or possibly uc_price($value, $context). It's not very well documented, but if you look thru Ubercart or contrib code (such as uc_price_per_role.module), you can find examples of how to set up the $context array.

see also http://api.ubercart.org/api/function/uc_price

AllyMediaGroup’s picture

I have found a way to pull the fields into views for Price Per Role, using a variation to Cablestein's snippit . Originally it pulled all three role prices into one column that I have set in my site. I found the way to do this using the Customfield: PHPcode. As follows:

<?php
$nid = $data->nid;

$result = db_query("SELECT price FROM {uc_price_per_role_prices} WHERE nid = %d AND rid = 5", $nid);

while ($row = db_fetch_object($result)) {
     echo t();
     echo sprintf('$%01.2f', $row->price);
} 
?>

Where the rid=5" is the role id column in the uc_price_per_role_prices table. This will be different depending on the roles that you have on you site. If you do not include the rid it pulls all the prices for the nid into the same column. I created three custom code fields so that they show separately from one another. One for each role that has custom pricing. Now I just need to create the VBO for updating the pricing for the role prices and my customer will be happy.

djdevin’s picture

This will work, but is very costly, ie. for each row you have another query. A view of 500 products = 500 queries instead of 1 or 2.

Creating a field handler using views_handler_field_prerender_list is probably the best way to go in the long run.

fonant’s picture

Patch in #16 worked quite well for me, so I could list products with a role-specific price for a given role. It might be worth tweaking the filter titles to make them a bit clearer as being from price_per_role module?

goodeit’s picture

I am really close to having a working D7 solution for this. However, my nodes are duplicated as many times as there are role prices for that item.

The db query is returning an extra row for each separate role price in the table. Does anyone know how to bring all of the role prices in on one row? I already have correct role price selection and display worked out.

I am looking into views_handler_field_prerender_list to see if that is a better handler to extend (currently I am using views_handler_field_numeric). However, I appear to be having the same duplication issue there as well.

I'm also concerned that modifying the data after the query will cause the pager to be off (see "Updates" section at http://capellic.com/blog/cure-duplicate-nodes-in-a-view for a description of what I think will happen).

When(/if) this gets worked out, I will explain how I did it. A backport to D6 should be very straightforward, as I am still using Views API v2.

goodeit’s picture

OK, I have a very rough version of this working, I think. It only shows the "correct" price for the current user, as determined by uc_price_per_role. It should be trivial to add an option to display all role prices for each item.

First, add these lines to uc_price_per_role.info:

files[] = includes/uc_price_per_role.views.inc
files[] = includes/uc_price_per_role_handler_field_prerender_list.inc

Then, add this to uc_price_per_role.module:

/**
 * Implementation of hook_views_api().
 */
function uc_price_per_role_views_api() {
  return array(
    'api' => 2,
	'path' => drupal_get_path('module', 'uc_price_per_role') . '/includes',
  );
}

Then put the two files I've attached in a subfolder of uc_price_per_role called includes (make sure they are named uc_price_per_role.views.inc and uc_price_per_role_handler_prerender_list.inc - with no extra _s or .txt)

There may be a few other small changes I have missed, so let me know if you encounter any problems with it.

I would really appreciate any feedback, and like I said, it should work without needing many changes on D6.

SeanA’s picture

Thanks for posting your code. Testing.

SeanA’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev

@goodeit, yes, maybe you missed something, it's not working for me: "Notice: Undefined property: stdClass::$_field_data in uc_price_per_role_handler_field_prerender_list->render() (line 39 of uc_price_per_role_handler_field_prerender_list.inc)" and the same on line 42, and of course the role prices aren't showing up in my View.

If possible can you post a diff instead? That would make it easier to patch and test this, and avoid missing some changes.

goodeit’s picture

@SeanA: Here's a diff from 7.x-1.x-dev to my current production code. I'm not sure it will fix the problem you're experiencing though :/ Note that it may have a few other miscellaneous changes in it unrelated to Views integration.

zviryatko’s picture

Assigned: AllyMediaGroup » zviryatko

thx, work fine (:

Ricky999’s picture

Version: 7.x-1.x-dev » 6.x-1.1
Assigned: zviryatko » Ricky999

Hi, i'm trying Goodeit Price per Role solution to use it with editView but it seems doesn't work.

My error is:

Error: handler for uc_price_per_role_prices > price doesn't exist!

can u help me?

goodeit’s picture

Version: 6.x-1.1 » 7.x-1.x-dev
Assigned: Ricky999 » goodeit

@zviryatko: Can you give some details of what is working for you? Are you using my patch, the module I posted, or someone else's code? What version of Drupal and views are you working with?

@Ricky999: the code I posted is for D7. I unfortunately don't have time to port it back to D6 at this time, but as I said, it should be pretty straightforward. Looking at existing views field code is a great place to start.

Ricky999’s picture

Version: 7.x-1.x-dev » 6.x-1.1

I'm trying for D6 but nothing.....could someone help me?????I need to set Retailers in Edit View

Ricky999’s picture

Component: Code » Documentation
Assigned: goodeit » Unassigned
Category: feature » support

Could anyone have code for D6? I need to integrate my roles in EditView

goodeit’s picture

Version: 6.x-1.1 » 7.x-1.x-dev
Component: Documentation » Code
Assigned: Unassigned » goodeit
Category: support » feature

@Ricky999, please stop changing the issue parameters. It's fine to ask for help, but more than one post in a row is not necessary.

silverwing’s picture

@Ricky999, The users are working on this issue for a specific version of the module. If you still need help, please open a new issue. Continuing to change versions and components and curse at them will only irritate the people you are asking for help, but can also lead to your account being blocked.

Comment #43 unpublished.

See this issue for the webmaster request for our involvement.

~silverwing - drupal.org site maintainer

DenimTornado’s picture

Nice! It's works for me too! Thx!

DanZ’s picture

Can anyone make a working patch out of #33?

allexx’s picture

Version: 7.x-1.x-dev » 6.x-1.1
Assigned: goodeit » Unassigned
Category: feature » task
FileSize
1.69 KB
298 bytes
1.55 KB

To contionue goodeit work, I'd like to share solution for exposed filter.
I tested with between condition.

uc_price_per_role.info add:
files[] = views_handlers/uc_price_per_role.views.inc

uc_price_per_role.module add:
function uc_price_per_role_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'uc_price_per_role') . '/views_handlers',
);
}

DanZ’s picture

Version: 6.x-1.1 » 7.x-1.x-dev
Category: task » feature

Please leave this on 7.x-1.x-dev. If you would like to do this for 6.x, please make a new issue. Also, please submit it as a patch. See https://drupal.org/project/uc_price_per_role/git-instructions for instructions on making a patch.

Note that there is no active maintainer for 6.x. If you would like to help, please reply to #1899398: Maintainer needed for 6.x-1.x.

This issue is planned for 7.x-1.1. I just have many other projects ahead of it. It would happen sooner if someone made a patch.

rjlang’s picture

Per #48, attached is a patch against 7.x-1.x-dev.

This is a little different from goodeit's #36. It creates a new field in the Product group, "Particular role price," that lets you choose the role to display in the view handler settings, and there is a special choice for "Applicable price" that displays the relevant price for the viewing user. So, for example, you could make a View with columns like this:

Retail price -- Wholesale price -- Your price

Each column would be the same field, but with different field handler settings to chose the relevant role and the last column is "Applicable price."

The Views integration also includes field definitions for role ID and role price from the uc_price_per_role table.

Poieo’s picture

@rjlang - I've tried out your patch and everything works great, except that I can only get the first 'price per role' to display in a view. The remaining products in the view are blank. Seems like a foreach issue or something...

edit: Only the first price shows when you're viewing the default price. i.e. - you're not logged in with a role. When logged in with a role the prices for all products in the view are visible.

rjlang’s picture

Ah, the edit was helpful. Thanks, I can reproduce this. Will look into it,

rjlang’s picture

OK, found it. (D'oh. Headsmack.) Attached is a revised patch against 7.x-1.x-dev.

If you've previously applied the patch from #49, just change the last 4 lines of uc_price_per_role_handler_field_role_price.inc from

        }
        $this->field_alias = $new_alias;
      }
   }
}

to

        }
      }
      $this->field_alias = $new_alias;
   }
}
Poieo’s picture

Status: Needs work » Reviewed & tested by the community

Works great. Thanks! Let's see if we can get it committed.

PetarB’s picture

Can anyone please provide the 'patched' file here. I can't do patches sorry! :(
Edit: well I should say that I tried, but it's not working, and throwing up 'hunk failed' errors in the patching.

SeanA’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
7.12 KB

Here's a re-worked patch, it's the same content as in #52, just formatted so that 'git apply' works cleanly with it. I haven't tried out the functionality yet, but thank you for working on this. Changing status back to 'needs review'.

PetarB’s picture

I'm still have trouble applying the patch with this one (OSX + Terminal). Can you post the patched module?

PetarB’s picture

Just got a working version of this to test. Works very nicely in views now, with good flexibility for different requirements. As far as the user experience goes (I'm not coder) it seems ready to go for a commit.

DanZ’s picture

Sorry, I've been away (moving house). Thank you for your work on this.

SeanA’s picture

FileSize
59.97 KB
24.77 KB

It seems to be working well so far. In my demo store there are just 2 products and price-per-role is set up for 'distributor' and 'admin' roles. Will continue testing.
role prices

#49 says: "The Views integration also includes field definitions for role ID and role price from the uc_price_per_role table." When these fields are added to a View, multiple rows of some products appear:
role prices

Are these extra fields needed? They Role ID might be useful, but I can't tell if it's working correctly or not. =D The 'Role price' field seems redundant. Maybe get rid of it and rename 'Particular role price' to just 'Role price'? (Shorter help text for that field: "A specified role price as determined by UC Price Per Role.")

rjlang’s picture

@SeanA #59: The difference between Role Price and Particular Role Price is that the latter ALWAYS returns exactly one row, while the former only returns a row if there's a price for that role, or multiple rows, if you've defined multiple role prices for a product.

So what's the use case for the Role Price field? In combination with a filter on Role ID, you could make a view that shows you ONLY the products for which you've defined a particular role price.

I can't think of a use case where you'd want to use both Role Price and Particular Role Price together, as you've done in your example.

The definitions for fields for Role Price and Role ID are there under the philosophy of "let's expose any field that might be of interest to give users maximum flexibility in building their views." The definition for Particular Role Price field is there under the philosophy of "this is what most users probably actually want to see."

HTH,

Robert

P.S. To tell if Role ID "is working correctly", see if the listed Role ID (e.g., 4) matches the price you've set for your product (e.g., "Daily Subscription") for that role. Looking at your screen shots, it looks like it is.

SeanA’s picture

Status: Needs review » Reviewed & tested by the community

Sure, I figured that out, but what confused me was the 2 extra identical rows in the second screenshot... using 'Distinct' on the query gets rid of the extra rows, so that was a basic Views issue.

After more testing logged in with various roles it looks like everything is working very well. I'll try backporting the patch to 6.x.

DanZ’s picture

I should have time to review the 7.x code this weekend.

esquareddesign’s picture

For the life of me, I can not get this patch to work. I had to patch the file manually. Am I missing something?

sirish.ayyagari’s picture

:uc_price_per_role >$ patch -p1 < views_integration-646724-55.patch
patching file uc_price_per_role.info
Hunk #1 FAILED at 4.
1 out of 1 hunk FAILED -- saving rejects to file uc_price_per_role.info.rej
patching file uc_price_per_role.module
patching file views/uc_price_per_role.views.inc
patching file views/uc_price_per_role_handler_field_role_price.inc

tj99ay’s picture

Issue summary: View changes

Anyone been able to use #13's solution but for Drupal 7? I tried this variant:

  $result = db_query("SELECT oid, rid, price FROM {uc_price_per_role_option_prices} WHERE nid = :nid", array(':nid' => $nid));
  foreach ($result as $row) {
    print $row->oid . ' ' . $row->rid . ' ' . $row->price;
  }

but got this error:

Notice: Undefined variable: nid in __lambda_func() (line 1 of /etc/drupal/7/sites/all/modules/views_php/plugins/views/views_php_handler_field.inc(131) : runtime-created function).

Cheers

SeanA’s picture

sirish.ayyagari, you have to examine the contents of the .rej (reject) file to see where the problem is. This patch creates new files... are you using 'patch' properly? In any case, you should prolly use 'git apply' instead.
https://www.drupal.org/project/uc_price_per_role/git-instructions

SeanA’s picture

tj99ay, does that (#13) accomplish something that can't be done with the current patch? It's been a while since I looked at this.

tj99ay’s picture

SeanA, how rude of me not to reply. Sorry I can't remember now, so long ago. I was trying to find a way to allow my client to edit lots of product prices at once via a spreadsheet then import (they are used to doing this), but as we used UPPR it stuffed our plans a bit. Still trying to find a way though, but it looks like we'll be needing to create a custom module at some point.

tgyuksel’s picture

for View entegration display Price or Sell Price ... If select display price eevrybody will see price ( price = default price as user per role price ) ... don't select SELL PRICE .... For per role price in view select DISPLAY PRICE ...

tgyuksel’s picture

Arkadaşlar view ile per role price seçilmiş üyelere göre otomatik görüntületmek için view düzenlerken DIPLAY PRICE seçiniz ! Eğer SELL PRICE seçerseniz per Role price tek fiyat gösterecek ve çalışmayacaktır.