graceven
Forum Replies Created
-
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.
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.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'] ) );
}
}
?>Hi Mihai,
Thank you for your feedback and insights.
Regarding the functions you mentioned (
slicewp_is_affiliate
,slicewp_get_affiliate_dashboard_page_id
, andslicewp_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:
- During WooCommerce checkout, the following information is collected:
- Username
- Password
- Referring member ID
- This information is then added to the SliceWP affiliate records or table.
- Upon checkout completion, the user is registered as an inactive affiliate.
- 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