4

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!

LBF
  • 1,133
  • 2
  • 14
  • 39

2 Answers2

3

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 your code to the 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', 'custom_add_subscription_name_to_table', 35 );
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
James Jones
  • 3,850
  • 5
  • 25
  • 44
2

If you check the source of the template file, you'll see that the do_action call looks like this:

do_action( 'woocommerce_my_subscriptions_after_subscription_id', $subscription )

That means that in the action that you're adding, you'll have access to the passed in $subscription object.

Therefore, change your function declaration to look like this:

function custom_add_subscription_name_to_table( $subscription ) {

Which will get you access to the $subscription object in YOUR function! I'm not sure exactly what the code will be to output the name, but you can easily figure it out by doing this as the first line of your function:

var_dump($subscription)

This will print out everything available to you in the subscription object, and I'm sure the name is in there somewhere. It's probably just echo $subscription->name;, but you'll want to double check!

Greg Burkett
  • 1,905
  • 1
  • 12
  • 14
  • 1
    Thank's Greg. I appreciate the explanation of why I needed to add $subscription to my function declaration. That will help me in the future. – LBF Oct 29 '16 at 17:25