Skip to content

Magento 2 How to export Fixed product taxes value in csv file

How can I export a products with attributes of type “Fixed Product Tax”? I tried to create one on a product manually and export it, but nothing appeared in the generated CSV.
How can I achieve this?
My controller code is here..

<?php

namespace VendorExtensionControllerAdminhtmlExport;

use MagentoBackendAppAction;
use MagentoBackendAppActionContext;
use MagentoFrameworkAppResponseHttpFileFactory;
use MagentoCatalogApiProductRepositoryInterface;
use MagentoFrameworkAppConfigScopeConfigInterface;
use MagentoStoreModelStoreManagerInterface;
use MagentoFrameworkApiSearchCriteriaBuilder;
use MagentoCatalogModelProductAttributeRepository as AttributeRepository;
use MagentoCatalogModelProductAttributeSourceStatus as ProductStatus;
use MagentoCatalogModelProductVisibility as ProductVisibility;
use MagentoCatalogApiDataProductInterface;

class Index extends Action
{
    protected $fileFactory;
    protected $productRepository;
    protected $scopeConfig;
    protected $storeManager;
    protected $searchCriteriaBuilder;
    protected $attributeRepository;

    public function __construct(
        Context $context,
        FileFactory $fileFactory,
        ProductRepositoryInterface $productRepository,
        ScopeConfigInterface $scopeConfig,
        StoreManagerInterface $storeManager,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        AttributeRepository $attributeRepository
    ) {
        parent::__construct($context);
        $this->fileFactory = $fileFactory;
        $this->productRepository = $productRepository;
        $this->scopeConfig = $scopeConfig;
        $this->storeManager = $storeManager;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->attributeRepository = $attributeRepository;
    }

    public function execute()
    {
        $fileName = 'fixed_product_tax.csv';
        $content = $this->getCsvContent();

        return $this->fileFactory->create(
            $fileName,
            $content,
            MagentoFrameworkAppFilesystemDirectoryList::VAR_DIR,
            'text/csv'
        );
    }

    protected function getCsvContent()
    {
        $searchCriteria = $this->searchCriteriaBuilder
            ->addFilter(ProductInterface::STATUS, ProductStatus::STATUS_ENABLED)
            ->addFilter(ProductInterface::VISIBILITY, ProductVisibility::VISIBILITY_BOTH)
            ->create();

        $productCollection = $this->productRepository->getList($searchCriteria);

        $data = [];
        $data[] = ['SKU', 'Fixed Product Tax'];
        foreach ($productCollection->getItems() as $product) {
            $fixedProductTax = $this->getAttributeValue($product, 'aams_nicotina');
            $logger->info(print_r($fixedProductTax,true));
            $data[] = [$product->getSku(), $fixedProductTax];
        }

        $handle = fopen('php://memory', 'w');
        foreach ($data as $line) {
            fputcsv($handle, $line);
        }
        fseek($handle, 0);

        return stream_get_contents($handle);
    }

    protected function getAttributeValue($product, $attributeCode)
    {
        $attribute = $this->attributeRepository->get($attributeCode);

        if (!$attribute) {
            return '';
        }

        $attributeValue = $product->getData($attributeCode);

        // Example of processing if attribute returns an array
        if (is_array($attributeValue) && isset($attributeValue[0]['value'])) {
            return $attributeValue[0]['value'];
        }

        return $attributeValue;
    }
}

And Csv file is :- https://imgur.com/ZBEiDuF