Linkwise Affiliate在Woocommerce thankyou页面中的整合

时间:2018-02-23 09:02:50

标签: javascript php wordpress woocommerce google-tag-manager

我有一个Woocommerce商店,我想向联盟会员发送订单完成数据,他们要求我提供以下代码:

<script>
lw("setCurrency", "numeric currency code, e.g. 978 for Euro");
lw("addItem", {
    id: "ID (as given in the XML) of first product in order"
    ,price: "unit price of first product, without VAT e.g. 13,49"
    ,quantity: "quantity of first product"
    ,payout: "1"
});
lw("addItem", {
    id: "ID (as given in the XML) of second product in order"
    ,price: "unit price of second product, without VAT e.g. 25,16"
    ,quantity: "quantity of second product"
    ,payout: "1"
});
// more items
lw("setCoupon", "0");
lw("thankyou", {
    orderid: "unique order ID"
    ,status: "pending"
});
</script>
<noscript>
<img
src="//go.linkwi.se/delivery/acl.php?program=program_ID&amp;decimal
=,_or_.&amp;itemid[1]=ID_of_first_product&amp;itemprice[1]=unit_pri
ce_of_first_product&amp;itemquantity[1]=quantity_of_first_product&a
mp;itempayout[1]=1&amp;itemid[2]=ID_of_second_product&amp;itemprice
[2]=unit_price_of_second_product&amp;itemquantity[2]=quantity_of_se
cond_product&amp;itempayout[2]=1&amp;coupon_price=0&amp;status=pend
ing&amp;orderid=unique_order_ID" style="width:0px;height:0px;"/>
</noscript>

在我的感谢页面 - 结帐页面。

我已经读过这与WooCommerce数据层有关,我搜索了很多时间都找不到任何帮助我的东西。

提前谢谢。

添加其他信息

我使用WordPress的页眉和页脚插件并输入以下代码:

<!-- LinkWise Script -->
<script async src="//go.linkwi.se/delivery/js/tl.js"></script>
<script>
window.lw=window.lw||function(){(lw.q=lw.q||[]).push(arguments)};lw
.l=+new Date;
lw("setProgram", "12686");
lw("setDecimal", ",");
</script>

<!-- End LinkWise Script -->

在网站的每个页面上

1 个答案:

答案 0 :(得分:1)

这是一种整合它的方法;但是你必须在代码中添加一些设置:

  • 程序编号(您的计划从属关系ID)
  • 小数点分隔符(可以是昏迷或点)。
  • 货币编号代码(采用ISO_4217格式)。
  

您将不再需要从页眉或页脚中删除其他相关代码 ...

代码分为两部分:

  • 包含Linkwise Affiliate脚本(和设置)的实用程序功能
  • 将在收到的订单(2个选项)上运行Linkwise Affiliate脚本的钩子函数:

第一个功能(您将添加设置的位置)

// Utility function that contain Linkwise Affiliate script
function linkwise_affiliate_scripts( $order_id ){

    ## --- YOUR SETTINGS START BELOW --- ##

    $program_id  = '12686'; // <== Your program number
    $decimal_sep = ',';     // Decimal separator
    $currency    = '978';   // For "EUR" => See: https://en.wikipedia.org/wiki/ISO_4217

    ## --- END SETTINGS --- ##

    $order        = wc_get_order( $order_id );
    $order_status = $order->get_status();
    $items_string = array();
    $count        = 0;

    ?>
    <script async src="//go.linkwi.se/delivery/js/tl.js"></script>
    <script>
    window.lw=window.lw||function(){(lw.q=lw.q||[]).push(arguments)};
    lw .l=+new Date;
    lw("setProgram", "<?php echo $program_id; ?>");
    lw("setDecimal", "<?php echo $decimal_sep; ?>");
    </script>
    <script>

        lw("setCurrency", "<?php echo $currency; ?>"); // Set your currency
        <?php
            foreach( $order->get_items() as $item ):
                $count++;
                $item_id        = $item->get_id(); // The item ID

                // Get an instance of the WC_Product object
                $product        = $item->get_product();
                $product_id     = $item->get_product_id(); // Product ID
                $price_excl_vat = wc_get_price_excluding_tax( $product ); // Unit price excluding VAT
                $item_qty       = $item->get_quantity(); // Item quantity
                $payout         = '1'; // (???)

                // The string for the <noscript> at the bottom
                $items_string[] = "itemid[$count]=$item_id&amp;itemprice[$count]=$price_excl_vat&amp;itemquantity[$count]=$item_qty&a
        mp;itempayout[$count]=$payout";

        ?>
        lw("addItem", {
            id: "<?php echo $item_id; // Or can be the product ID (may be) ?>"
            ,price: "<?php echo $price_excl_vat; ?>"
            ,quantity: "<?php echo $item_qty; ?>"
            ,payout: "<?php echo $payout; ?>"
        });
        <?php
            endforeach;

            // Set the array of items strings in a unique string
            $items_string = implode( '&amp;', $items_string );
        ?>
        // Other items types
        <?php
            $coupon_discounts = $coupon_discounts_tax = 0;
            foreach( $order->get_items('coupon') as $item_coupon ){
                $coupon_discounts     += $item_coupon->get_discount();
                $coupon_discounts_tax += $item_coupon->get_discount_tax();
            }
        ?>
        lw("setCoupon", "<?php echo $coupon_discounts; ?>");
        lw("thankyou", {
            orderid: "<?php echo $order_id; ?>"
            ,status: "<?php echo $order_status; ?>"
        });
    </script>
    <noscript>
        <img
        src="//go.linkwi.se/delivery/acl.php?program=<?php echo $program_id; ?>&amp;decimal=<?php echo $decimal_sep; ?>&amp;<?php echo $items_string; ?>&amp;coupon_price=<?php echo $coupon_discounts; ?>&amp;status=<?php echo $order_status; ?>&amp;orderid=<?php echo $order_id; ?>" style="width:0px;height:0px;"/>
    </noscript>
    <?php echo 'test';
}

将运行效用函数的钩子函数(有两种不同的可能性)

A)使用woocommerce_thankyou动作挂钩:

add_action( 'woocommerce_thankyou','wc_linkwise_affiliate_thanyou_integration', 20, 1 );
function wc_linkwise_affiliate_thanyou_integration( $order_id ){
    if ( empty($order_id) || $order_id == 0 )
        return; // Exit

    linkwise_affiliate_scripts( $order_id ); // Run the Linkwise Affiliate
}

B)或使用WordPress wp_footer动作挂钩:

add_filter( 'wp_footer', 'wc_linkwise_affiliate_order_received_integration' );
function wc_linkwise_affiliate_order_received_integration() {
    if ( ! is_wc_endpoint_url( 'order-received' ) )
        return; // Exit

    global $wp;

    $order_id  = absint( $wp->query_vars['order-received'] );
    if ( empty($order_id) || $order_id == 0 )
        return; // Exit

    linkwise_affiliate_scripts( $order_id ); // Run the Linkwise Affiliate
}

代码放在活动子主题(或主题)的function.php文件中。

经过测试和工作。

您可以登录浏览器控制台,在订单收到页面中看到没有错误和正确的输出标记。