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
Comment #2
hubbs commentedSome 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.
Comment #3
hubbs commentedI 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.
Comment #4
sgalata commentedComment #5
sgalata commentedWIP Patch
How to Review:
Scroll down to the comment
// Product Images.Changes:
1) I added a section to the
CustomerDisplaySettingsFormwhich 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_styleswhich 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 toCustomerDisplaySettingsFormfor each product variation type by iterating over$product_variation_type_idsin 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_stylesin 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 thecommerce_pos_customer_displaydirectory?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
buildFormso 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,
getProductVariationTypesIdsfor 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 theEntityFieldManagerreturned. If the entity was of the typecommerce_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_stylesto 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
Comment #6
gmem commentedI'll give this a full functionality test soon but a couple notes about the patch:
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 arrayto thegetProductVariationTypesIds()function PHPDoc comment.Besides those nitpick I think the patch looks okay, I'll test functionality shortly :)
Comment #7
sgalata commentedWIP 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_stylesto 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_settingsconfig inthe controller,
Display.php. Here is where I will determine what image field will be used inthe 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
Comment #8
sgalata commentedWIP Patch: Update 3
Changes Since Second Patch
Logic to Determine Image Field
Added code to the
commerce_pos_customer_display_form_commerce_pos_alterhook. 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 thecommerce_posform. 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
Displaycontroller 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.jscan be added to the item array. This array is then sent tocommerce_pos_customer_display.jswhich handles rendering the order item/items in the POS order. Here I can use JS functions to set thesrcattribute of the image element for the order item's image to the image url that was sent from thecommerce_pos_customer_display_form_commerce_pos_alterhook.Write Tests for CustomerDisplaySettingsForm
Comment #9
sgalata commentedWIP Patch: Update 4
Changes Since Third Patch
commerce_pos_customer_display_register.js, added the product image url to the item array.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.What Needs to Be Done
Comment #10
sgalata commentedComment #11
joshmillerNot a full review, but leaving one thing I noticed here.
Could probably use some optimization in the service. Reduce arguments and make it a property of the service.
Comment #12
sgalata commentedPatch Description
ProductImagesare made with dependency injection.Comment #13
jseniuk commentedTested and working