Cart / Checkout: Autoship Percent Discount String (Sale Price Based)

Changes the Autoship Discount String text to reflect percentage based off of the Sale Price (if sale price exists). If no sale price, discount percentage based off of regular price. This does not change any values, just the display text

Note: Always test customizations to your site on a staging environment before making changes to your live / production site.  If you are not experienced with testing changes on a staging site, here is a good article to review.

/**
* Adjust Autoship Discount String so text shows percentages ( Checkout & Recurring )
* based off sale price not regular. If no sale price exists the discount percentages
* will be calculated off Regular price.
*
* NOTE IMPORTANT - This adjusts the text only.  It's important that the
* values entered for the Checkout and Recurring prices are entered
* relative to the Sale price.
* @param array $prices The current product/variation prices and discount pct.
* @param int $product_id The WC_Product id.
* @param WC_Product $product The woocommerce product.
*
* @return array The filtered prices array.
*/
function xx_autoship_adjust_discount_string( $prices, $product_id, $product ){

    // If a sale price exists adjust the checkout pct discount based off that.
    if ( isset( $prices['sale_price'] ) && !empty( $prices['sale_price'] ) )
    $prices['autoship_percent_discount'] = 100 - round( ( $prices['autoship_checkout_price'] / $prices['sale_price'] ) * 100 , 2 );
  
    // If a sale price exists adjust the recurring pct discount based off that.
    if ( isset( $prices['sale_price'] ) && !empty( $prices['sale_price'] ) )
    $prices['autoship_percent_recurring_discount'] = 100 - round( ( $prices['autoship_recurring_price'] / $prices['sale_price'] ) * 100 , 2 );
  
    return $prices;
  
  }
  add_filter('autoship_all_prices_array', 'xx_autoship_adjust_discount_string', 10, 3 );

Still need help? Contact Us Contact Us