如何在确认屏幕上访问Big Commerce上的订单数据

保罗

我需要向订单确认页面添加一些JavaScript,其中包含有关订单的详细信息。尽管我可以通过BigCommerce全局变量访问订单ID,但是我无法确定如何将其余的订单详细信息获取到我的JavaScript中。

例如,我可以全局访问BigCommerce order_id%%GLOBAL_OrderId%%并将其用于JavaScript警报中,但我还需要访问以下内容:

  1. 合计订单
  2. 订单税
  3. 订单运送
  4. 订购邮编

并依次订购产品

  1. product_id
  2. 单价
  3. 数量

那里有这些全局项目,但是当我尝试访问它们时它们为空白,我想我需要遍历购物车中的内容。

  1. %% GLOBAL_ProductModel %%
  2. %% GLOBAL_ProductPrice %%
  3. %% GLOBAL_ProductQty %%

我已经阅读了所有可以找到的文档。谁能给我一个关于如何实现这一目标的想法。我需要这些值,以便可以将它们传递给第三方JS函数以供使用。所有这些都在等待和准备就绪,但是我无法从Big Commerce模板系统中获取数据。社交共享面板读取数据时,数据位于order.html模板页面上,但是我再也看不到社交共享代码段如何访问它。

用户名

我为您创建了一个hacky脚本,用于提取产品数据(以及一些订单详细信息)。

它分析从数据%%GLOBAL_ConversionCode%%模板变量,因此该脚本应在插入order.html后立即%%GLOBAL_ConversionCode%%变量。

具体来说,%%GLOBAL_ConversionCode%%输出到:

<!-- Include the conversion tracking code for all analytics packages -->
<!-- Start conversion code for analytics_googleanalytics -->
<script type="text/javascript">
  if(typeof(pageTracker) != 'undefined') {
    pageTracker._addTrans(
      '196',
      'store-name',
      '0.00',
      '2.12',
      '1.92',
      'Austin',
      'Texas',
      'United States'
    );

    pageTracker._addItem(
      '196',
      '2004',
      'TAKE YOUR TIME: Sweet Body Butter',
      '',
      '24.96',
      '1'
    );

    pageTracker._trackTrans();
  }
</script>

解:

