Linkwise Affiliate integration in Woocommerce thankyou page

后端 未结 1 874
眼角桃花
眼角桃花 2020-12-21 23:51

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:



        
相关标签:
1条回答
  • 2020-12-22 00:45

    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.

    0 讨论(0)
提交回复
热议问题