Damon
Damon

Reputation: 135

How to setup external products in woocommerce to that product details page is bypassed?

I want to setup a woocommerce website with external products. I want to take the customer to the main website of the product (external link) on clicking the product without showing them the product details page.

Bypass the product details page by clicking the product thhumbnail and opening the external link in new tab.

How can i do this ?

Upvotes: 1

Views: 2391

Answers (1)

Andrew Schultz
Andrew Schultz

Reputation: 4243

This is how this code works, I override the default loop product link and associate it with my own custom field that appears under the General TAB when you edit the product called "External URL". When an external URL is entered the link will take the user to another website and when no external URL is present it links the user to the details page. I took some of this code from this great article about creating custom product fields.

Paste this code into your functions.php file.

remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
add_action( 'woocommerce_before_shop_loop_item', 'modify_woocommerce_template_loop_product_link_open', 10 );

// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

function woo_add_custom_general_fields() {
    global $woocommerce, $post;

    echo '<div class="options_group">';

    woocommerce_wp_text_input( 
        array( 
            'id'          => '_external_url', 
            'label'       => __( 'External URL', 'woocommerce' ), 
            'placeholder' => 'http://',
            'desc_tip'    => 'true',
            'description' => __( 'Enter the external URL here.', 'woocommerce' ) 
        )
    );

    echo '</div>';
}

function woo_add_custom_general_fields_save( $post_id ){
    $woocommerce_text_field = $_POST['_external_url'];

    if( !empty( $woocommerce_text_field ) )
        update_post_meta( $post_id, '_external_url', esc_attr( $woocommerce_text_field ) );

}

function modify_woocommerce_template_loop_product_link_open() {
    $product_url =  get_post_meta( get_the_ID(), '_external_url', true );

    if( empty( $product_url) )
        echo '<a href="' . esc_url( get_the_permalink() ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
    else
        echo '<a href="' . esc_url( $product_url ) . '" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
}

Upvotes: 2

Related Questions