我是编码新手,目前正在使用woocommerce订阅插件,并希望更改默认" shop"如果我没有订阅。我希望它链接到特定产品而不是商店。
以下是此部分的当前代码:
<?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
任何帮助将不胜感激。
答案 0 :(得分:2)
要实现这一目标,您需要:
woocommerce_subscriptions_message_store_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文件中或任何插件文件中。
经过测试和工作