Currently, the Customer Display shows a placeholder product image instead of an image for the actual product. Let's change that.

Things to consider

  • Products don't need images
  • In some situations, a single product image for all items in the Customer Display could suffice.
  • Image fields can be added at the Product Type and/or Product Variation Type levels. There is no set-in-stone image field.

How it could work

The Customer Display settings page could use a new section titled 'Product Images'

Each product variation could have it's own image configuration options. By default, there would be NO image for the variations, however, this can be overridden depending on the options you choose.

Here's a concept that I could see working.

Comments

Hubbs created an issue. See original summary.

hubbs’s picture

Some additional thoughts. The thing I don't really like about this concept is that the form could get really long if you have a bunch of different product variations. Although I suppose as long as each section starts closed it shouldn't be too big of a deal.

hubbs’s picture

Issue summary: View changes
StatusFileSize
new119.51 KB

I talked with smccabe offline and determined that we probably don't need the "Image fields to use" to be so complicated.

Instead, we could probably get away with just a primary image field and fallback image field since it would be set for each individual product variation type basis.

I've updated the image in the issue summary.

sgalata’s picture

Assigned: Unassigned » sgalata
sgalata’s picture

StatusFileSize
new8.24 KB

WIP Patch

How to Review:
Scroll down to the comment // Product Images.

Changes:
1) I added a section to the CustomerDisplaySettingsForm which allows the user to set the image field for each product variation type that will be used when the product variation type appears on the customer display.

2) The image field can be selected from a select form element whose options comes from $image_styles which is an array of all of the available image styles for the site.

3) I get an array of all existing product variation types, which is contained in $product_variation_type_ids. I then add a section to CustomerDisplaySettingsForm for each product variation type by iterating over $product_variation_type_ids in a foreach loop. For each product variation type I get it's label to be used in titles for the form. I also interate over $image_styles in order to properly display the options in the form select element. I placed each product variation type in a form of the type 'details' in case we have many product variation types in which case the form could get very long. The user can set the image field that will be used in the customer display by selecting from the list provided by Primary and Alternate Product Images. The user is encouraged to select an image file to use in the customer display, placeholder image, in case neither of the chosen image fields from the primary and alternate options for the product variation type are found.

4) The placeholder image is of type 'managed_file' as when it is set to simply 'file' the config object is unable to properly save the uploaded file. The upload location was set to public://files, however I am wondering if it should not be set to something else. Perhaps a folder in the commerce_pos_customer_display directory?

5) A checkbox was added in order to allow the user to choose whether they wish to override the options set by the primary and alternate product images with the alternate product image.

6) A config object was created in order to configure the settings for the image field to be used for the product variation type when displayed on the customer display. I used the same same foreach loop as in buildForm so that the values set by the config object match those chosen in the form.
I first checked to see if the checkbox for using the placeholder image was checked as if it was then there
is no need to set the primary and alternate image fields and only the placeholder image field needs to be
set. Only if it was not selected do I proceed to set values in the config object for primary, alternate and placeholder image fields.

7) I added a helper function, getProductVariationTypesIds for getting the ids of all existing product variation types on the site.

Alternative Solutions
1) I initially used $this->entity_field_manager->getFieldMapByFieldType('image') and then iterated over the array that the EntityFieldManager returned. If the entity was of the type commerce_product_variation, then I would proceed to add that product's image fields to the array that would be used as the options for the select element that allows the user the set the image field. I concluded that this wasn't what the issue was suggesting should be done and hence went with $image_styles.

What Needs to Be Done:
1) Use 'state' in the form in order to hide the placeholder checkbox unless a file for the placeholder image has been uploaded.

2) Determine how to check whether the product variation type or it's parent product type have the selected image fields. I am thinking I can use something similar to $image_styles to achieve this.

3) Determine how to use the values in the config object to display the product variation type with the selected image field in the customer display.

4) Write Tests for CustomerDisplaySettingsForm

gmem’s picture

Status: Active » Needs work

I'll give this a full functionality test soon but a couple notes about the patch:

