0

Hi i am trying to create a page which will search a product and show the product there. Everything is working fine but i can't show the product additional information tab in the product.

Here is my block code:

 <?php

namespace Custom\Module\Block\Product;

use Magento\Catalog\Model\Product;

use Magento\Catalog\Model\Category;

class Create extends \Magento\Framework\View\Element\Template
{
    /**
     * @var \Magento\Catalog\Model\Product
     */
    protected $_product;

    /**
     * @var \Magento\Catalog\Model\Category
     */
    protected $_category;

    /**
     * @var \Magento\Cms\Model\Wysiwyg\Config
     */
    protected $_wysiwygConfig;

    /**
    * @param \Magento\Catalog\Block\Product\Context $context
    * @param \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig
    * @param Product $product
    * @param Category $category
    * @param array $data
    */
    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig,
        Product $product,
        Category $category,
        array $data = []
    ) 
    {
        $this->_wysiwygConfig = $wysiwygConfig;
        $this->_product = $product;
        $this->_category = $category;
        parent::__construct($context, $data);
    }

    public function getWysiwygConfig()
    {
        $config = $this->_wysiwygConfig->getConfig();
        $config = json_encode($config->getData());
    }

    public function getProduct($id)
    {
        return $this->_product->load($id);
    }

    public function getCategory()
    {
        return $this->_category;
    }

    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
}

Here is template code:

<?php

$helper = $this->helper('Webkul\Marketplace\Helper\Data');

/** @var $this \Magento\Framework\View\Element\Template */
/** @var $helper \Magento\Search\Helper\Data */
$helper_search = $this->helper('Magento\Search\Helper\Data');
 $product_id=$this->getRequest()->getParam('pid');
$product_coll = $block->getProduct($product_id);
$pname = $product_coll->getname();
$desc = $product_coll->getDescription();
$pSku = $product_coll->getsku(); 

$attributes = $product_coll->getAttributes();

foreach ($attributes as $attribute) { 
    echo $attribute->getAttributeCode(); echo '<br />';
    echo $attribute->getStoreLabel(); echo '<br />';
    echo $attribute->getFrontendLabel(); echo '<br />';    
    echo $attribute->getFrontend()->getLabel(); echo '<br />';
    echo $attribute->getFrontend()->getValue($product); echo '<br />';    
}
?>

Above code shows the product name, description and sku but it's now showing product additional data.

1 Answer 1

0

After searched lot over the internet i decided to show product additional information using the product attributes. Get all the attributes of product using getAttributes() method. Then filter the attribute using the group id for group product details. After that avoid the attributes which are not necessary like sku, price, image etc. Following is code may be it is helpful for some one.

Block code:

<?php

namespace Vendor\Module\Block\Product;

use Magento\Catalog\Model\Product;

use Magento\Catalog\Model\Category;

use Magento\Framework\ObjectManagerInterface;

class Create extends \Magento\Framework\View\Element\Template
{
    /**
     * @var \Magento\Catalog\Model\Product
     */
    protected $_product;

    /**
     * @var \Magento\Catalog\Model\Category
     */
    protected $_category;

    /**
     * @var \Magento\Cms\Model\Wysiwyg\Config
     */
    protected $_wysiwygConfig;
    protected $customerSession;
    public $_objectManager;
    /**
    * @param \Magento\Catalog\Block\Product\Context $context
    * @param \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig
    * @param Product $product
    * @param Category $category
    * @param array $data
    */
    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Cms\Model\Wysiwyg\Config $wysiwygConfig,
        \Magento\Customer\Model\Session $customerSession,
        Product $product,
        Category $category,
        ObjectManagerInterface $objectManager,
        array $data = []
    ) 
    {
        $this->_wysiwygConfig = $wysiwygConfig;
        $this->_product = $product;
        $this->_objectManager = $objectManager;
        $this->customerSession = $customerSession;
        $this->_category = $category;
        parent::__construct($context, $data);
    }

    public function getWysiwygConfig()
    {
        $config = $this->_wysiwygConfig->getConfig();
        $config = json_encode($config->getData());
    }

    public function getProduct($id)
    {
        return $this->_product->load($id);
    }

     public function getSellerId()
     {      

         return $this->customerSession->getCustomer()->getId();

     }

    public function getSellerProduct($mageProductId, $sellerId)
    {

        $sellerProductColls = $this->_objectManager->create(
                'Vendor\Module\Model\Product'
            )
            ->getCollection()
            ->addFieldToFilter(
                'mageproduct_id',
                $mageProductId
            )->addFieldToFilter(
                'seller_id',
                $sellerId
            );

            $count = $sellerProductColls->getData();
            return $count;
    }

    public function getAttributeGroupId($attributeSetId)
    {
         $obj = \Magento\Framework\App\ObjectManager::getInstance();

        /** @var \Magento\Catalog\Model\Config $config */
        $config= $obj->get('Magento\Catalog\Model\Config');

        return $attributeGroupId = $config->getAttributeGroupId($attributeSetId, 'Product Details');

    }


    public function getProductAttrValue($pid, $code)
    {
        $product = $this->_product->load($pid);
        return $product->getResource()->getAttribute($code)->getFrontend()->getValue($product);
    }



    public function getCategory()
    {
        return $this->_category;
    }

    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
}

Phml Template:

 <?php

   $product_id=$this->getRequest()->getParam('pid');
   $product_coll = $block->getProduct($product_id);
   $pname = $product_coll->getname();
   $pattr = $product_coll->getAttributeSetId();
   $extraAttr = array('swatch_image', 'old_id', 'name', 'url_path', 'required_options', 'has_options', 'image_label', 'swatch_image', 'small_image_label', 'thumbnail_label', 'created_at', 'updated_at', 'sku', 'price', 'tax_class_id', 'image', 'quantity_and_stock_status', 'weight', 'updated_at', 'category_ids', 'description', 'status', 'price_type', 'sku_type', 'sw_featured', 'weight_type', 'shipment_type', 'links_purchased_separately', 'samples_title', 'links_title', 'links_exist', 'new', 'sale');

   $group_id = $block->getAttributeGroupId($pattr);
   $productAttributes = $product_coll->getAttributes();

   foreach ($productAttributes as $attribute):
     $code = $attribute->getAttributeCode();

      if ($attribute->isInGroup($pattr, $group_id) && !in_array($code,  $extraAttr)):
         echo '<strong>'.$attribute->getDefaultFrontendLabel().': </strong>&nbsp;&nbsp;&nbsp;'.$block->getProductAttrValue($product_id,   $code).'<br>';

     endif;

  endforeach;

    ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.