Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AshlinRejo/e2dcee1d7a1fec13a4b89f4fd70468d6 to your computer and use it in GitHub Desktop.
Save AshlinRejo/e2dcee1d7a1fec13a4b89f4fd70468d6 to your computer and use it in GitHub Desktop.
Add to cart button with discount price for variable product
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 ); // Single product pages
function custom_add_to_cart_price( $button_text, $product ) {
/**
* Get the discount details of given product with custom price
* @param $price float/integer
* @param $product object (Product object Example: wc_get_product($product_id))
* @param $quantity int
* @param $customPrice float/integer (0 for calculate discount from product price)
* @param $returnDetails string (Default value 'discounted_price' accepted values = 'discounted_price','all')
* @param $manualRequest boolean (Default value false: pass this as true for get discount even if there is no item in cart)
* @param $isCart boolean (Default value true)
* @return array|float|int - if product has no discounts, returns $price;else return array of discount details based on $return_details
*/
$price = $product -> get_price();
$productId = $product -> get_id();
$quantity = 1;
$customPrice = 0;
$returnDetails = 'discounted_price';
$manualRequest = true;
$isCart = false;
if ( $product-> is_type('woosb') ) { // WPC Bundle
return $button_text;
} else if( $product-> is_type('variable') ) { // Variable products
$priceMin = $product -> get_variation_price( 'min', false );
$priceMax = $product -> get_variation_price( 'max', false );
$variationDiscountMin = apply_filters('advanced_woo_discount_rules_get_product_discount_price_from_custom_price', $priceMin, $product, $quantity, $customPrice, $returnDetails, $manualRequest, $isCart);
$variationDiscountMax = apply_filters('advanced_woo_discount_rules_get_product_discount_price_from_custom_price', $priceMax, $product, $quantity, $priceMax, $returnDetails, $manualRequest, $isCart);
// var_dump ( apply_filters('advanced_woo_discount_rules_get_product_discount_price_from_custom_price', $priceMax, $productId, $quantity, $customPrice, 'all', $manualRequest, $isCart) );
if ( $variationDiscountMin !== false ){ // On Sale
if ($variationDiscountMin != $variationDiscountMax) { // Ranged Price
return $button_text . ' - (' . strip_tags( wc_price( $priceMin) ) . ' - ' . strip_tags( wc_price($priceMax) ) . ') ' . strip_tags( wc_price( $variationDiscountMin ) ) . ' - ' . strip_tags( wc_price( $variationDiscountMax ) );
} else {
return $button_text . ' - (' . strip_tags( wc_price( $price ) ) . ') ' . strip_tags( wc_price( $variationDiscountMin ) );
}
} else {
if ($priceMin != $priceMax) { // Ranged Price
return $button_text . ' - ' . strip_tags( wc_price( $priceMin ) ) . ' - ' . strip_tags( wc_price( $priceMax ) );
} else {
return $button_text . ' - ' . strip_tags( wc_price( $priceMin ) );
}
}
} else { // All other product types
$discount = apply_filters('advanced_woo_discount_rules_get_product_discount_price_from_custom_price', $price, $product, $quantity, $customPrice, $returnDetails, $manualRequest, $isCart);
if($discount !== false){ // On Sale
return $button_text . ' - (' . strip_tags( wc_price( $price ) ) . ') ' . strip_tags( wc_price( $discount ) );
} else {
return $button_text . ' - ' . strip_tags( wc_price( $price ) );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment