php - 在查看订阅页面 woocommerce 中显示与订阅相关的许可证

标签 php wordpress woocommerce hook-woocommerce woocommerce-subscriptions

我使用 woocommerce 订阅和软件许可证管理器主插件,我想在查看订阅页面上显示软件许可证管理器主插件生成的许可证,现在软件许可证管理器主插件创建的许可证显示在 View -订单页面,该功能是在软件许可证管理器主插件中编写的,用于显示在查看订单页面:

/**
 * 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);

我在查看订阅页面找到了一个位置:

do_action( 'woocommerce_subscription_totals_table', $subscription );

我将 woocommerce_order_details_after_order_table 更改为此 woocommerce_subscription_totals_table,但查看订阅页面上没有显示任何内容

我的工作有什么问题吗?您的任何回答都将带来我的谢意

最佳答案

/**
 * 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);

关于php - 在查看订阅页面 woocommerce 中显示与订阅相关的许可证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73414723/

相关文章:

php - 每周从多个表中获取唯一数据

javascript - 可定制的 jquery 警报框的问题

wordpress - 使用 Bootstrap 的类别部分

php - 结帐页面中的 Woocommerce 付款方式检测

javascript - JS - 需要使用通配符

php - WooCommerce 产品 get_stock_quantity() 方法返回 null

php - ZF2 查看助手配置

javascript - 模态隐藏在 FF 但不是 Chrome,IE11

php - 用 __() 和 sprintf() 翻译 WP

php - 如何在 WordPress 中为其他子网站共享 WP_POST