green_deal_core.module HOW TO USE PREPROCESS HOOK

PHOTO EMBED

Wed Jan 12 2022 22:17:41 GMT+0000 (Coordinated Universal Time)

Saved by @igor #drupal #php

/**
 * Implements hook_preprocess_HOOK().
 */
function green_deal_core_preprocess_commerce_product__teaser(&$variables) {
  $product = $variables['elements']['#commerce_product'];
  $product_variation = reset($product);
  $price = $product_variation->get('price')->number;
  $list_price = $product_variation->get('list_price')->number;

  if ($list_price != NULL) {
    $percentage = round((($list_price - $price) / $list_price) * 100);
    if ($percentage < 10) {
      $percentage = 10;
    }
    $variables['percentage'] = $percentage . '%';
  }
}

function green_deal_core_preprocess_commerce_product__full(&$variables) {
  $product = $variables['elements']['#commerce_product'];
  $product_variation = reset($product);
  $price = $product_variation->get('price')->number;
  $list_price = $product_variation->get('list_price')->number;

  if ($list_price != NULL) {
    $percentage = round((($list_price - $price) / $list_price) * 100);
    if ($percentage < 10) {
      $percentage = 10;
    }
    $variables['percentage'] = $percentage . '%';
  }
}
content_copyCOPY