我想使用woocommerce Web挂钩针对特定产品类别更改商店页面中的产品链接.
我知道如何使用woocommerce_before_shop_loop_item挂钩删除产品链接,但是我想更改商店页面中特定类别的产品链接.
任何帮助表示赞赏.
解决方法:
已更新:以下功能将替换商店链接,并在商店和档案页面上为定义的产品类别添加一个自定义链接,以添加到购物车链接.
您需要在此代码中为每次定义两次替换自定义链接和产品类别:
add_action( 'woocommerce_before_shop_loop_item', 'customizing_loop_product_link_open', 9 );
function customizing_loop_product_link_open() {
global $product;
// HERE BELOW, replace clothing' with your product category (can be an ID, a slug or a name)
if( has_term( array('clothing'), 'product_cat' )){
remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
add_action( 'woocommerce_before_shop_loop_item', 'custom_link_for_product_category', 10 );
}
}
function custom_link_for_product_category() {
global $product;
// HERE BELOW, Define the Link to be replaced
$link = $product->get_permalink();
echo '<a href="' . $link . '" class="woocommerce-LoopProduct-link">';
}
// Changing the link on the button add to cart in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'change_loop_add_to_cart_button_link', 10, 2 );
function change_loop_add_to_cart_button_link( $button, $product ) {
// HERE BELOW, replace 'clothing' with your product category (can be an ID, a slug or a name)
if( has_term( array('clothing'), 'product_cat', $product->get_id() ) ){
// HERE BELOW, Define the Link to be replaced
$link = $product->get_permalink();
$button_text = __( "View product", "woocommerce" );
$button = '<a class="button add_to_cart_button" href="' . . '">' . $button_text . '</a>';
}
return $button;
}
代码进入活动子主题(或活动主题)的function.php文件中.
经过测试和工作