SliceWP Multi Tier Single Form Woocommerce Checkout and Affiliate Registration
-
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’] ) );
}
}
}
- You must be logged in to reply to this topic.