【问题标题】:Change woocommerce subscription "shop" link inside my account page在我的帐户页面中更改 woocommerce 订阅“商店”链接
【发布时间】:2017-12-19 12:05:54
【问题描述】:

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

我要更改红色部分:

以下是本节的当前代码:

<?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】:

    为此,您需要:

    • 检测当前用户是否有活动订阅的条件函数
    • 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 文件中或任何插件文件中。

    经过测试并且可以工作

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 2018-07-19
    相关资源
    最近更新 更多