CMS Development

ckingg
Participant

Get specific section or text from URL with request.query

I need to pass the last section of a URL into a HUBL dictionary. 

I can do this by: 

 

 

 

{% set URL = request.query %}

 


And then setting {{URL}} to a dictionary value like so. 

 

 

{% set myPlatform = [
{
'name': 'UNIVERSAL macOS/WINDOWS',
'platform': 'Universal',
'description': 'License works on macOS or Windows',
'paddleProductId': 12345,
'coupon': '{{URL}}'
},

 


This will give me the all the text after the first backslash in the URL

Now, what I need to do, for example, is when someone goes to 
mysite.com/coupon=COUPONCODE I need to figureout how to get whatever just the COUPONCODE text may be so I can apply that as variable for the dictionary value. 

I could do this with JS:

let couponString =
window.location.href.substring(window.location.href.lastIndexOf('/') + 1);
let couponCode = couponString.replace('coupon=', '');


But then I'm not sure how to pass the {{URL}} variable here to grab the specific text I need. 

0 Upvotes
3 Replies 3
ckingg
Participant

Get specific section or text from URL with request.query

Thanks for the tip @Ntbrown

This is for an integration with Paddle. And this specifically: https://developer.paddle.com/guides/how-tos/pricing/coupons#using-paddlejs

I'm running this JS function to apply my coupon 

 

function openCheckout(e) {
  let id = e.getAttribute('data-prod-id');
  let couponGet = e.getAttribute('data-coupon');

  Paddle.Checkout.open({ 
    product: id,
    coupon: couponGet
  });
}

 


I need to get the "data-coupon" attribute to apply the coupon to the product automatically (this is within our Paddle-generated modal) when the user goes to http://store.mystore.com/?coupon=xxxxxxxx 

I belive this would be the actualy URL structure vs the example in my intial quesiton. 

So I need the data-coupon attribute in this example to be "xxxxxxxx"

I'm applying the coupon text to the data-coupon attribute with:

 

{% set path = request.query %}
{% set couponString = path|split('coupon=','2')|last %}

{% set myPlatform = [
  {
    'name': 'UNIVERSAL macOS/WINDOWS',
    'platform': 'Universal',
    'description': 'License works on macOS or Windows',
    'paddleProductId': 12345,
    'coupon': '{{couponString}}'
  },

 


and rendering it in the HTML (with some loops and if statements) with

 

 data-coupon = "{{ platform.coupon }}" 

 


Would your suggestion be a better solution here? 

Ntbrown
Contributor

Get specific section or text from URL with request.query

Simpler way: request.query_dict.coupon if using proper querystrings w/ url of form:  mysite.com?coupon=COUPONCODE Although with these types of requests you're almost always exclusively posting rather than getting, but unclear what you're doing precisely so I list it on the off chance it makes your code cleaner.

ckingg
Participant

Get specific section or text from URL with request.query

I think I figured it out in case anyone else is having this problem. 

{% set path = request.query %}

{% set couponString = path|split('coupon=','2')|last %}

{% set myPlatform = [
  {
    'name': 'UNIVERSAL macOS/WINDOWS',
    'platform': 'Universal',
    'description': 'License works on macOS or Windows',
    'paddleProductId': 12345,
    'coupon': '{{couponString}}'
  },