更改我的帐户页面内的woocommerce订阅“商店”链接

问题描述 投票:2回答:1

我是编码的新手,目前正在使用woocommerce订阅插件,如果我没有任何订阅,我想改变默认的“商店”。我希望它链接到特定产品而不是商店。

我想改变红色部分:I want to change the red part

以下是此部分的当前代码:

<?php
        // translators: placeholders are opening and closing link tags to take to the shop page
        printf( esc_html__( 'Anda masih belum melanggan. Mula melanggan %sdi sini%s.', 'woocommerce-subscriptions' ), '<a href="' . esc_url( apply_filters( 'woocommerce_subscriptions_message_store_url', get_permalink( wc_get_page_id( 'shop' ) ) ) ) . '">', '</a>' );
        ?>
    </p>

<?php endif; ?>

我想将example.com/shop改成像example.com/product-category/myproduct这样的东西

任何帮助将不胜感激。

php wordpress woocommerce account woocommerce-subscriptions
1个回答
2
投票

为此,您需要:

  • 一种条件函数,用于检测当前用户是否具有活动订阅
  • 自定义函数挂钩在woocommerce_subscriptions_message_store_url过滤器挂钩中,您将在其中定义链接到您的产品的新URL。

这是代码:

// Conditional function detecting if the current user has an active subscription
function has_active_subscription( $user_id=null ) {
    // if the user_id is not set in function argument we get the current user ID
    if( null == $user_id )
        $user_id = get_current_user_id();

    // Get all active subscrptions for a user ID
    $active_subscriptions = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => $user_id,
        'post_type'   => 'shop_subscription', // Subscription post type
        'post_status' => 'wc-active', // Active subscription
    ) );
    // if user has an active subscription
    if( ! empty( $active_subscriptions ) ) return true;
    else return false;
}


// Change woocommerce subscription “shop” link inside my account page
add_filter( 'woocommerce_subscriptions_message_store_url', 'custom_subscriptions_message_store_url', 10, 1 );
function custom_subscriptions_message_store_url( $url ){

    // If current user has NO active subscriptions 
    if( ! has_active_subscription() ){

        // HERE Define below the new URL linked to your product.
        $url = home_url( "/product-category/myproduct/" );
    }
    return $url;
}

此代码位于活动子主题(或主题)的function.php文件中,或者也可以放在任何插件文件中。

经过测试和工作

© www.soinside.com 2019 - 2024. All rights reserved.