Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EricBusch/c6904dc6c0964b3f6bfe2a6292f859f2 to your computer and use it in GitHub Desktop.
Save EricBusch/c6904dc6c0964b3f6bfe2a6292f859f2 to your computer and use it in GitHub Desktop.
Replace product's [Buy Now] button URL with link to lowest priced product from the Comparison Set. [datafeedr][dfrcs][woocommerce]
<?php
/**
* Replace product's [Buy Now] button URL with link to lowest priced product from
* the Comparison Set.
*
* If a Comparison Set does not exist for the product or one has not been cached
* or the number of products returned does not meet the minimum required products
* specified in the "Minimum Number of Results" field, then this will just return
* the default $url.
*
* @param string $url Product link
* @param WC_Product $product
*
* @return string URL of product.
*/
function mycode_replace_buy_url_with_url_of_lowest_priced_product( $url, $product ) {
$source = dfrcs_wc_get_source_of_product( $product );
$source['context'] = 'wc_single_product_page';
$compset = new Dfrcs( $source );
// If compset is not cached, return default $url.
if ( ! $compset->cached ) {
return $url;
}
// If compset does not contain a lowest price product field, return default $url.
if ( empty( $compset->lowest_priced_product ) ) {
return $url;
}
// If compset is not being displayed because of min product requirement, return default $url.
if ( ! $compset->meets_min_num_product_requirement() ) {
return $url;
}
$url = dfrcs_url( $compset->lowest_priced_product );
return $url;
}
add_filter( 'woocommerce_product_add_to_cart_url', 'mycode_replace_buy_url_with_url_of_lowest_priced_product', 20, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment