Skip to content

Instantly share code, notes, and snippets.

@statickidz
Last active February 9, 2020 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statickidz/72be96dca9cd39331f1fcbdc59392f5b to your computer and use it in GitHub Desktop.
Save statickidz/72be96dca9cd39331f1fcbdc59392f5b to your computer and use it in GitHub Desktop.
Woocommerce - External Links New Tab for Affiliates
<?php
/**
* @snippet Woocommerce - External Links New Tab for Affiliates
* @sourcecode https://gist.github.com/statickidz/72be96dca9cd39331f1fcbdc59392f5b
* @author Adrián Barrio Andrés (statickidz.com)
*/
/**
* Redirect WC external products single to his affiliate link
*/
add_action( 'template_redirect', 'skidz_redirect_external_products' );
function skidz_redirect_external_products() {
global $post;
if ( is_singular( 'product' ) && ! empty( $post ) && ( $product = wc_get_product( $post ) ) && $product->is_type( 'external' ) ) {
wp_redirect( $product->get_product_url() );
exit;
}
}
/**
* Update WC external product link to his affiliate link
*/
add_filter( 'post_type_link', 'skidz_update_external_product_link', 10, 2 );
function skidz_update_external_product_link( $url, $post )
{
if ( ! empty( $post ) && ( $product = wc_get_product( $post ) ) && $product->is_type( 'external' ) ) {
return $product->get_product_url();
}
return $url;
}
/**
* Replace WC external products blocks links to open in new tab and no refollow
*/
add_filter( 'woocommerce_blocks_product_grid_item_html', 'skidz_product_block', 10, 3 );
function skidz_product_block( $html, $data, $product ) {
$html = str_replace('rel="nofollow"', '', $html);
$html = str_replace('target="_blank"', '', $html);
$html = str_replace('<a', '<a target="_blank" rel="nofollow"', $html);
return $html;
}
.add_to_cart_button {
position: relative;
padding-left: 47px;
background: #f0c14b;
background: linear-gradient(to bottom, #f7dfa5, #f0c14b) repeat scroll 0 0 rgba(0, 0, 0, 0);
color: #111 !important;
cursor: pointer;
font-size: 15px;
font-weight: 400;
line-height: 20px;
text-align: center;
text-decoration: none !important;
}
.add_to_cart_button:before {
position: absolute;
content: '';
top: 0;
right: 0;
bottom: 0;
left: 0;
background-repeat: no-repeat;
background-size: 18px 18px;
background-position: 20px center;
box-sizing: border-box;
background-image: url(https://mk0getaawprm95y8pmm9.kinstacdn.com/wp-content/plugins/aawp/public/assets/img/icon-amazon-black.svg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment