phpwordpresswoocommerceproductpermalinks

How to disable the link from product title in WooCommerce Archives


I would like to develop wooCommerce custom plugin. For the project requirement product title hyperlink will be disable.For this simple task i define the following script.Please give me the solution How to fix this.I want to use it WordPress custom plugin.

class Custom_WooCommerce_Disable_Title_Link {
    public function __construct() {
        add_action( 'init', array( $this, 'remove_title_hyperlink' ) );
    }

    public function remove_title_hyperlink() {

        remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
    }
}

new Custom_WooCommerce_Disable_Title_Link();

I tried about above script. this is doesn't work


Solution

  • The following code has been tested in a custom plugin (Tested with Storefront theme, on last WordPress and WooCommerce versions). It works also in functions.php file of a child theme.

    To disable the product link only for product titles from WooCommerce Archive pages, use the following:

     class WC_Loop_Product_Title_No_Link {
        public function __construct() {
            add_action( 'init', array( $this, 'remove_link_from_product_title' ) );
        }
    
        public function remove_link_from_product_title() {
            // Close link before product title
            add_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_link_close', 5 );
            // Open link after product title
            add_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_link_open', 20 );
        }
    }
    
    new WC_Loop_Product_Title_No_Link();
    

    Note that this will not work with some themes that have their own related customizations.