Add inline widget to product display

I am trying to integrate our Drupal 7 Commerce install with Digital River’s where-to-buy widget.

All I need to do is add a little JS to the product page, with a script variable set to the product sku. On page load, this inline JS runs and adds a button that when clicked will pop-up a lightbox with where-to-buy information. This script has a variable that requires a SKU to be entered. If I add the snippet to my node-template it works fine.

HOWEVER, I have several products on each product display, and I have 2 product attributes that can be selected. This, of course, refreshes the node/entity field values in my page, like the title, a product image and so forth. This will not refresh the sku parameter of my javascript snippet though, so my WTB button does not update. IE, if I did something like this:

<script type="text/javascript">           pswtb.loader.show({             widgetConfigurationId: "3a4acc53-216e-48da-8e92-bfaeb58168a7",              sku: "US_<?php print trim($content['product:sku']['#markup']) ?>"            });          </script> 

I discovered that it would initially load but the product:sku wouldn’t change as I selected a new attribute.

So, I also discovered that my values would update if they were displayed via the render() function. So, if I just did this:

<?php print render($content['product:sku']) ?> 

I would get an element that updates ‘properly’ when a product attribute is selected. It does, however come with a lot of div’s wrapping it, so I can’t use it as a parameter in my script,and I didn’t want to override the ‘sku’ field template because I need that sku elsewhere on the page, and even overriding the template, I would still get a wrapper around it.

Ok, so my next attempt was to create a ‘virtual’ field, using the virtual_field module. This just lets you have a dummy field that doesn’t get stored anywhere but still lets you create custom templates and specify display formatters. I created a dummy field that just contains the product sku.

Using this, I created a custom field template (field–field-product-wtb.tpl.php) for this virtual field like so:

this will update: <?php print render($item); ?> but this gets stripped out: <script type="text/javascript">    pswtb.loader.show({      widgetConfigurationId: "3a4acc53-216e-48da-8e92-bfaeb58168a7",       sku: "US_<?php print render($item); ?>"     });  </script> 

And then in my node template, I just do this:

<?php print render($content['product:field_product_wtb']) ?> 

It works on initial load, and when a product attribute is selected, the sku does change (the ‘this will update’ part) but the entire script tag gets stripped out and my button disappears. I guess because it gets filtered via some xss protection, but I haven’t tracked it down. As near as I can tell, this is because when content on a page is updated via ajax, the HTML returned from a tpl file is sanitized and things like script tags are filtered out.

Now I’m at a loss as to how to get this to work.

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

Add inline widget to product display

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.