• Resolved Jason Wong

    (@eljkmw)


    I like to amend the last line of

    public function add_compare_link( $product_id = false, $args = array() )

    without any core hack.

    The last line of code is

    printf( '<a href="%s" class="%s" data-product_id="%d" rel="nofollow">%s</a>', $this->add_product_url( $product_id ), 'compare' . ( $is_button == 'button' ? ' button' : '' ), $product_id, $button_text );

    And I want it to be

    printf( '<a href="%s" class="%s" data-product_id="%d" data-tip="%s" rel="nofollow"><span>%s</span></a>', $this->add_product_url( $product_id ), 'compare tip-top' . ( $is_button == 'button' ? ' button' : '' ), $product_id, $button_text, $button_text );

    Please advise. Thank you in advance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Jason Wong

    (@eljkmw)

    I’ve implemented the following snippet into my active theme’s functions.php

    
    /* YITH: YITH WooCommerce Wishlist
    =============================================================== */
    if ( class_exists( 'YITH_Woocompare' ) ) {
    	global $yith_woocompare;
    
    	// Remove Compare default link after Single Product page summary
    	if ( get_option( 'yith_woocompare_compare_button_in_product_page' ) == 'yes' )  remove_action( 'woocommerce_single_product_summary', array( $yith_woocompare->obj, 'add_compare_link' ), 35 );
    
    	// Add custom link to compare
    	function add_compare_custom_link( $product_id = false, $args = array() ) {
    		extract( $args );
    
    		if ( ! $product_id ) {
    			global $product;
    			$product_id = ! is_null( $product ) ? yit_get_prop( $product, 'id', true ) : 0;
    		}
    
    		// return if product doesn't exist
    		if ( empty( $product_id ) || apply_filters( 'yith_woocompare_remove_compare_link_by_cat', false, $product_id ) )
    			return;
    
    		$is_button = ! isset( $button_or_link ) || ! $button_or_link ? get_option( 'yith_woocompare_is_button' ) : $button_or_link;
    
    		if ( ! isset( $button_text ) || $button_text == 'default' ) {
    			$button_text = get_option( 'yith_woocompare_button_text', __( 'Compare', 'yith-woocommerce-compare' ) );
    			do_action ( 'wpml_register_single_string', 'Plugins', 'plugin_yit_compare_button_text', $button_text );
    			$button_text = apply_filters( 'wpml_translate_single_string', $button_text, 'Plugins', 'plugin_yit_compare_button_text' );
    		}
    
    		printf( '<a href="%s" class="%s" data-product_id="%d" data-tip="%s" rel="nofollow"><span>%s</span></a>', $yith_woocompare->obj->add_product_url( $product_id ), 'compare tip-top' . ( $is_button == 'button' ? ' button' : '' ), $product_id, $button_text, $button_text );
            }
    
    	// Add Compare custom link after Single Product page summary
    	if ( get_option( 'yith_woocompare_compare_button_in_product_page' ) == 'yes' )  add_action( 'woocommerce_single_product_summary', 'add_compare_custom_link', 35 );
    
    }

    It isn’t working as I found that $yith_woocompare->obj->add_product_url( $product_id ) is producing an error ~ PHP Fatal error: Call to a member function add_product_url()

    If I replace this with ‘#’, I can see that the custom link gets inserted with the ‘a’ tag being ‘#’. I cannot figure out how to get around the $yith_woocompare->obj->add_product_url( $product_id ) problem.

    Can anyone know how to resolve this?
    Please advise. Thank you.

    Thread Starter Jason Wong

    (@eljkmw)

    Finally, I managed to overcome the hurdle.

    Apparently, I had to copy-&-paste the entire “add_product_url” function from “/yith-woocommerce-compare/includes/class.yith-woocompare-frontend.php” into my active theme’s functions.php. Then replace the variable “$this->action_add” with “$action_add” in that function, and insert “$action_add = ‘yith-woocompare-add-product’;” . The entire snippet is as shown below.

    /* YITH: YITH WooCommerce Wishlist
    =============================================================== */
    if ( class_exists( 'YITH_Woocompare' ) ) {
    	global $yith_woocompare;
    
    	// Add custom link to compare
    	function add_compare_custom_link( $product_id = false, $args = array() ) {
    		extract( $args );
    
    		if ( ! $product_id ) {
    			global $product;
    			$product_id = ! is_null( $product ) ? yit_get_prop( $product, 'id', true ) : 0;
    		}
    
    		// return if product doesn't exist
    		if ( empty( $product_id ) || apply_filters( 'yith_woocompare_remove_compare_link_by_cat', false, $product_id ) )
    			return;
    
    		if ( ! isset( $button_text ) || $button_text == 'default' ) {
    			$button_text = get_option( 'yith_woocompare_button_text', __( 'Compare', 'yith-woocommerce-compare' ) );
    			do_action ( 'wpml_register_single_string', 'Plugins', 'plugin_yit_compare_button_text', $button_text );
    			$button_text = apply_filters( 'wpml_translate_single_string', $button_text, 'Plugins', 'plugin_yit_compare_button_text' );
    		}
    
    		printf( '<a href="%s" class="%s" data-product_id="%d" data-tip="%s" rel="nofollow"><span>%s</span></a>', add_product_url( $product_id ), 'compare tip-top', $product_id, 'Add to ' . $button_text, $button_text );
    	}
    
    	// The URL to add the product into the comparison table
    	function add_product_url( $product_id ) {
    		$action_add = 'yith-woocompare-add-product';
    
    		$url_args = array(
    			'action' => $action_add,
    			'id' => $product_id
    		);
    
    		$lang = defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : false;
    		if( $lang ) {
    			$url_args['lang'] = $lang;
    		}
    
    		return apply_filters( 'yith_woocompare_add_product_url', esc_url_raw( add_query_arg( $url_args, site_url() ) ), $action_add );
    	}
    
    	// Check if Compare button is in Product page
    	if ( get_option( 'yith_woocompare_compare_button_in_product_page' ) == 'yes' ) {
    		// Remove Compare default link after Single Product page summary
    		remove_action( 'woocommerce_single_product_summary', array( $yith_woocompare->obj, 'add_compare_link' ), 35 );
    
    		// Add Compare custom link after Single Product page summary
    		add_action( 'woocommerce_single_product_summary', 'add_compare_custom_link', 35 );
    	}
    
    }

    I hope this will help others who wish to replace the plugin’s default Compare button.
    *\(^.^)/*

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘How to amend “add_compare_link” function w/o core hack?’ is closed to new replies.