我使用WooCommerce订阅和软件许可管理器主插件,我想在查看-订阅页面上显示软件许可管理器主插件生成的许可,现在在查看-订购页面上显示软件许可管理器主插件创建的许可,这个函数写在软件许可管理器主插件中,在查看-订购页面上显示:

/**
 * 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相关问答推荐

模型中的Laravel自定义查询字段

根据未用于变体的产品属性隐藏WooCommerce发货方式

将部分产品排除在WooCommerce的计算附加费之外

使用php创建表格和插入数据

在PHP 8.2 Gloogle云引擎上加载大型php文件速度缓慢

如何在PHP中组合日期范围

SFTP在PHP(phpseclib3\Net\SFTP)-过滤目录列表结果服务器端可能吗?类似于psftp中的LS命令?

PHP SimpleXML:按属性值访问 node

Select 本地提货时隐藏WooCommerce显示的发货成本

Laravel 10.x在到达控制器之前将查询参数强制转换为正确的类型

根据WooCommerce的定制订单状态增加产品库存

PHP 支持 APNG 文件吗?

在 Heroku 上部署 Sylius - 路由不正确

如何将文件上传添加到 WooCommerce 结帐?

Woocommerce单个添加到购物车页面中的产品动态定价

我怎样才能在 .htaccess 中有 2 个参数?

转发和非转发呼叫 - 后期静态绑定

无法在 Laravel 10 中安装 jenssegers/mongodb

列表不在 GROUP BY 子句中并且包含 X2CRM 中的非聚合列

hasOne/belongsToMany 或多对多