PHP code example of baraja-core / heureka-feed

1. Go to this page and download the library: Download baraja-core/heureka-feed library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

baraja-core / heureka-feed example snippets




declare(strict_types=1);

namespace App\Services;

use Baraja\Heureka\ProductLoader;
use Baraja\Heureka\HeurekaProduct;
use Baraja\Heureka\CategoryManager;
use Baraja\Heureka\Delivery;

final class MyProductLoader implements ProductLoader
{
    public function __construct(
        private CategoryManager $categoryManager,
        private ProductRepository $productRepository,
    ) {
    }

    /**
     * @return HeurekaProduct[]
     */
    public function getProducts(): array
    {
        $products = [];

        foreach ($this->productRepository->findAllActive() as $product) {
            $heurekaProduct = new HeurekaProduct(
                itemId: (string) $product->getId(),
                product: $product->getName(),
                productName: $product->getFullName(),
                url: 'https://example.com/product/' . $product->getSlug(),
                priceVat: $product->getPrice(),
                category: $this->categoryManager->getCategory($product->getHeurekaCategoryId()),
                manufacturer: $product->getManufacturer(),
            );

            $heurekaProduct->setDescription($product->getDescription());
            $heurekaProduct->setImgUrl($product->getMainImageUrl());
            $heurekaProduct->setDeliveryDate($product->getDeliveryDays());

            if ($product->getEan() !== null) {
                $heurekaProduct->setEan($product->getEan());
            }

            $heurekaProduct->addDelivery(new Delivery(Delivery::PPL, 99.0));
            $heurekaProduct->addDelivery(new Delivery(Delivery::ZASILKOVNA, 59.0, 79.0));

            $products[] = $heurekaProduct;
        }

        return $products;
    }
}

$product->setDescription('Product description text');
$product->setImgUrl('https://example.com/image.jpg');
$product->setVideoUrl('https://youtube.com/watch?v=xxx');
$product->addImgUrlAlternative('https://example.com/image2.jpg');
$product->setVat(21.0);
$product->setDeliveryDate(3); // days or DateTime instance
$product->setEan('1234567890123');
$product->setIsbn('978-3-16-148410-0');
$product->setProductNo('SKU123');
$product->setHeurekaCpc(1.5); // max 1000
$product->setParams(['Color' => 'Red', 'Size' => 'XL', 'Wireless' => true]);
$product->setItemType('new');
$product->addAccessory('accessory-item-id');
$product->addCustomTag('CUSTOM_TAG', 'value');

$product->setParams([
    'Color' => 'Blue',
    'Weight' => '500 g',
    'Waterproof' => true,  // becomes "ano"
    'Bluetooth' => false,  // becomes "ne"
]);

use Baraja\Heureka\Delivery;

// Basic delivery
$delivery = new Delivery(Delivery::PPL, 99.0);

// Delivery with COD (cash on delivery) price
$delivery = new Delivery(Delivery::ZASILKOVNA, 59.0, 79.0);

$product->addDelivery($delivery);

use Baraja\Heureka\CategoryManager;

class MyService
{
    public function __construct(
        private CategoryManager $categoryManager,
    ) {
    }

    public function example(): void
    {
        // Get a specific category by ID
        $category = $this->categoryManager->getCategory(123);

        // Get all categories as flat array
        $allCategories = $this->categoryManager->getCategories();

        // Get categories formatted for selectbox (id => name)
        $selectableCategories = $this->categoryManager->getSelectableCategories();

        // Get categories as tree-structured selectbox (with indentation)
        $selectboxTree = $this->categoryManager->getCategoriesSelectbox();
    }
}

$category->getId();           // int - Category ID
$category->getName();         // string - Category name
$category->getParent();       // ?Category - Parent category
$category->getParentId();     // ?int - Parent category ID
$category->getCategoryText(); // string - Full path (e.g., "Heureka.cz | Electronics | Phones")
$category->getParentsPath();  // array - [id => name] of all parents

$categoryManager->setFeedUrl('https://custom-feed-url.com/categories.xml');

use Baraja\Heureka\DescriptionRenderer;

final class HtmlStripperDescriptionRenderer implements DescriptionRenderer
{
    public function render(string $haystack): string
    {
        return strip_tags($haystack);
    }
}

$feedRenderer->setDescriptionRenderer($myRenderer);

use Baraja\Heureka\Helpers;

// Validate EAN-13 barcode
Helpers::validateEAN13('1234567890123'); // bool

// Validate ISBN-10
Helpers::isValidIsbn10('0-306-40615-2'); // bool

// Validate ISBN-13
Helpers::isValidIsbn13('978-3-16-148410-0'); // bool

use Baraja\Heureka\FeedRenderer;
use Baraja\Heureka\CategoryManager;
use Nette\Http\Response;
use Nette\Caching\Storages\MemoryStorage;

// Create dependencies
$storage = new MemoryStorage();
$response = new Response();
$categoryManager = new CategoryManager($storage);

// Create renderer
$feedRenderer = new FeedRenderer($response);
$feedRenderer->setProductLoader(new MyProductLoader($categoryManager));

// Render feed
$feedRenderer->render();