Customizing Shop Page products to go straight to A

2020-04-21 08:01发布

What do I need to use on WooCommerce, so when you see a product on the shop/catalogue page it links straight through to the affiliate site and not via the single product page.

Then if possible for that to open in a new tab.

Thanks

1条回答
爷的心禁止访问
2楼-- · 2020-04-21 08:40

Update: Added WC 3+ compatibility

There is 3 related custom hooked functions for your case, that you will need to customize:

### Custom Product link ###

// Removing the default hooked function
remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
// Add back the hook to a custom function
add_action ( 'woocommerce_before_shop_loop_item', 'custom_loop_product_link', 10 );
function custom_loop_product_link() {
    $custom_link = get_permalink( 16 ); // link to a custom page
    echo '<a href="' . $custom_link . '" class="woocommerce-LoopProduct-link">';
}

### Custom add-to-cart link ###

add_filter( 'woocommerce_loop_add_to_cart_link', 'customizing_add_to_cart_button', 10, 2 );
function customizing_add_to_cart_button( $link, $product ){
    // CUSTOM ADD TO CART text and link
    $add_to_cart_url = site_url('/custom_link/');
    $button_text =  __('View now', 'woocommerce');

    // compatibility with WC +3
    $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
    $product_type = method_exists( $product, 'get_type' ) ? $product->get_type() : $product->product_type;

    $link = sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button product_type_%s">%s</a>',
        esc_url( $add_to_cart_url ),
        esc_attr( $product_id ),
        esc_attr( $product->get_sku() ),
        esc_attr( isset( $quantity ) ? $quantity : 1 ),
        esc_attr( $product_type ),
        esc_html( $button_text )
    );

    return $link;
}

### Custom link Redirect after add-to-cart ###

add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect', 10, 1 );
function my_custom_add_to_cart_redirect( $url ) {
    $url = get_permalink( 16 ); // an example: here redirect is on page ID 16
    return $url;
}

The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

查看更多
登录 后发表回答