Subscriptions in Woocommerce - Add the product name next to the subscription ID

I cannot find a forum specifically for subscribing to Woocommerce, so I hope Stack can help me!

The My Subscriptions page has a table that displays all customer subscriptions. Subscriptions are called an identification number. I want to add the name of the subscription product next to the identifier.

Here is the code from the plugin that relates to adding an identifier to a 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 will notice that there is a hook that I can use to enter the code after the ID, and I figured out how to do it. The problem is that I can make it work if I manually enter the subscription identifier.

Here is my code, where 1341 is the subscription identifier. This successfully enters the product name for 1341 after the identifier 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 );

, , . , , , . ?

, , !

!

+4
2

. , . WordPress. . :

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 );
+1

, , do_action :

do_action( 'woocommerce_my_subscriptions_after_subscription_id', $subscription )

, , , $subscription.

:

function custom_add_subscription_name_to_table( $subscription ) {

$ ! , , , :

var_dump($subscription)

, , , - . , echo $subscription->name;, !

+2

Source: https://habr.com/ru/post/1659233/


All Articles