Woocommerce -将商店页面上的添加到购物车重定向到产品页面?



我需要在客户点击商店页面上的添加到购物车按钮后将他们重定向到产品页面。

以下代码:

add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product  ) {
$button_text = __("View product", "woocommerce");
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
return $button;
}

从下面的问题:

将"添加到购物车"按钮替换为链接到WooCommerce 3商店页面产品页面的阅读详情

完成任务。然而,它完全改变了所有的按钮标签这不是我想要的,因为我已经改变了"阅读更多"标签改为"缺货";但是这段代码将所有内容更改为"view product"

我不想重命名标签。我已经有一个代码,我可以很容易地重命名任何翻译在Woocommerce:

add_filter('gettext', x_translate_text , 20, 3);
function x_translate_text ( $translated_text, $text, $domain ) {
$translation = array (
//'Enter any translation from the translation file in Woocommerce' => 'Enter Your Translation', Example:
'Add to cart' => 'View Product',
);
if( isset( $translation[$text] ) ) {
return $translation[$text];
}
return $translated_text;
}

所以我所需要的只是一个简单的重定向从添加到购物车(在商店页面上)到产品页面,而不改变标签。

你应该删除或注释第4行代码,像这样:

add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product  ) {
//the line below is responsible for the button text 
//$button_text = __("View product", "woocommerce");
$button = '<a class="button" href="' . $product->get_permalink() . '">add to cart</a>';
return $button;
}

该代码负责按钮文本或标签

或者您可以将$button_text值更改为esc_html( $product->add_to_cart_text() ),从而从woocommerce获取按钮文本。所以你的代码应该是这样的:

add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product  ) {
//the line below is responsible for the button text 
$button_text = esc_html( $product->add_to_cart_text() );
$button = '<a class="button" href="' . $product->get_permalink() . '">'.$button_text.'</a>';

return $button;
}

最新更新