How to get the highest value of a specific field in a multivalue field collection, USING PHP?

This is about displaying the right discount for a marketplace product.

There is a node type Product with two field collection fields: (1) field_discount which contains: - a field for the minimum quantity required for the discount - a field for the discount percentage (2) field_price which contains: - a field for the base price - a field for the discounted price

The reason why (2) is also a field collection, is because I want to allow the marketplace sellers to show multiple price options. This might be relevant information because it seems that the views reference filter module does not work with field collections. Using that module would have made this issue a lot easier. Instead, this question focuses on using PHP, unless someone has another solution that does not require or requires less PHP coding.

The discount price needs to be built on the fly when a product node is displayed, using the raw quantity value from the URL and calculating the discount. My solution was to use a jump menu view with numbers 1 to 1000, which adds the quantity to the URL, like http://xxxx.com/node/100/5 in which the "5" as product quantity is inserted by using the jump menu. However, this might not be the most efficient method in the context of this issue. You are free to suggest another approach if that serves a more efficient solution to this issue.

STORING DISCOUNT AS VARIABLE Only after the customer sees the discount price and clicks ORDER (a flag button), the transaction values are stored in transaction entity fields. Therefore, the transaction values need to be available as variables.

Using PHP code in Global NULL, Session variables. My current approach is to use a Global Null as views contextual filter, default value PHP code, in order to fetch field values and calculate discount, and to store the values as SESSION variables. Since no transaction entity is created yet, I can't see another way to store these values for further use in the (possible) transaction.

My experience with php and metadata wrappers etc is insufficient, especially with regard to fieldcollections.

WANTED: the discount percentage, corresponding with the quantity chosen by the buyer. The discount percentage must be within the range of quantities specified by the product owner. Solutions with either other Drupal modules, or custom php are both welcome. As mentioned above I tried the views reference filter, using an exposed filter referencing quantity values of the field collection, but I couldn't get that to work.



Try the Computed Field module

Computed Field is a very powerful field module that lets you add a custom "computed fields" to your content types. These computed fields are populated with values that you define via PHP code. You may draw on anything available to Drupal, including other fields, the current user, database tables, you name it. (Feeling the power yet? :) ) You can also choose whether to store your computed field values in the database with other content fields, or have them "calculated" on the fly during node views. (Although you should note that Views use requires database stored values.) This field is literally the Swiss Army knife of fields. So start cooking up your PHP based values!

  • As you mention yourself "you should note that Views use requires database stored values" and the current issue involves calculating 'on the fly' from a contextual value, so I don't see how the computed field can help here.
    – Yuri
    Jul 19 '14 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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