Content Egg Range Offer prices in WC Product

Content Egg Range Offer prices in WC Product

If you have the Content Egg plugin installed you can know that this plugin has a shortcode which allows list the Offers in the WooCommerce Product. Usually, the plugin displays the price of the cheapest product from the Offer list but with help of this snippet you can get price from the cheapest to the most expensive products divided with the hyphen.

function bd_cegg_get_wc_price( $product_id, $args = array() ){
  if(false == is_plugin_active('content-egg/content-egg.php'))
    return;

  $args = wp_parse_args(
    $args, array(
      'currency' => '',
      'decimal_separator' => wc_get_price_decimal_separator(),
      'thousand_separator' => wc_get_price_thousand_separator(),
      'decimals' => wc_get_price_decimals(),
      'price_format' => get_woocommerce_price_format(),
    )
  );

  $affiliate_modules = ContentEgg\application\components\ModuleManager::getInstance()->getAffiliteModulesList(true);
  
  if( empty($affiliate_modules) )
    return;

  $offer_prices = array();
  $currency_code = get_woocommerce_currency();
  $count = 0;
  foreach ($affiliate_modules as $module_id => $module_name) {
     $data = ContentEgg\application\components\ContentManager::getViewData($module_id, $product_id);
     if( !empty( $data ) ){
       foreach( $data as $offer_id => $offer_arr ){
        $currency_rate = ContentEgg\application\helpers\CurrencyHelper::getCurrencyRate($offer_arr['currencyCode'], $currency_code);
        if(!$currency_rate) $currency_rate = 1;
        $offer_prices[] = round( ($offer_arr['price'] * $currency_rate), $args['decimals']);
    $count++;
      }
     }
  }
  
  if( empty($offer_prices) )
    return;

  sort( $offer_prices, SORT_NUMERIC );
  $count = count($offer_prices);
  $low_price = $offer_prices[0];
  $high_price = ( $count > 1 ) ? $offer_prices[$count - 1] : '';
  $low_price = number_format( $low_price, $args['decimals'], $args['decimal_separator'], $args['thousand_separator'] );
  
  if( !empty($high_price) ){
    $high_price = number_format( $high_price, $args['decimals'], $args['decimal_separator'], $args['thousand_separator'] );
    $price = $low_price .' - '. $high_price;
  }else{
    $price = $low_price;
  }
  
  $formatted_price = sprintf( $args['price_format'], '<span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol( $args['currency'] ) . '</span>', $price );
  $formatted_price = '<span class="woocommerce-Price-amount amount">' . $formatted_price . '</span>';
  
  if( $args['count_offers'] == true ){
   $formatted_price .= '<span class=="woocommerce-Offers-amount">'. $count .' '. __('Offers', 'rh-grandchild') .'</span>';
  }
  
  $return = '<span class="price">'. $formatted_price .'</span>';
  
  return $return;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.