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

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

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.

|improve this answer|||||
  • Hi @Msquare Thanks for your answer, we will check with that and let you know – shivashankar m Mar 10 at 13:11
  • Hi @Msquare is there any possibilty to get cart total on product page – shivashankar m Mar 10 at 13:37
  • @shivashankarm try this link magento.stackexchange.com/questions/121271/… – Msquare Mar 10 at 13:40
  • the link is showing totals on cart page, I would like to get it on product page – shivashankar m Mar 10 at 14:02
  • @shivashankarm I will never do it but I try this and I form you when done. – Msquare Mar 10 at 14:08

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.