Woocommerce 感谢页面中的 Linkwise Affiliate 集成

Linkwise Affiliate integration in Woocommerce thankyou page

我有一个 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的Header和Footer插件,放了下面的代码:

<!-- 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 -->

网站每一页的页眉

这是一种集成它的方法;但是您必须在代码中添加一些设置:

  • 程序编号(您的程序附属 ID)
  • 小数点分隔符(可以是逗号或点)。
  • 货币代码(ISO_4217格式)。

You will have to remove other related code from header or footer as not needed anymore…

代码分为两部分:

  • 包含 Linkwise Affiliate 脚本(和设置)的实用函数
  • 运行 Linkwise Affiliate scripts on order received (2 choices) 的钩子函数:

第一个函数(您将在其中添加设置):

// 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';
}

以及将 运行 效用函数 的挂钩函数 (有 2 种不同的可能性):

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_action( '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 文件。

经过测试并有效。

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