LearnDash customer access list from WooCommerce orders

LearnDash by default is not backward compatible with completed orders from WooCommerce and associating them with products. If you changed your content delivery of your learning platform and ran into this issue, I found a rough solution.

First, build a simple connection to your database

<?php
$servername = "localhost";
$username = "";
$password = "";
$database = "";
 
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
 
// Check connection
if ($conn-&gt;connect_error) {
    die("Connection failed: " . $conn-&gt;connect_error);
}

Next, we will want to find the orders IDs of all the orders with a status of “completed” from WooCommerce. WooCommerce changes the post_status of the shop_order post in wp_posts to “wc-completed”.

//completed orders
$completed_orders = $conn-&gt;query("SELECT ID from wp_posts WHERE post_type = 'shop_order' AND post_status = 'wc-completed'");
while($row = $completed_orders-&gt;fetch_array()) {
    print $row["ID"].',';
}

This will return a CSV (comma separated value) which we will use to build a CSV of customers associated with a product title of a completed WooCommerce order. We can use this CSV to insert into LearnDash access list for each product from the WordPress admin panel.

Let us assume the output is: 2022,2036,2040

$product = $conn-&gt;query("SELECT wp_woocommerce_order_items.order_item_name, wp_woocommerce_order_items.order_id, wp_postmeta.meta_key, wp_postmeta.meta_value FROM wp_woocommerce_order_items INNER JOIN wp_postmeta ON wp_woocommerce_order_items.order_id = wp_postmeta.post_id WHERE order_id IN (2022,2036,2040) AND order_item_name = 'PRODUCT TITLE HERE'");
while($row = $product-&gt;fetch_array()) {
 
    if($row["meta_key"] === '_customer_user')
        print $row["meta_value"].',';
}

We are going to INNER JOIN two tables (wp_woocommerce_order_item & wp_postmeta) of data by the order_id (in wp_postmeta case it will be post_id). This will allow us to use a decision control by “_customer_user” wp_postmeta and return the user IDs associated with the order_item_name (which will be the title of the product) in a CSV.

You can paste the CSV in the course access list from WordPress admin for each.

Leave a reply:

Your email address will not be published.

Site Footer