How to get the highest value of a specific field in a multivalue field collection

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.

FIELD COLLECTION DISCOUNT OPTIONS

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

How to get the highest value of a specific field in a multivalue field collection

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.