【问题标题】:Linkwise Affiliate integration in Woocommerce thankyou pageWoocommerce 感谢页面中的 Linkwise Affiliate 集成
【发布时间】:2018-02-23 09:02:50
【问题描述】:

我有一家 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 and 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 -->

在网站的每个页面上的标题

【问题讨论】:

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


    【解决方案1】:

    这是一种整合方式;但是您必须在代码中添加一些设置:

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

    如果不再需要,您必须从页眉或页脚中删除其他相关代码...

    代码分为两部分:

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

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

    // 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_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 文件中。

    经过测试并且有效。

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

    【讨论】:

    • 非常感谢您的回答我很抱歉没有包含以下代码: 他们确实提供了每个页面的标题,因为它很容易实现。也许这是缺少的 lw() 之一。我的下一步是什么?我需要包含主脚本吗?由于我没有经验,如何以及在哪里。
    • 我只是想丢失的 JavaScript (js) 文件必须在这个源 src="//go.linkwi.se/delivery/js/tl.js 中,它会加载到标头。我刚刚在我的子主题 php 文件的末尾添加了建议的代码,并且感谢页面不再加载。
    • 伟大的解决方案我的朋友。感谢您宝贵的时间。
    • @TasosKaratzoglou 仅供参考:我已经更新了 2 个替代方案(2 个不同的工作挂钩)......您可以保持您的版本正常工作......
    猜你喜欢
    • 2018-11-05
    • 2023-03-20
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 2015-01-17
    • 2021-06-09
    • 2017-01-14
    • 2018-04-15
    相关资源
    最近更新 更多