<script>
    //-------------- Main --------------//

    //** Create the order data array from analytics script **//
    var data = parseAnalyticsData(getAnalyticsScript());
    //console.log(data);

    /**
     * Retrieve the order details as an object, properties are:
     * id          - The order ID.
     * shipping    - The order shipping cost. 
     * tax         - The order tax cost. 
     * shippingTax - The order shipping tax cost.
     * city        - The order shipping city.
     * state       - The order shipping state. 
     * country     - The order shipping country.
     */
    var orderDetails = getOrderDetails(data);

    console.log("Order ID = %d", orderDetails.id);
    console.log("Order shipping city = %s", orderDetails.city);
    console.log("Order subtotal = %f", orderDetails.subtotal);

    /**
     * Retrieve the order product details, as an array of product objects. 
     * Properties are:
     * id          - The product ID. 
     * description - The product description.
     * tax         - The product tax cost.
     * price       - The product price per product. 
     * qty         - The product quantity purchased. 
     */
    var products = getOrderProducts(data);

    //** Loop through the products array to access each product **//
    console.log("Total number of products = %d", products.length);
    for (x=0; x<products.length; x++) {
      console.log("--------");
      console.log("Item # ", x+1);
      console.log("Product ID = %f", products[x].id);
      console.log("Product QTY = %f", products[x].qty);
      console.log("Product Price = %f", products[x].price);
      console.log("--------");
    }



    //-------------- Functions --------------//

    /**
     * Parses the DOM to retrieve the order data analytics script.
     */
    function getAnalyticsScript() {
      var scripts = document.getElementsByTagName('script');
      var thisScriptTag = scripts[scripts.length - 2];
      var data = thisScriptTag.textContent || thisScriptTag.innerText;
      return data;
    }

    /**
     * Parses the raw analytics script element to remove all script
     * text, and parse just the order related data into an array.
     * @param script <String> - The raw order analytics script.
     * @return <mixed> - Array containing the order data. 
     */
    function parseAnalyticsData(data) {
      String.prototype.replaceAll = function(search, replacement) {
        var target = this;
        return target.split(search).join(replacement);
      };
      // This is hacky, and probably inefficient, but it removes all
      // script related text, so the end result is just a comma separated
      // array of the order and product data. 
      data = data.replace("if(typeof(pageTracker) != 'undefined') {", '');
      data = data.replaceAll( 'pageTracker._addTrans(', '');
      data = data.replaceAll( ' pageTracker._trackTrans();', '');
      data = data.replaceAll( 'pageTracker._addItem(', '');
      data = data.replaceAll(');', '');
      data = data.replace('}', '');
      data = data.replace( /\n/g, ",").replaceAll( ",,",",");
      data = data.replace(/\s/g,'');
      data = data.split(',');
      data = cleanArray(data); // Remove all empty values from array. 
      return data;
    }

    /**
     * Removes all empty data from array.
     * @param array <mixed> - The array to clean. 
     */
    function cleanArray(array) {
      var newArray = new Array();
      for (var i = 0; i < array.length; i++) {
        if (array[i]) {
          newArray.push(array[i]);
        }
      }
      return newArray;
    }

    /**
     * Parse Analytics Data for Order Details
     * @param data <mixed> - The order analytics data.
     * @return <mixed>     - Object containing the order details. 
     */
    function getOrderDetails(data) {
      String.prototype.replaceAll = function(search, replacement) {
        var target = this;
        return target.split(search).join(replacement);
      };
      return {
        id          : parseFloat(data[0].replaceAll("'",'')),
        subtotal    : ( parseFloat(data[2].replaceAll("'",'')) - (parseFloat(data[3].replaceAll("'",'')) + parseFloat(data[4].replaceAll("'",'')) ) ),
        total       : parseFloat(data[2].replaceAll("'",'')),
        tax         : parseFloat(data[3].replaceAll("'",'')),
        shipping    : parseFloat(data[4].replaceAll("'",'')),
        city        : data[5].replaceAll("'",''),
        state       : data[6].replaceAll("'",''),
        country     : data[7].replaceAll("'",'')
      }
    }

    /**
     * Parse Analytics Data for All Order Product Details.
     * @param data <mixed> - The order analytics data.
     * @return <mixed>     - Array containing individual product details.
     */
    function getOrderProducts(data) {
      String.prototype.replaceAll = function(search, replacement) {
        var target = this;
        return target.split(search).join(replacement);
      };
      var counter = -1;        // Keep index of details per product.
      var productsArray = []; // Init empty array to hold all products. 
      var product = {};       // Init empty object to hold single product data. 
      //** Product data starts at index 8 **//
      for (x=8; x<data.length; x++) {
        counter++;
        switch (counter) {
          case 1:
            product.id = parseFloat(data[x].replaceAll("'",''));
            break;
          case 2:
            product.description = data[x].replaceAll("'",'');
            break;
          case 3:
            product.tax = parseFloat(data[x].replaceAll("'",''));
            break;
          case 4:
            product.price = parseFloat(data[x].replaceAll("'",''));
            break;
          case 5:
            product.qty = parseFloat(data[x].replaceAll("'",''));
            counter = -1;                 // reset counter
            productsArray.push(product); // push product to products array
            product = {};
            break;
        }
      }
      return productsArray;
    }

 </script>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何否定无效的订单数据?

购物车确认订单数据库

如何在firestore中构建订单数据?

如何对R中的订单数据进行“分组”?

如何获取更新的 WooCommerce 订单数据?

如何从订单数组访问自定义数据(使用插件添加)?

如何获得第三份报告以结合客户和订单数据

如何将 Shopify 订单数据获取到产品页面

如何将两个查询和订单数据组合在一起

订单数组如何按日期和时间降序?

如何获取每个类别的订单数量?

如何在 React-Native 的屏幕上访问嵌套数组 JSON 数据中的嵌套对象

如何计算订单数量和相同订单ID的退货数量?

ModelBinding到DataTables订单数据

如何更新购物车中的订单数量(增加和减少)?

如何通过WooCommerce中的时间限制来限制每个客户的订单数量

SQL查询:如何获取地址列表和每个地址的订单数量

如何使用pandas / matplotlib显示价格范围内的订单数?

SQL中如何统计第一个小时的订单数量

如何以最大编号显示客户名称 MongoDB中的订单数量?

我如何查看员工选择的特定日期每个客户下的订单数量?

如何自定义Woocommerce产品页面中的订单数量输出

在新订单挂钩中获取订单数据

如何在Windows上访问原始剪贴板数据?

如何在React Native上访问本地数据库?

如何在Windows上访问原始剪贴板数据?

获取特定时期之间的订单数据

成功结帐挂钩后获取订单数据

BigQuery / Shopify订单数据查询