首页 文章

在WooCommerce中更改特定产品类别的存档页面上的产品链接[关闭]

提问于
浏览
3

我想使用针对特定产品类别的woocommerce web hook更改商店页面中的产品链接 .

我知道如何删除与 woocommerce_before_shop_loop_item 钩子的产品链接,但我想更改特定类别的商店页面中的产品链接 . 任何帮助表示赞赏 .

1 回答

  • 3

    Updated: 以下功能将替换产品链接,并通过商店和存档页面上的自定义产品链接添加到购物车链接以获取已定义的产品类别 .

    您需要在此代码中为每个链接定义替换自定义链接和产品类别2次:

    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文件中 .

    经过测试和工作

相关问题