• Resolved graceven

    (@graceven)


    Hello Guys,
    I have an ecommerce affiliate multi-tiered system. I need to join the information of checkout form and affiliate registration form in a single form. I have this code and the information of the username, password and referring member id did not added on the affiliate table of SliceWP. This is the code I have.

    <?php
    /**

    • Plugin Name: WooCommerce Affiliate Registration
    • Description: Automatically register customers as affiliates after checkout using SliceWP.
    • Version: 1.0
    • Author: Your Name
      */

    // Register affiliate after WooCommerce checkout
    function register_affiliate_after_checkout( $order_id ) {
    // Get order details
    $order = wc_get_order( $order_id );
    $user_id = $order->get_user_id();

    // Get custom fields
    $username = $order->get_meta( '_checkout_billing_username' );
    $password = $order->get_meta( '_checkout_billing_password' );
    $referring_member_id = $order->get_meta( '_checkout_referring_member_id' );
    
    // Register user as affiliate if not already registered
    if ( $user_id && ! slicewp_is_affiliate( $user_id ) ) {
        $args = array(
            'user_id'          => $user_id,
            'status'           => 'inactive', 
            'first_name'       => $order->get_billing_first_name(),
            'last_name'        => $order->get_billing_last_name(),
            'email'            => $order->get_billing_email(),
            'affiliate_id'     => $user_id, 
            'referring_member' => $referring_member_id
        );
    
        $affiliate_id = slicewp_register_affiliate( $args );
        if ( $affiliate_id ) {
            error_log( "Affiliate registered: User ID $user_id, Affiliate ID $affiliate_id" );
        } else {
            error_log( "Failed to register affiliate for User ID $user_id" );
        }
    }

    }
    add_action( ‘woocommerce_checkout_order_processed’, ‘register_affiliate_after_checkout’ );

    // Redirect to affiliate dashboard after successful purchase
    function redirect_to_affiliate_dashboard( $order_id ) {
    $order = wc_get_order( $order_id );
    $user_id = $order->get_user_id();

    if ( $user_id && slicewp_is_affiliate( $user_id ) ) {
        wp_redirect( get_permalink( slicewp_get_affiliate_dashboard_page_id() ) );
        exit;
    }

    }
    add_action( ‘woocommerce_thankyou’, ‘redirect_to_affiliate_dashboard’ );

    // Add custom fields to the checkout form
    add_action( ‘woocommerce_after_checkout_billing_form’, ‘add_custom_affiliate_fields_to_checkout’ );
    function add_custom_affiliate_fields_to_checkout( $checkout ) {
    if ( ! is_user_logged_in() ) {
    echo ‘

    ‘; woocommerce_form_field( ‘billing_username’, array( ‘type’ => ‘text’, ‘class’ => array( ‘form-row-wide’ ), ‘label’ => ( ‘Username’ ), ‘required’ => true, ), $checkout->get_value( ‘billing_username’ )); woocommerce_form_field( ‘billing_password’, array( ‘type’ => ‘password’, ‘class’ => array( ‘form-row-wide’ ), ‘label’ => ( ‘Password’ ), ‘required’ => true, ), $checkout->get_value( ‘billing_password’ )); woocommerce_form_field( ‘referring_member_id’, array( ‘type’ => ‘text’, ‘class’ => array( ‘form-row-wide’ ), ‘label’ => __( ‘Referring Member ID (if applicable)’ ), ‘required’ => false, ), $checkout->get_value( ‘referring_member_id’ )); echo ”;
    }
    }

    // Save custom fields during checkout process
    add_action( ‘woocommerce_checkout_create_order’, ‘save_custom_affiliate_fields’, 20, 2 );
    function save_custom_affiliate_fields( $order, $data ) {
    if ( ! is_user_logged_in() ) {
    if ( isset( $_POST[‘billing_username’] ) && ! empty( $_POST[‘billing_username’] ) ) {
    $order->update_meta_data( ‘_checkout_billing_username’, sanitize_text_field( $_POST[‘billing_username’] ) );
    }
    if ( isset( $_POST[‘billing_password’] ) && ! empty( $_POST[‘billing_password’] ) ) {
    $order->update_meta_data( ‘_checkout_billing_password’, sanitize_text_field( $_POST[‘billing_password’] ) );
    }
    if ( isset( $_POST[‘referring_member_id’] ) && ! empty( $_POST[‘referring_member_id’] ) ) {
    $order->update_meta_data( ‘_checkout_referring_member_id’, sanitize_text_field( $_POST[‘referring_member_id’] ) );
    }
    }
    }

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author iova.mihai

    (@iovamihai)

    Hey @graceven,

    Thank you for reaching out! From what I see, your code is using functions that SliceWP does not have, namely “slicewp_is_affiliate”, “slicewp_get_affiliate_dashboard_page_id” and “slicewp_register_affiliate”.

    Are these functions created by you or did you find them somewhere online? If you’ve found them online, please let me know as they are not correct and I’d like to make sure they are addressed.

    Also, for what you’re attempting to do, you may want to use the “Auto Register Affiliates” option from SliceWP > Settings > General > General Settings. With this option all new user accounts will be registered as affiliates, somewhat similar to what you’ve coded.

    Thank you and best wishes,

    Mihai

    Thread Starter graceven

    (@graceven)

    Hi Mihai,

    Thank you for your feedback and insights.

    Regarding the functions you mentioned (slicewp_is_affiliate, slicewp_get_affiliate_dashboard_page_id, and slicewp_register_affiliate), those were custom functions I created. I will review and adjust them to better align with SliceWP’s structure.

    To clarify the process I’m working on:

    1. During WooCommerce checkout, the following information is collected:
      • Username
      • Password
      • Referring member ID
    2. This information is then added to the SliceWP affiliate records or table.
    3. Upon checkout completion, the user is registered as an inactive affiliate.
    4. Once the user makes a successful referral, their status automatically changes to active.

    I’m not using the “Auto Register Affiliates” option in SliceWP > Settings > General > General Settings because the client requires a single form for both checkout and affiliate registration, rather than having separate processes.

    I have a custom plugin in place but need help integrating these additional fields (username, password, referring member ID) into the affiliate registration process within SliceWP. Could you advise on the best approach for this?

    Thank you again for your support, and I look forward to your advice!

    Best regards,
    Grace

    Plugin Author iova.mihai

    (@iovamihai)

    Hey @graceven,

    Thank you for providing this additional information! Can you please share the entire code, alongside the custom functions? Without knowing how the custom functions work, I can’t really offer a complete explanation.

    Also, can you please add the entire code into a code block so that’s easier to read? If you don’t know how to add a code block, click the blue plus sign from the editor’s toolbar and select the “Code” block.

    Alternatively, if you prefer GitHub, you can add a new gist (here: https://gist.github.com/) and share the link with me.

    Thank you and best wishes,

    Mihai

    Thread Starter graceven

    (@graceven)

    Hi Mihai,

    Thank you for your help.

    This is the code, and I included the multilevel tier commission.

    <?php
    /**
    * Plugin Name: WooCommerce Multi-Level Affiliate Registration and Commission
    * Description: Automatically register customers as affiliates after checkout and handle multi-level commissions using SliceWP.
    * Version: 1.6
    * Author: MGLV
    */

    // Register affiliate after WooCommerce checkout
    function register_affiliate_after_checkout( $order_id ) {
    // Get order details
    $order = wc_get_order( $order_id );
    $user_id = $order->get_user_id();

    // Get custom fields
    $username = $order->get_meta( '_checkout_billing_username' );
    $password = $order->get_meta( '_checkout_billing_password' );
    $referring_member_id = $order->get_meta( '_checkout_referring_member_id' );

    // Additional WooCommerce checkout fields
    $first_name = $order->get_billing_first_name();
    $last_name = $order->get_billing_last_name();
    $email = $order->get_billing_email();
    $phone = $order->get_billing_phone();
    $address = $order->get_billing_address_1();
    $city = $order->get_billing_city();
    $state = $order->get_billing_state();
    $postcode = $order->get_billing_postcode();

    // Determine affiliate status based on referring member ID
    $status = empty( $referring_member_id ) ? 'inactive' : 'active';

    // Register user as affiliate if not already registered
    if ( $user_id && ! slicewp_is_affiliate( $user_id ) ) {
    $args = array(
    'user_id' => $user_id,
    'status' => $status, // Inactive unless there's a referral
    'first_name' => $first_name,
    'last_name' => $last_name,
    'email' => $email,
    'username' => $username, // Mapping WooCommerce billing_username
    'password' => $password, // Mapping WooCommerce billing_password
    'billing_address_1' => $address,
    'billing_city' => $city,
    'billing_state' => $state,
    'billing_postcode' => $postcode,
    'billing_phone' => $phone,
    'referring_member' => $referring_member_id,
    'personal_referrals' => 0, // Field to count personal referred affiliates
    'active_referrals' => 0 // Field to count 1st tier active affiliates
    );

    $affiliate_id = slicewp_register_affiliate( $args );
    if ( $affiliate_id ) {
    error_log( "Affiliate registered: User ID $user_id, Affiliate ID $affiliate_id" );
    } else {
    error_log( "Failed to register affiliate for User ID $user_id" );
    }
    }
    }
    add_action( 'woocommerce_checkout_order_processed', 'register_affiliate_after_checkout' );

    // Multi-level Commission Logic with 6 Tiers
    function apply_multilevel_commission( $affiliate_id, $order_id, $commission_amount ) {
    // Define tiered payout amounts (adjust values as needed)
    $level_1_payout = 5; // $5 for direct referral
    $level_2_payout = 3; // $3 for second-level referral
    $level_3_payout = 2; // $2 for third-level referral
    $level_4_payout = 1; // $1 for fourth-level referral
    $level_5_payout = 0.5; // $0.50 for fifth-level referral
    $level_6_payout = 0.25;// $0.25 for sixth-level referral
    $bonus_per_sale = 1; // Additional $1 bonus for third-level referrals

    // Get the referring affiliate for level 1
    $referring_affiliate_id = slicewp_get_affiliate_meta( $affiliate_id, 'referring_member', true );

    if ( $referring_affiliate_id ) {
    // Level 1 commission (direct referral)
    slicewp_create_commission( array(
    'affiliate_id' => $referring_affiliate_id,
    'order_id' => $order_id,
    'amount' => $level_1_payout
    ));

    // Get the referring affiliate's referrer (level 2)
    $referring_affiliate_level_2 = slicewp_get_affiliate_meta( $referring_affiliate_id, 'referring_member', true );
    if ( $referring_affiliate_level_2 ) {
    // Level 2 commission
    slicewp_create_commission( array(
    'affiliate_id' => $referring_affiliate_level_2,
    'order_id' => $order_id,
    'amount' => $level_2_payout
    ));

    // Get the level 2 referrer (level 3)
    $referring_affiliate_level_3 = slicewp_get_affiliate_meta( $referring_affiliate_level_2, 'referring_member', true );
    if ( $referring_affiliate_level_3 ) {
    // Level 3 commission with bonus
    slicewp_create_commission( array(
    'affiliate_id' => $referring_affiliate_level_3,
    'order_id' => $order_id,
    'amount' => $level_3_payout + $bonus_per_sale // Add bonus here
    ));

    // Get the level 3 referrer (level 4)
    $referring_affiliate_level_4 = slicewp_get_affiliate_meta( $referring_affiliate_level_3, 'referring_member', true );
    if ( $referring_affiliate_level_4 ) {
    // Level 4 commission
    slicewp_create_commission( array(
    'affiliate_id' => $referring_affiliate_level_4,
    'order_id' => $order_id,
    'amount' => $level_4_payout
    ));

    // Get the level 4 referrer (level 5)
    $referring_affiliate_level_5 = slicewp_get_affiliate_meta( $referring_affiliate_level_4, 'referring_member', true );
    if ( $referring_affiliate_level_5 ) {
    // Level 5 commission
    slicewp_create_commission( array(
    'affiliate_id' => $referring_affiliate_level_5,
    'order_id' => $order_id,
    'amount' => $level_5_payout
    ));

    // Get the level 5 referrer (level 6)
    $referring_affiliate_level_6 = slicewp_get_affiliate_meta( $referring_affiliate_level_5, 'referring_member', true );
    if ( $referring_affiliate_level_6 ) {
    // Level 6 commission
    slicewp_create_commission( array(
    'affiliate_id' => $referring_affiliate_level_6,
    'order_id' => $order_id,
    'amount' => $level_6_payout
    ));
    }
    }
    }
    }
    }
    }
    }
    add_action( 'slicewp_commission_created', 'apply_multilevel_commission', 10, 3 );

    // Redirect to affiliate dashboard after successful purchase
    function redirect_to_affiliate_dashboard( $order_id ) {
    $order = wc_get_order( $order_id );
    $user_id = $order->get_user_id();

    if ( $user_id && slicewp_is_affiliate( $user_id ) ) {
    wp_redirect( get_permalink( slicewp_get_affiliate_dashboard_page_id() ) );
    exit;
    }
    }
    add_action( 'woocommerce_thankyou', 'redirect_to_affiliate_dashboard' );

    // Add custom fields to the WooCommerce checkout form
    add_action( 'woocommerce_after_checkout_billing_form', 'add_custom_affiliate_fields_to_checkout' );
    function add_custom_affiliate_fields_to_checkout( $checkout ) {
    if ( ! is_user_logged_in() ) {
    echo '<div id="custom_affiliate_checkout_fields">';

    // Username Field
    woocommerce_form_field( 'billing_username', array(
    'type' => 'text',
    'class' => array( 'form-row-wide' ),
    'label' => __( 'Username' ),
    'required' => true,
    ), $checkout->get_value( 'billing_username' ));

    // Password Field
    woocommerce_form_field( 'billing_password', array(
    'type' => 'password',
    'class' => array( 'form-row-wide' ),
    'label' => __( 'Password' ),
    'required' => true,
    ), $checkout->get_value( 'billing_password' ));

    // Referring Member Field
    woocommerce_form_field( 'referring_member_id', array(
    'type' => 'text',
    'class' => array( 'form-row-wide' ),
    'label' => __( 'Referring Affiliate ID (optional)' ),
    'required' => false,
    ), $checkout->get_value( 'referring_member_id' ));

    echo '</div>';
    }
    }

    // Save custom fields on WooCommerce checkout
    add_action( 'woocommerce_checkout_update_order_meta', 'save_custom_affiliate_checkout_fields' );
    function save_custom_affiliate_checkout_fields( $order_id ) {
    if ( ! empty( $_POST['billing_username'] ) ) {
    update_post_meta( $order_id, '_checkout_billing_username', sanitize_text_field( $_POST['billing_username'] ) );
    }
    if ( ! empty( $_POST['billing_password'] ) ) {
    update_post_meta( $order_id, '_checkout_billing_password', sanitize_text_field( $_POST['billing_password'] ) );
    }
    if ( ! empty( $_POST['referring_member_id'] ) ) {
    update_post_meta( $order_id, '_checkout_referring_member_id', sanitize_text_field( $_POST['referring_member_id'] ) );
    }
    }
    ?>
    Plugin Author iova.mihai

    (@iovamihai)

    Hey @graceven,

    From what I see, you have not shared all of the custom functions you’ve written. For example, slicewp_is_affiliate,?slicewp_get_affiliate_dashboard_page_id, and?slicewp_register_affiliate are missing.

    Without knowing the code of these functions, I cannot say what is misfiring.

    Thank you and best wishes,

    Mihai

    Thread Starter graceven

    (@graceven)

    Hi Sir,

    What is the function used when I will add an affiliate? We need to automatically add the information from woocommerce checkout page to the slicewp affiliate and the other tables as well.
    Thank you.

    Plugin Author iova.mihai

    (@iovamihai)

    Hey @graceven,

    The function you’re looking for is slicewp_insert_affiliate. I recommend you to search our code base to see exactly how this works.

    Thank you and best wishes,

    Mihai

    Thread Starter graceven

    (@graceven)

    Hi Sir,
    Good day.
    What is this error after checkout page? We already changed the checkout and added fields for to insert record to Slicewp affiliate. https://drive.google.com/file/d/13rAWj-7tRvbwMHp9eSSWVpWWvPBqKB2A/view?usp=sharing
    Thank you.

    • This reply was modified 2 weeks, 3 days ago by graceven.
    Plugin Author iova.mihai

    (@iovamihai)

    Hey @graceven,

    The error you’re seeing is a general WordPress error. Please check your PHP error log file to see the actual error and the error stack trace.

    Also, please note that we do not offer consulting on custom coded solutions. If you are having trouble implementing a custom coded solution, please contract a WordPress developer to assist you.

    Thank you and best wishes,

    Mihai

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.