+  public function buildForm(array $form, FormStateInterface $form_state)
+  {

Consider in-lining the { to match the Drupal coding standards, if I remember right the Drupal CI will fail the patch. Same goes for comments - they should end with proper punctuation. Might also want to add @return array to the getProductVariationTypesIds() function PHPDoc comment.

Besides those nitpick I think the patch looks okay, I'll test functionality shortly :)

sgalata’s picture

StatusFileSize
new9.37 KB

WIP Patch: Update 2

Changes Since First Patch

* Fixed opening braces to match Drupal coding standards
* Added @return array to docblock for getProductVariationTypesIds() function
* Added commerce_pos_customer_display.product_image_settings to getEditableConfigNames() in order to set
default values according to previously set options.
* Added properties to placeholder in order to use core's image_widget
* Added validateForm() function in order to confirm that if the checkbox to only use the placeholder image is
checked, then a placeholder image has been uploaded.

Alternative Solution to Using validateForm()

I considered using form states which would hide the placeholder only checkbox unless there was a placeholder image file uploaded. Unfortunately I could not get this to work, I am thinking it is a bit more complicated if the selector in the form state is a managed file.

What Needs to Be Done:

1) Determine how to check whether the product variation type or it's parent product type have the selected image fields. I am thinking I can use something similar to $image_styles to achieve this.

2) Use the image field that was set in CustomerDisplaySettingsForm in the customer display.
Potential Solution
I will need to load the commerce_pos_customer_display.product_image_settings config in
the controller, Display.php. Here is where I will determine what image field will be used in
the customer display. Once that is known, I will set the URL to the image in a variable that I will then send to
the JS file that handles the output of the customer display.

2) Write Tests for CustomerDisplaySettingsForm

sgalata’s picture

StatusFileSize
new20.25 KB
new27.27 KB

WIP Patch: Update 3

Changes Since Second Patch

  • Logic to Determine Image Field

    Added code to the commerce_pos_customer_display_form_commerce_pos_alter hook. I decided to add my logic for setting the image field to be used in the customer display here as this hook is invoked each time something changes in the commerce_pos form. For example: a new order item is added to the POS order; the quantity of an order item is increase or decreased, etc. When this hook is invoked I iterate over each order item in the current POS order. I use a service that I created, ProductImages, to determine which image field to use to display the order item in the customer display. The url of the image field is then sent to the JS register file as a php variable.
    Alternative Solution
    I had considered using the Display controller to determine what image field would be used in the customer display. However, I realized this wouldn't work as the controller is only called when the customer display is first loaded.

  • Service with Methods to Help Determine Image Field

    I created a custom service, ProductImages, with method that help to determine the image field to be used to render the order item in the customer display.
    Alternative Solution
    None were considered.

What Needs to Be Done

  • Set the Src Attribute of the Image Element

    The image url that is passed as a php variable to commerce_pos_customer_display_register.js can be added to the item array. This array is then sent to commerce_pos_customer_display.js which handles rendering the order item/items in the POS order. Here I can use JS functions to set the src attribute of the image element for the order item's image to the image url that was sent from the commerce_pos_customer_display_form_commerce_pos_alter hook.

  • Write Tests for CustomerDisplaySettingsForm

sgalata’s picture

StatusFileSize
new31.46 KB

WIP Patch: Update 4

Changes Since Third Patch

  • Added image url of customer display product image to form array of the Commerce POS form.
  • In commerce_pos_customer_display_register.js, added the product image url to the item array.
  • In commerce_pos_customer_display.js, added the product image url to the src attribute of the image element where the product image will be rendered.
  • Wrote logic to handle special cases (eg. Product has not has it's customer display image configured, product doesn't need an image, etc.).
  • Improved ProductImage's methods to help implement the aforementioned changes.

What Needs to Be Done

  • To be reviewed.
sgalata’s picture

Status: Needs work » Needs review
joshmiller’s picture

Status: Needs review » Needs work

Not a full review, but leaving one thing I noticed here.

+++ b/modules/customer_display/src/ProductImages.php
@@ -0,0 +1,166 @@
+  public function productImageType(ProductVariation $product_variation, $product_variation_type) {
...
+  public function primaryImageFieldUri(ProductVariation $product_variation) {
...
+  public function alternateImageFieldUri(ProductVariation $product_variation) {
...
+  public function placeholderImageFileUri($product_variation_type) {
...
+  public function usePrimary(ProductVariation $product_variation, $product_variation_type) {
...
+  public function useAlternate(ProductVariation $product_variation, $product_variation_type) {

Could probably use some optimization in the service. Reduce arguments and make it a property of the service.

sgalata’s picture

Status: Needs work » Needs review
StatusFileSize
new30.96 KB
new12.81 KB

Patch Description

  • Made sure that all calls to services in ProductImages are made with dependency injection.
  • Added product variation as a property of ProductImages to reduce arguments
jseniuk’s picture

Status: Needs review » Reviewed & tested by the community

Tested and working