Skip to content

How to show product information in a costume module page like Magento product page built?

I would like to show a product with its details in my module view page. There is summery of my code, it maybe boring but I have exhibit my issue clearly.

1)File name, path and code: MyVendor/MyModule/Api/Data/ViewInterface.php

namespace MyVendorMyModuleApiData;
interface ViewInterface
{
    const VALUE            = 'value ';
    /**
     * Get Value
     *
     * @return string|null
     */
    public function geValue();
    /**
     * Set $Value
     *
     * @param string $Value
     * @return $this
     */
    public function setValue($Value);
}

2)File name, path and code: MyVendor/MyModule/Model/View.php

<?php

namespace MyVendorMyModuleModel;

use MagentoFrameworkModelAbstractModel;
use MagentoFrameworkDataObjectIdentityInterface;
use MyVendorMyModuleApiDataViewInterface;

/**
 * Class File
 * @package MyVendorMyModuleModel
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class View extends AbstractModel implements ViewInterface, IdentityInterface
{
    /**
     * Cache tag
     */
    const CACHE_TAG = 'MyVendor_MyModule_view';

    /**
     * Post Initialization
     * @return void
     */
    protected function _construct()
    {
        $this->_init('MyVendorMyModuleModelResourceModelView');
    }
    /**
     * Get Value
     *
     * @return string|null
     */
    public function geValue()
    {
        return $this->getData(self::VALUE);
    }
    /**
     * Return identities
     * @return string[]
     */
    public function getIdentities()
    {
        return [self::CACHE_TAG . '_' . $this->geValue()];
    }
    /**
     * Set $Value
     *
     * @param string $Value
     * @return $this
     */
    public function setValue($Value)
    {
        return $this->setData(self::VALUE, $Value);
    }
}

3)File name, path and code: MyVendor/MyModule/ResourceModel/View.php(Maybe here would be modify):

namespace MyVonderMyModelModelResourceModel;

use MagentoFrameworkModelResourceModelDbAbstractDb;

class View extends AbstractDb
{
    /**
     * Post Abstract Resource Constructor
     * @return void
     */
    protected function _construct()
    {
        $this->_init('catalog_product_entity_media_gallery', 'value_id');
    }
}

In magento DB there are many tables that I do not know how to use or join them to fetch data. I only applied “catalog_product_entity_media_gallery” which had value of image or product.

4)File name, path and code: MyVendor/MyModule/view/frontend/templates/View.phtml

<?php /** @var MyVendorMyModelBlockProduct $block */ ?>
<?php $productCollection = $block->getCollection(); ?>
<?php
$pic_number=time()%(count($productCollection));// for selecting a random product
echo $pic_number;
dd($productCollection[0]['value']);// test the value data of table
 ?>

5) File name, path and code: MyVendor/MyModule/Block/Product.php

class Product extends Template
    {
        
        protected $_viewCollectionFactory = null;
    
        /**
         * Constructor
         *
         * @param Context $context
         * @param ViewCollectionFactory $viewCollectionFactory
         * @param array $data
         */
        public function __construct(
            Context $context,
            ViewCollectionFactory $viewCollectionFactory,
            array $data = []
        ) {
            $this->_viewCollectionFactory  = $viewCollectionFactory;
            parent::__construct($context, $data);
        }
    
        /**
         * @return Post[]
         */
        public function getCollection()
        {
            /** @var ViewCollection $viewCollection */
            $viewCollection = $this->_viewCollectionFactory ->create();
            $viewCollection->addFieldToSelect('value')->load();
            return $viewCollection->getData();
        }
    
    }

Overall: I could not fetch the Qty, Details, Name and other extra information of product. While all of them are in default page of product in Magento home page.