Advertisement
KunalkaushikV

Untitled

May 27th, 2025
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/javascript">
  2.   window.unbxd_recommendation_pageInfo_new = <?= json_encode($block->getPageInfo()); ?>;
  3.  
  4.   /**
  5.    * 1) Promise that resolves when the SDK global appears
  6.    */
  7.   function waitForSdk() {
  8.     return new Promise(resolve => {
  9.       if (window.getUnbxdRecommendations || window._unbxd_getRecommendations) {
  10.         return resolve();
  11.       }
  12.       const timer = setInterval(() => {
  13.         if (window.getUnbxdRecommendations || window._unbxd_getRecommendations) {
  14.           clearInterval(timer);
  15.           resolve();
  16.         }
  17.       }, 200);
  18.     });
  19.   }
  20.  
  21.   /**
  22.    * 2) Promise that wraps the SDK call and resolves with the templateData
  23.    *    before the SDK actually renders the widget.
  24.    */
  25.   function initRecsAsPromise(cfg) {
  26.     return new Promise((resolve, reject) => {
  27.       // override dataParser to catch the payload
  28.       const originalParser = cfg.dataParser;
  29.       cfg.dataParser = data => {
  30.         console.log('[PROMISE] raw payload:', data);
  31.         resolve(data);
  32.         // restore original parser in case SDK re-uses it
  33.         cfg.dataParser = originalParser;
  34.         return originalParser ? originalParser(data) : data;
  35.       };
  36.  
  37.       try {
  38.         // instantiate v3 ES6 class if available
  39.         if (typeof window.getUnbxdRecommendations === 'function') {
  40.           new window.getUnbxdRecommendations(cfg);
  41.         } else if (typeof window._unbxd_getRecommendations === 'function') {
  42.           window._unbxd_getRecommendations(cfg);
  43.         } else {
  44.           throw new Error('Unbxd Recs entrypoint not found');
  45.         }
  46.       } catch (e) {
  47.         reject(e);
  48.       }
  49.     });
  50.   }
  51.  
  52.   /**
  53.    * 3) Wire it all up
  54.    */
  55.   waitForSdk()
  56.     .then(() => {
  57.       console.log('> SDK ready, initializing recommendations…');
  58.  
  59.       // ensure eventQueue exists for any hook registrations
  60.       window.eventQueue = window.eventQueue || {};
  61.  
  62.       // register any before/after hooks you still want
  63.       window._unbxd_registerHook('beforeTemplateRender', d => {
  64.         console.log('[HOOK] beforeTemplateRender ➞', d);
  65.         return d;
  66.       });
  67.       window._unbxd_registerHook('afterTemplateRender', v => {
  68.         console.log('[HOOK] afterTemplateRender ➞', v);
  69.       });
  70.  
  71.       // build the config exactly as you were doing
  72.       const cfg = {
  73.         widgets: {
  74.           widget1: { name: 'widget1' },
  75.           widget2: { name: 'widget2' },
  76.           widget3: { name: 'widget3' }
  77.         },
  78.         userInfo: {
  79.           userId:   readCookie('unbxd.userId') || 'guest',
  80.           siteKey:  '<?= $siteKey ?>',
  81.           apiKey:   '<?= $apiKey ?>'
  82.         },
  83.         pageInfo: window.unbxd_recommendation_pageInfo_new,
  84.         dataParser: data => data,          // placeholder—will be overridden
  85.         itemClickHandler: product => {
  86.           console.log('[HOOK] itemClickHandler ➞', product);
  87.           if (product.productUrl) window.location = product.productUrl;
  88.         }
  89.       };
  90.  
  91.       // call the SDK and wait for the payload
  92.       return initRecsAsPromise(cfg);
  93.     })
  94.     .then(templateData => {
  95.       console.log('✅ Recommendation payload received. You can now render or inspect:', templateData);
  96.       // At this point the SDK will continue and render its templates.
  97.     })
  98.     .catch(err => {
  99.       console.error('❌ Unbxd Recs init error:', err);
  100.     });
  101.  
  102.   // Simple cookie reader
  103.   function readCookie(name) {
  104.     return document.cookie
  105.       .split('; ')
  106.       .reduce((v, p) => {
  107.         const [k, val] = p.split('=');
  108.         return (k === name) ? decodeURIComponent(val) : v;
  109.       }, null);
  110.   }
  111. </script>
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement