Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k views
in Technique[技术] by (71.8m points)

javascript - Linkwise Affiliate integration in Woocommerce thankyou page

I have a Woocommerce store and I like to send an order completion data to an affiliate and they ask me to include the following code:

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

In my thank you page - Checkout Page.

I have read that this is related to the WooCommerce Data Layer, I have searched for many hours not found anything to help me.

Thank you in advance.

Adding additional info

I use the Header and Footer plug in of WordPress and put the following code:

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

on every page of the website on header

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a way to integrate it; But you will have to add some settings to the code:

  • The program number (your program affiliation ID)
  • The decimal separator (can be a coma or a point).
  • The currency number code (in ISO_4217 format).

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

The code is divided in 2 parts:

  • The utility function that contain Linkwise Affiliate script (and settings)
  • The hooked function that will run Linkwise Affiliate scripts on order received (2 choices):

The first function (Where you will add your settings):

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

And the hooked function that will run the utility function (with 2 different possibilities):

A) Using woocommerce_thankyou action hook:

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) or Using WordPress wp_footer action hook:

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
}

Code goes in function.php file of your active child theme (or theme).

Tested and works.

You can check in your browser console, you will see no errors and the correct output flags in Order received page.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...