ProductActionController.php

PHOTO EMBED

Wed Jan 12 2022 14:52:18 GMT+0000 (Coordinated Universal Time)

Saved by @igor #drupal #php

<?php

namespace Drupal\green_deal_core\Controller;

use Drupal\commerce_price\Price;
use Drupal\Core\Controller\ControllerBase;

class ProductActionController extends ControllerBase {

  public function test() {

    $flag_id = 'promotion_akcija';

    $flag_service = \Drupal::service('flag');
    $flag = $flag_service->getFlagById($flag_id);
    $products = $this->entityTypeManager()
      ->getStorage('commerce_product')
      ->loadByProperties([
        'type' => 'default'
      ]);

    $variation = $this->entityTypeManager()
      ->getStorage('commerce_product_variation')
      ->load(1267);

    foreach ($products as $product) {
      if($flag_service->getEntityFlaggings($flag, $product)) {
        $variation = $product->getVariations();
        $variation = reset($variation);
        $price = $variation->getPrice()->getNumber();
        $nesto[] = $price;
        $list_price = ceil($price / 0.9);
        $variation->setListPrice(new Price($list_price, 'RSD'));
        $variation->save();
        $poruka = 'nesto';
      }
    }

    return ([
      '#markup' => $poruka,
    ]);
  }

}
content_copyCOPY