"WooCommerce Subscription Order-Based Profile Post Creator and Redirect"

Home » Snippets » "WooCommerce Subscription Order-Based Profile Post Creator and Redirect"
0

Created with:

Visibility: 

public

Creator: Shanna Middleton

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
// Define the wpturbo_create_new_profile_post function
function wpturbo_create_new_profile_post($user_id, $order) {
    $user_info = get_userdata($user_id);
    $first_name = $user_info->first_name;
    $last_name = $user_info->last_name;
    $email = $user_info->user_email;
    
    // Get the billing details from the order
    $billing_address_1 = $order->get_billing_address_1();
    $billing_city = $order->get_billing_city();
    $billing_state = $order->get_billing_state();
    $billing_postcode = $order->get_billing_postcode();
    $billing_country = $order->get_billing_country();
    
    // Create a new 'profile' post
    $profile_post = array(
        'post_author' => $user_id,
        'post_title'  => $first_name . ' ' . $last_name,
        'post_content' => '', // Add content if needed
        'post_type'   => 'profile',
        'post_status' => 'publish',
    );
    
    // Insert the post into the database
    $profile_post_id = wp_insert_post($profile_post, true);
    
    if (is_wp_error($profile_post_id)) {
        // Return the error
        return $profile_post_id;
    }

    // Update ACF fields for the profile post based on order data, assuming ACF Pro is active and these field keys are correct
    if ($profile_post_id) {
        update_field('field_65bdb8233057e', $first_name, $profile_post_id); // First Name
        update_field('field_65bdb8303057f', $last_name, $profile_post_id); // Last Name
        update_field('field_65b540c09a122', $email, $profile_post_id); // Email

        // Prepare the address data as an array with field keys
        $address_data = array(
            'field_65b546dfae54a' => $billing_address_1, // Street
            'field_65b546e6ae54b' => $billing_city,      // City
            'field_65b546ecae54c' => $billing_state,     // State
            'field_65b546f3ae54d' => $billing_postcode,  // Zip Code
            'field_65b54703ae54e' => $billing_country,   // Country
        );

        // Update the group field with the address data
        update_field('field_65b546b2ae549', $address_data, $profile_post_id);
    }
    
    return $profile_post_id;
}

// Hook into the WooCommerce 'thankyou' page
add_action('woocommerce_thankyou', 'wpturbo_update_role_and_create_profile_post', 10, 1);

function wpturbo_update_role_and_create_profile_post($order_id) {
    if (!$order_id) {
        return;
    }

    // Get the order object
    $order = wc_get_order($order_id);
    
    // Get the user ID from the order
    $user_id = $order->get_customer_id();
    
    // Get the user object
    $user = new WP_User($user_id);

    $new_role = false;

    // Check each product in the order
    $items = $order->get_items();
    foreach ($items as $item) {
        $product_id = $item->get_product_id();
        
        // Check if the product is in the 'Designer' category
        if (has_term('designer', 'product_cat', $product_id)) {
            $new_role = 'designer';
            break;
        }
        // Check if the product is in the 'Partner' category
        elseif (has_term('partner', 'product_cat', $product_id)) {
            $new_role = 'partner';
            break;
        }
        // Check if the product is in the 'Apprentice' category
        elseif (has_term('apprentice', 'product_cat', $product_id)) {
            $new_role = 'apprentice';
            break;
        }
    }

    if ($new_role) {
        // Assign user role based on the product category if not already assigned
        if (!in_array($new_role, $user->roles)) {
            $user->set_role($new_role);
        }

        // Create a new profile post
        $profile_post_id = wpturbo_create_new_profile_post($user_id, $order);
        
        if (is_wp_error($profile_post_id)) {
            // Handle the error accordingly
            error_log('Failed to create a profile post: ' . $profile_post_id->get_error_message());
            return;
        }
        
        // Redirect to the single profile page
        $profile_slug = sanitize_title($user->first_name . ' ' . $user->last_name);
        wp_redirect(home_url("/profile/{$profile_slug}/"));
        exit;
    }
}
				

Description: This code snippet for a WordPress plugin enhances a WooCommerce-powered online store by automatically creating a custom post type 'profile' for users upon completing a purchase. The 'profile' post is generated based on the billing details provided during the checkout process and the user's first and last name. Additionally, the code assigns a new user role to the customer based on the product categories in their order, such as 'designer', 'partner', or 'apprentice'. The roles are determined by checking if the purchased products belong to specific categories.

Once the role is assigned, and the 'profile' post is created, the code updates custom fields using Advanced Custom Fields (ACF) Pro to store the user's billing information within the profile post. If any errors occur during the profile post creation, the code logs the error message for debugging purposes. After successfully creating the profile post, the user is redirected to their newly created profile page, ensuring a personalized post-purchase experience.

This snippet should be placed in the functions.php file of your WordPress theme or in a custom plugin. It hooks into the 'woocommerce_thankyou' action, which triggers after an order is completed, to perform the role assignment and profile post creation. It assumes that you have ACF Pro installed and that you have the appropriate fields and custom post type set up in your WordPress installation.

Register an account to save your snippets or go Pro to get more features.