我正在使用WooCommerce订阅插件来管理重复订单。
但是,我希望我的客户在他们的订阅详细信息页面中看到自定义数据,这些数据是我添加到所有新订阅中的。
我为客户的婴儿名添加了一个变量,我将其作为_baby_name添加到订阅数据post_meta数据中,如下所示:
/**
* Triggered after a new subscription is created.
*
* @since 2.2.22
* @param WC_Subscription $subscription
*/
function action_wcs_create_subscription( $subscription ) {
// Get ID
$subscription_id = $subscription->get_id();
update_post_meta( $subscription_id, '_baby_name', get_current_user_id() );
}
add_action( 'wcs_create_subscription', 'action_wcs_create_subscription', 10, 1 );出于测试目的,我只是将值设置为get_current_user_id()。
为了在客户端显示这些自定义数据,我尝试修改订阅-细节. the文件:
wp-content/plugins/woocommerce-subscriptions/vendor/woocommerce/subscriptions-core/templates/myaccount/subscription-details.php
我在subscription_details表的顶部添加了一行,在状态栏上方,如下所示:
<tbody>
<tr>
<td><?php esc_html_e( 'Baby Name', 'woocommerce-subscriptions' ); ?></td>
<td><?php echo esc_html( $subscription.'baby_name' ); ?></td>
</tr>
<tr>
<td><?php esc_html_e( 'Status', 'woocommerce-subscriptions' ); ?></td>
<td><?php echo esc_html( wcs_get_subscription_status_name( $subscription->get_status() ) ); ?></td>
</tr>但是在我的新行中,我只得到了与$subscriptions相关的所有数据:

我应该用什么代替$subscription.'_baby_name'来提取_baby_name的值,并在表中显示它呢?
发布于 2022-07-08 07:06:38
由于数据被保存为后置元,所以可以使用$subscription->get_meta( '_baby_name', true );。
所以你得到了:
<tbody>
<tr>
<td><?php esc_html_e( 'Baby Name', 'woocommerce-subscriptions' ); ?></td>
<td><?php echo $subscription->get_meta( '_baby_name', true ); ?></td>
</tr>
<tr>
<td><?php esc_html_e( 'Status', 'woocommerce-subscriptions' ); ?></td>
<td><?php echo esc_html( wcs_get_subscription_status_name( $subscription->get_status() ) ); ?></td>
</tr>或者使用get_post_meta( $subscription->get_id(), '_baby_name', true );
<tbody>
<tr>
<td><?php esc_html_e( 'Baby Name', 'woocommerce-subscriptions' ); ?></td>
<td><?php echo get_post_meta( $subscription->get_id(), '_baby_name', true ); ?></td>
</tr>
<tr>
<td><?php esc_html_e( 'Status', 'woocommerce-subscriptions' ); ?></td>
<td><?php echo esc_html( wcs_get_subscription_status_name( $subscription->get_status() ) ); ?></td>
</tr>但是,如果不想编辑模板文件,可以使用wcs_subscription_details_table_before_dates操作钩子:
function action_wcs_subscription_details_table_before_dates( $subscription ) {
echo '<tr><td>';
echo esc_html_e( 'Baby Name', 'woocommerce-subscriptions' );
echo '</td><td>';
echo get_post_meta( $subscription->get_id(), '_baby_name', true );
echo '</td></tr>';
}
add_action( 'wcs_subscription_details_table_before_dates', 'action_wcs_subscription_details_table_before_dates', 10, 1 );这样做的唯一缺点是,新行将显示在“状态”行下面,与上面的“状态栏”相反。
https://stackoverflow.com/questions/72905091
复制相似问题