0

I try to set special prices for some customers / products. The new prices are calculated in a module. But how can I display these prices in list and view mode?

In 1.9. I could use an Observer. Tried the same in 2.1., but without success:

event name="prepare_catalog_product_collection_prices"

Observer:

use Magento\Framework\Event\ObserverInterface;
use Magento\Catalog\Model\Product;
use Magento\Customer\Model\Session as CustomerModelSession;
use Magento\Framework\Event\Observer as EventObserver;

class Discount implements ObserverInterface
{
    /**
     * @var CustomerModelSession
     */
    protected $customerSession;



    public function __construct(        
        CustomerModelSession $customerSession
    ) {
        $this->customerSession = $customerSession;
    }




   public function execute(\Magento\Framework\Event\Observer $observer)
    {
        if ($this->customerSession->isLoggedIn()) {
            $collection = $observer->getEvent()->getCollection();
            $cId = $this->customerSession->getCustomerId();

            foreach ($collection as $product) {
                $finalPrice = min($product->getData('final_price'), $this->getmyPrice( $product,$cId));
$_product->setMinimalPrice($finalPrice)
                  ->setPrice($finalPrice)
                  ->setFinalPrice($finalPrice);             
                }

            return $this;
        }


        return $this;
    }
    public function getmyPrice($product,$cid)
    {

        $price=2;

        return price;

    }
}

Any help / hint very welcome!!

1

I ended up using the plugin

afterGetPrice(\Magento\Catalog\Model\Product $subject, $result)

to display the Customer price on list / view page and will have implement something else to adjust the Customer price in checkout (the customer price shows up in the cart, but not in the checkout...)

Here is the code:

<?php
/**
 * @author      Schasiepen
 * @category    Prommerce
 * @package     Prommerce_Resellerdiscount
 */

namespace Prommerce\Resellerdiscount\Plugin;



class Tierprice
{


    public $customerSession;
    protected $customerRepository;
    public $kunde;


    public function __construct(
  \Magento\Customer\Model\Session $customerSession, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
) {
   $this->customerSession = $customerSession;
   $this->customerRepository = $customerRepository;
   $this->kunde =$this->customerSession->isLoggedIn();
   $this->cid = $this->customerSession->getCustomerId();

}


    public function afterGetPrice(\Magento\Catalog\Model\Product $subject, $result)
    {


        $id=$subject->getId();
        $sku=$subject->getSku();
        $brand_id=substr($sku, 0, 2);
        $result=$this->calculate($result,$subject,$id,$brand_id);
                return $result;
    } 



    public function calculate($price,$subject,$id,$brand_id)
    {

        if ($this->customerSession->isLoggedIn()) { 
            $this->kunde = true;

            ...do some calulation here based on $this->cid ....

                return $price;  


        }
        else {

            //echo "<pre> kein kunde</pre>";
            return $price;

        }

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.