autoship_get_calculated_scheduled_order_item_sale_price (function)

(src/scheduled-orders.php)  Used to retrieve or calculate the supplied Scheduled Order items sale price including coupon discounts. It takes the $scheduled_item (array) and $scheduled_order (array) as its parameters

Code

 
 * Retrieves or Calculates the supplied Scheduled Order items sale price including coupon discounts.
 *
 * @param array $scheduled_item An Array of Scheduled Order Item data.
 * @param array $scheduled_order The Scheduled Order data
 * @return float the price
 */
function autoship_get_calculated_scheduled_order_item_sale_price( $scheduled_item, $scheduled_order ){

  // Get any Discounts that need to be re-applied
  $discounts = autoship_get_valid_coupons_from_order( $scheduled_order );

  // Get the price to use
  $price = !empty( $scheduled_item['salePrice'] ) ? $scheduled_item['salePrice'] : $scheduled_item['price'];

  if ( apply_filters( 'autoship_exclude_product_coupon_discounts_in_saleprice', false ) )
  return $price;

  /**
  *  Apply the discounts
  */
  foreach ( $discounts as $code => $coupon ) {

    // Confirm the product for this coupon is still on the order
    if ( $coupon['productDiscountNotAppliesWhenItHasSalePrice'] && !empty( $scheduled_item['salePrice'] ) )
    continue;

    if (
      'ReduceProductPriceByPercentage' == $coupon['discountType'] &&
      !empty( $coupon['productGroupId'] ) &&
      !empty( $scheduled_item['product']['productGroupIds'] ) &&
      in_array( $coupon['productGroupId'], $scheduled_item['product']['productGroupIds'] )
    ){

      // Calculate the new sale price
      $price = $price - ( $price * ( $coupon['amount'] / 100 ) );

    } else if (
      'ReduceProductPriceByPercentage' == $coupon['discountType'] &&
      isset( $coupon['product'] ) &&
      ( $coupon['product']['externalId'] == $scheduled_item['productId'] )
    ){

      // Calculate the new sale price
      $price = $price - ( $price * ( $coupon['amount'] / 100 ) );

    } else if (
      'ReduceProductPriceByAmount' == $coupon['discountType'] &&
      !empty( $coupon['productGroupId'] ) &&
      !empty( $scheduled_item['product']['productGroupIds'] ) &&
      in_array( $coupon['productGroupId'], $scheduled_item['product']['productGroupIds'] )
    ){

      // Calculate the new sale price
      $discount_price = $price - $coupon['amount'];
      $price = $discount_price >= 0 ? $discount_price : 0;

    } else if (
      'ReduceProductPriceByAmount' == $coupon['discountType'] &&
      isset( $coupon['product'] ) &&
      ( $coupon['product']['externalId'] == $scheduled_item['productId'] )
    ){

      // Calculate the new sale price
      $discount_price = $price - $coupon['amount'];
      $price = $discount_price >= 0 ? $discount_price : 0;

    }

    // Check if the price is reduced to Free & stop discounts
    if ( !$price )
    return 0;

  }

  return $price;

}