Search code examples
phpwordpresswoocommercehook-woocommercewoocommerce-subscriptions

Display the license related to the subscription in view-subscription page woocommerce


I use woocommerce subscriptions and software license manager master plugin, i want to display the licenses generated by the software license manager master plugin on the view-subscription page, now the licenses created by software license manager master plugin are displayed on view-order page, this function is written in the software license manager master plugin to be displayed on view-order page :

/**
 * Display values on the order details page
 */

function slm_order_details($order) {
    global $wpdb;
    $items = $order->get_items();
    $licences = array();
    foreach ($items as $item_key => $item_details) {
        $product = $item_details->get_product();
        if ( $product->is_type('subscription') && wcs_order_contains_subscription( $order, 'parent' ) && $order->has_status('completed') ) { 
            if ($lic_keys = wc_get_order_item_meta($item_details->get_id(), '_slm_lic_key', false)) {
                $lic_types = wc_get_order_item_meta($item_details->get_id(), '_slm_lic_type', false);
                $licences = array_map(function ($keys, $types) {
                    return array(
                        'lic_key' => $keys,
                        'lic_type' => $types
                    );
                }, $lic_keys, $lic_types);
            }
        }
    }
    if ($licences) {
        echo '
            <h2 class="woocommerce-order-details__title">' . __('License details', 'softwarelicensemanager') . '</h2>
            <table class="woocommerce-table woocommerce-table--order-details shop_table order_details">
                <thead>
                    <tr>
                        <th class="woocommerce-table__product-name product-name">' . __('License key', 'softwarelicensemanager') . '</th>
                        <th class="woocommerce-table__product-table product-total">' . __('Type', 'softwarelicensemanager') . '</th>
                    </tr>
                </thead>
                <tbody>
        ';
        foreach ($licences as $lic_row) {
            echo '
                    <tr class="woocommerce-table__line-item order_item">
                        <td class="woocommerce-table__product-name product-name">
                            ' . $lic_row['lic_key'] . ' - <a href="' . get_permalink(wc_get_page_id('myaccount')) . '/my-licenses"> ' . __('view my licenses', 'softwarelicensemanager') . '</a>
                        </td>
                        <td class="woocommerce-table__product-total product-total">
                            ' . $lic_row['lic_type'] . '
                        </td>
                    </tr>
            ';
        }
        echo '
                </tbody>
            </table>
        ';
    }
} add_action('woocommerce_order_details_after_order_table', 'slm_order_details', 10, 1);

i found a position on view-subscription page :

do_action( 'woocommerce_subscription_totals_table', $subscription );

i change woocommerce_order_details_after_order_table to this woocommerce_subscription_totals_table, but nothing is displayed on view-subscription page

what is wrong with my work? any answer from you will certainly bring my thanks


Solution

  • /**
     * Display values on the subscription details page
     */
    
    function subscription_order_details($subscription) {
        global $wpdb;
        $order = $subscription->get_parent();
        $items = $order->get_items();
        $licences = array();
        foreach ($items as $item_key => $item_details) {
            $product = $item_details->get_product();
            if ( $product->is_type('subscription') && wcs_order_contains_subscription( $order, 'parent' ) && $order->has_status('completed') ) { 
                if ($lic_keys = wc_get_order_item_meta($item_details->get_id(), '_slm_lic_key', false)) {
                    $lic_types = wc_get_order_item_meta($item_details->get_id(), '_slm_lic_type', false);
                    $licences = array_map(function ($keys, $types) {
                        return array(
                            'lic_key' => $keys,
                            'lic_type' => $types
                        );
                    }, $lic_keys, $lic_types);
                }
            }
        }
        if ($licences) {
            echo '
                <h2 class="woocommerce-order-details__title">' . __('License details', 'softwarelicensemanager') . '</h2>
                <table class="woocommerce-table woocommerce-table--order-details shop_table order_details">
                    <thead>
                        <tr>
                            <th class="woocommerce-table__product-name product-name">' . __('License key', 'softwarelicensemanager') . '</th>
                            <th class="woocommerce-table__product-table product-total">' . __('Type', 'softwarelicensemanager') . '</th>
                        </tr>
                    </thead>
                    <tbody>
            ';
            foreach ($licences as $lic_row) {
                echo '
                        <tr class="woocommerce-table__line-item order_item">
                            <td class="woocommerce-table__product-name product-name">
                                ' . $lic_row['lic_key'] . ' - <a href="' . get_permalink(wc_get_page_id('myaccount')) . '/my-licenses"> ' . __('view my licenses', 'softwarelicensemanager') . '</a>
                            </td>
                            <td class="woocommerce-table__product-total product-total">
                                ' . $lic_row['lic_type'] . '
                            </td>
                        </tr>
                ';
            }
            echo '
                    </tbody>
                </table>
            ';
        }
    } 
    
    add_action('woocommerce_subscription_totals_table', 'subscription_order_details', 10, 1);