0

I have added a custom link in My Account page, In that link I have added custom collection. I Would like to get that custom Collection of specified customer in Product view page.

Help me great.

Thanks

6
  • 1
    Please describe more. Custom data or customer data ??
    – Msquare
    Mar 10, 2020 at 8:10
  • Please find the updated question. Mar 10, 2020 at 8:13
  • it is custom data of customer. Mar 10, 2020 at 8:14
  • 1
    Your custom data store in customer table or your custom table ??
    – Msquare
    Mar 10, 2020 at 8:15
  • 1
    have you store customer ID in your custom table ?
    – Msquare
    Mar 10, 2020 at 8:19

1 Answer 1

3

create your custom table Model files for get your custom collection.

VendoreName\ModuleName\Model

CustomData.php

<?php

namespace VendoreName\ModuleName\Model;

use Magento\Framework\Model\AbstractModel;
use VendoreName\ModuleName\Model\ResourceModel\CustomData as CustomDataResourceModel;

class CustomData extends AbstractModel
{
    protected function _construct()
    {
        $this->_init(CustomDataResourceModel::class);
    }
}

VendoreName\ModuleName\Model\ResourceModel

CustomData.php

<?php

namespace VendoreName\ModuleName\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class CustomData extends AbstractDb
{
    protected function _construct()
    {
        $this->_init('your_table_name', 'your_primary_key_column_name');
    }
}

VendoreName\ModuleName\Model\ResourceModel\CustomData

Collection.php

<?php

namespace VendoreName\ModuleName\Model\ResourceModel\CustomData;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use VendoreName\ModuleName\Model\CustomData as CustomDataModel;
use VendoreName\ModuleName\Model\ResourceModel\CustomData as CustomDataResourceModel;

class Collection extends AbstractCollection
{
    protected function _construct()
    {
        $this->_init(
            CustomDataModel::class,
            CustomDataResourceModel::class
        );
    }
}

Now ready to get custom collection

Add to your block file

protected $customCollection;

public function __construct(
    ..............................................
    ..............................................
    \Magento\Customer\Model\SessionFactory $customerSession,
    \VendoreName\ModuleName\Model\ResourceModel\CustomData\CollectionFactory $customCollection,
    ..............................................
    ..............................................
) {
   ..............................................
   .............................................. 
    $this->_customerSession = $customerSession->create();
    $this->customCollection = $customCollection;
    ..............................................
    ..............................................
}

public function getCustomDataCollection()
{
    //get current customer id
    $cust_id = $this->getCustomerId();

    // filter custom data with customer id
    $Mycollection = $this->customCollection ->create();
    $Mycollection ->addFieldToFilter('customer_id', $cust_id);
    return $Mycollection ;
}
public function getCustomerId()
{
    return $this->_customerSession->getId();
}

Note:- Here replace customer_id with your store column name which store customer id.

you can call this in your phtml file

$block->getCustomDataCollection();

Run Magento command

php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento c:f
php bin/magento c:c

If your block not get customer ID add cacheable='false'. In your layout file.

I Hope This Helps You.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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