<?php

use KupShop\KupShopBundle\Util\Compat\ServiceContainer;

function smarty_function_get_pompo_product_availability($params, &$smarty)
{
    $cart = ServiceContainer::getService(\KupShop\OrderingBundle\Cart::class);

    $productId = $params['product']['id'] ?? null;
    $variationId = $params['product']['id_variation'] ?? null;

    $result = 0;
    foreach ($cart->getPurchaseState()->getProducts() as $item) {
        if ($item->getIdProduct() == $productId && $item->getIdVariation() == $variationId) {
            $result = $item->getNote()['availability'] ?? 0;
            break;
        }
    }

    if (!empty($params['assign'])) {
        $smarty->assign($params['assign'], $result);
    } else {
        return $result;
    }
}
