如何仅针对特定产品类别将商店页面上的外部产品链接到 WooCommerce 中的产品页面

问题描述 投票:0回答:2
Hi Everyone.

So i have found this code shown below: (credit to https://www.tychesoftwares.com/how-to-link-external-products-on-the-shop-page-to-the-product-page-in-woocommerce/)

The quesion i need help with is how to modify this code so it only works on certain product categories, at the moment the code is applied to all products sitewide.

add_filter( 'woocommerce_loop_add_to_cart_link', 'ts_link_external_product_page', 16, 3 );
function ts_link_external_product_page( $button, $product, $args ) {
  $url = $product->add_to_cart_url();
  $button_text = $product->add_to_cart_text();
  if ( 'external' === $product->get_type() ) {
    $url = $product->get_permalink();
    $button_text = "View Details";
  }
  return sprintf( '<a href="%s" data-quantity="%s" class="%s" %s>%s</a>',
    esc_url($url),
    esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
    esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
    isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
    esc_html( $button_text)
  );
}

 

I feel like i need to add a line like "if(is_product_category( array( 'catslugurl', 'anothercatslugurl' ) )){" but can not seem to be able to get it to work.

Any help with this would be great. Thank you so much :)

我觉得我需要添加一行“if(is_product_category( array( 'catslugurl', 'anothercatslugurl' ) )){”,但似乎无法让它工作。

php wordpress woocommerce product external
2个回答
1
投票

试试这个:

add_filter( 'woocommerce_loop_add_to_cart_link', 'ts_link_external_product_page', 16, 3 );

function ts_link_external_product_page( $button, $product, $args ) {
    $url         = $product->add_to_cart_url();
    $button_text = $product->add_to_cart_text();
    $categories  = array( 'music', 'clothes' ); //Your specific categories 
    $product_id  = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
    if ( has_term( $categories, 'product_cat', $product_id ) && 'external' === $product->get_type() ) {
            $url         = $product->get_permalink();
            $button_text = 'View Details';

    }
    return sprintf(
        '<a href="%s" data-quantity="%s" class="%s" %s>%s</a>',
        esc_url( $url ),
        esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
        esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
        isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
        esc_html( $button_text )
    );
}

0
投票

我添加了以下代码:

//Modify
add_filter( 'woocommerce_loop_add_to_cart_link', 'ts_replace_add_to_cart_button', 10, 2 );
function ts_replace_add_to_cart_button( $button, $product ) {
if (is_product_category() || is_shop()) {
$button_text = __("Ver más", "woocommerce");
$button_link = $product->get_permalink();
$button = '<a class="button" href="' . $button_link . '">' . $button_text . '</a>';
return $button;
}
}

它适用于类别页面,但现在该按钮没有显示在我的主页上,我在主页上使用 ELEMENTOR 显示外部产品。

在添加代码之前,它看起来像这样:

我的主页显示一个按钮,上面写着“议程”,并将用户发送到外部链接(这会阻止用户看到详细的产品页面,这并不理想。如果用户单击图像,他确实会被重定向到产品页面,但大多数用户点击按钮(有意义)。主页是 https://faceup-romanorte.com/ 。类别页面上的情况相同,例如 https://faceup-romanorte.com/ tratamiento-categoria/valoraciones/

Main Page

Category Page

当我添加代码时,类别页面上的按钮确实发生了变化

Category Page with modified code

但是它隐藏了主页上的按钮,这也不理想

Main page when adding code - buttons disappear

任何人都可以帮助解决我在这里缺少的东西吗?

© www.soinside.com 2019 - 2024. All rights reserved.