LBF LBF - 1 month ago 18
PHP Question

Woocommerce Subscriptions - add product name next to subscription ID

I can't find a forum specifically for Woocommerce Subscriptions, so I'm hoping Stack can help me!

On the "My Subscriptions" page, there is a table which displays all of the customer's subscriptions. The subscriptions are named by the ID number. I want to add the subscription product name next to the ID.

Here is the code from the plugin that pertains to adding the ID to the table:

<?php foreach ( $subscriptions as $subscription_id => $subscription ) : ?>
<tr class="order">
<td class="subscription-id order-number" data-title="<?php esc_attr_e( 'ID', 'ultimatewoo-pro' ); ?>">
<a href="<?php echo esc_url( $subscription->get_view_order_url() ); ?>"><?php echo esc_html( sprintf( _x( '#%s', 'hash before order number', 'ultimatewoo-pro' ), $subscription->get_order_number() ) ); ?></a>
<?php do_action( 'woocommerce_my_subscriptions_after_subscription_id', $subscription ); ?>
</td>
</tr>
<?php endforeach; ?>


You'll notice there is a hook I can use to inject code after the ID, and I figured out how to do this. The problem is, I can only make it work if I manually type in the subscription ID.

Here's my code, where 1341 is the subscription ID. This successfully injects the product name for 1341 after the ID in the table:

function custom_add_subscription_name_to_table() {

$subscription = wcs_get_subscription(1341);

foreach ( $subscription->get_items() as $item_id => $item ) {
$_product = apply_filters( 'woocommerce_subscriptions_order_item_product', $subscription->get_product_from_item( $item ), $item );

if ( apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {

echo wp_kses_post( apply_filters( 'woocommerce_order_item_name', sprintf( '<a href="%s">%s</a>', get_permalink( $item['product_id'] ), $item['name'] ), $item ) );

}
}
}
add_action( 'woocommerce_my_subscriptions_after_subscription_id', 'custom_add_subscription_name_to_table', 35 );


The problem, I cannot figure out how to get the ID dynamically as a variable. I obviously can't use it if I have to manually enter the ID. Is there any way to do this?

I feel like I'm so close, yet so far!

Thank you!

Answer

You are close. You don't need the id for what you're doing. The subscription is being passed into the function by the WordPress hooks system. You simply have to use it. Change code to tge following:

function custom_add_subscription_name_to_table( $subscription ) {

    foreach ( $subscription->get_items() as $item_id => $item ) {
        $_product  = apply_filters( 'woocommerce_subscriptions_order_item_product', $subscription->get_product_from_item( $item ), $item );

        if ( apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {

            echo wp_kses_post( apply_filters( 'woocommerce_order_item_name', sprintf( '<a href="%s">%s</a>', get_permalink( $item['product_id'] ), $item['name'] ), $item ) );

        }
    }
}
add_action( 'woocommerce_my_subscriptions_after_subscription_id', 'bee_add_subscription_name_to_table', 35 );
Comments