MattisW
MattisW

Reputation: 107

Link Woocommerce Products directly to external site (skip detailpage)

WP: 3.71 Theme: Jupiter (artbees) v.3.02 URL: http://thefastlearners.com/store/

Hi all, I have some Woocommerce products that are external linking out to amazon or other sites.

My goal is to link these products to their external pages directly from the store page, without the detailpage!

Already tried the visibility option extension which didn’t change a thing.

Here is the original content-product.php excerpt: `

$mk_add_to_cart = 'id ) ).'" class="add_to_cart_button">'. apply_filters( 'out_of_stock_add_to_cart_text', ( 'READ MORE', 'woocommerce' ) ).''; $out_of_stock_badge = ''.( 'OUT OF STOCK', 'woocommerce' ).''; } else { ?>

switch ( $product->product_type ) {
case "external" :
    $link  = apply_filters( 'external_add_to_cart_url', get_permalink( $product->id ) );
    $label  = apply_filters( 'external_add_to_cart_text', __( 'Read More', 'woocommerce' ) );
    $icon_class = 'mk-moon-search-3';
    break;
default :
    $link  = apply_filters( 'add_to_cart_url', esc_url( $product->add_to_cart_url() ) );
    $label  = apply_filters( 'add_to_cart_text', __( 'ADD TO CART', 'woocommerce' ) );
    $icon_class = 'mk-moon-cart-plus';
    break;
}

if ( $product->product_type != 'external' ) {
    $mk_add_to_cart = &#039;<a href="&#039;. $link .&#039;" rel="nofollow" data-product_id="&#039;.$product->id.&#039;" class="add_to_cart_button product_type_&#039;.$product->product_type.&#039;"><i class="&#039;.$icon_class.&#039;"></i>&#039;. $label.&#039;</a>&#039;;
}
else {
    $mk_add_to_cart = &#039;&#039;;
}

}`

Which I changed to

´<?php
$mk_add_to_cart = &#039**;<a href="<?php echo esc_url( $product_url )**; ?>" rel="nofollow" class="single_add_to_cart_button shop-flat-btn shop-skin-btn alt"></a>&#039;;
$out_of_stock_badge = &#039;<span class="mk-out-stock">&#039;.__( &#039;OUT OF STOCK&#039;, &#039;woocommerce&#039; ).&#039;</span>&#039;;

} else { ?>

switch ( $product->product_type ) {
case "external" :
    $link     = apply_filters( &#039;external_add_to_cart_url&#039;, esc_url( $product_url ) );
    $label     = apply_filters( &#039;external_add_to_cart_text&#039;, __(&#039;Read More&#039;, &#039;woocommerce&#039;) );
    $icon_class = &#039;mk-moon-search-3&#039;;
    break;
default :
    $link  = apply_filters( &#039;add_to_cart_url&#039;, esc_url( $product_url ) );
    $label  = apply_filters( &#039;add_to_cart_text&#039;, __( &#039;ADD TO CART&#039;, &#039;woocommerce&#039; ) );
    $icon_class = &#039;mk-moon-cart-plus&#039;;
    break;
}

if ( $product->product_type != &#039;external&#039; ) {
    $mk_add_to_cart = &#039;<a href="&#039;. $link .&#039;" rel="nofollow" data-product_id="&#039;.$product->id.&#039;" class="add_to_cart_button product_type_&#039;.$product->product_type.&#039;"><i class="&#039;.$icon_class.&#039;"></i>&#039;. $label.&#039;</a>&#039;;
}
else {
    $mk_add_to_cart = &#039;&#039;;
}`

Without any results except that I don’t get any button to add anything.

from the external.php template I already figured out that the command should be something like <?php echo esc_url( $product_url ); ?>

but I don’t get where to put/link it.

Would be wonderful if you can help me out here. Once I got that working I can change the links of the title and image myself.

Thanks, Mattis

Upvotes: 1

Views: 4858

Answers (1)

thkouk
thkouk

Reputation: 43

You should try $product->product_url instead of plain $product_url.

That worked for me on the content-product.php page.

Upvotes: 1

Related Questions