在我的 WooCommerce 网上商店中,我想将“Return to shop” URL 更改为自定义 URL。我尝试在事件主题的 function.php
文件中使用下面的代码,但它不起作用。
在我的网站上,我有五种由 WPML 管理的活跃语言商业插件。它还运行一个脚本,确保来自这些国家的访问者被重定向到他们自己的语言。
/**
* Changes Return to Shop button URL on Cart page.
*
*/
function wc_empty_cart_redirect_url() {
return 'http://pacsymposium.com/';
}
add_filter( 'woocommerce_return_to_shop_redirect', 'wc_empty_cart_redirect_url' );
我怎样才能让它工作以获取当前的语言商店链接?
谢谢。
最佳答案
Update2:在您的代码中,您需要使用:
- WooCommerce
wc_get_page_id()
获取 WooCommerce 商店页面 ID 的函数。 - WPML
wpml_object_id
过滤器 Hook 以获取商店的当前语言翻译页面 ID。 - WooCommerce
wc_get_page_permalink()
由过滤器 Hook 本身使用(参见 HERE )
使用该 Material ,您可以获得商店的当前翻译链接(或任何其他链接)。
所以你的代码将是:
add_filter( 'woocommerce_return_to_shop_redirect', 'wc_empty_cart_redirect_url' );
function wc_empty_cart_redirect_url() {
// Getting the shop ID
$shop_id = wc_get_page_id( 'shop' );
// Getting the current language ID for the shop page
$current_lang_id = apply_filters( 'wpml_object_id', $shop_id, 'page', TRUE );
// Getting the post object for the ID
$post = get_post($current_lang_id);
// Getting the slug from this post object
$slug = $post->post_name;
// We re-use wc_get_page_permalink() function just like in this hook
$link = wc_get_page_permalink( $slug );
return $link;
}
代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。
最后我测试了并且它有效......
关于php - 使用 WPML 插件更改所有语言的 "return to shop"URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42231656/