Search code examples
phpwordpresswoocommerceaffiliate

Add Product to Order Queue when User Click on External/Affiliate Product Button


I'm working on Affiliate Website configuration of Woocommerce. While I've got most of it configured and set. I want to add the External Product as Order in the Woocommerce Order Queue (with Pending state) which will allow me to track.

So basically, approach I was thinking was to embedded the a query along the Buy Product Button Link which redirects to external affiliate website, while it does that at the same time it should at a the item as an Order in my Woocommerce list. I believe a simple query/function call in the call itself should work but this is me speaking in Techno-Functional terms. Slightly new to Wordpress.

Help in guiding towards the same would be much appreciated. This will then inturn allow me to send email to users on their incomplete or pending orders.


Solution

  • Okay, so since I received no response and before Stackoverflow terms this as an invalid Question.

    I've developed my own plugin which basically checks when an external product button is clicked and then perform the order entry based on the Product_ID and Current User ID with the order status as Pending.

    My JQuery Script is as follows

    jQuery(document).ready(function($) {
    $(document).off( 'click', '#affiliate' );
    $(document).on( 'click', '#affiliate', function() {
    //Code Here
    var prodid = $("#prodid").text();
    alert( prodid );
    var odata = {
        'action': 'pending_order',
        security: wp_ajax_eaco.ajaxnonce,
        prod_id: prodid
    };
    
    $.post( 
        wp_ajax_eaco.ajaxurl, 
        odata, function(response) {
        alert('Got this from the server: ' + response);
                });
     });
    });
    

    And my corresponding Call to Function is as follows

    //Order Call
    add_action( 'wp_ajax_pending_order', array( &$this, 'create_order'));
    function create_order()
            {
                global $woocommerce, $wp;
                // Security check
                check_ajax_referer( 'ajax_post_validation', 'security' );
                //Getting Product ID
                $id = $_POST['prod_id'];
    
    
                // create order
                $address = array(
                        'first_name' => 'Iskandariya',
                        'last_name'  => 'Solutions',
                        'company'    => 'Iskandariya.Solutions',
                        'phone'      => '+91-9999999999',
                        'address_1'  => 'Chandigarh',
                        'address_2'  => 'Mohali,Punjab', 
                        'city'       => 'Chandigarh',
                        'state'      => 'PB',
                        'postcode'   => '160001',
                        'country'    => 'IN'
                    );
    
                $order_data = array(
                        'customer_id'       => get_current_user_id()
                        );
    
                $order = wc_create_order($order_data);
                $order->add_product( get_product( $id ), 1 ); //(get_product with id and next is for quantity)
                $order->set_address( $address, 'billing' );
                $order->set_address( $address, 'shipping' );
                $order->calculate_totals();
                wp_die();
            }
    

    In a way thank you for not responding it took me longer but I learnt on developing Plugin for Wordpress and alot more.