Using below observer the price need to be calculated based on the start and end date in custom options.
<?php
/**
* CustomPrice Observer
*
* @category CustomPricing
* @package CustomPricing_RentPricing
*
*/
namespace CustomPricing\RentPricing\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
class CustomPrice implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer) {
$item=$observer->getEvent()->getData('quote_item');
$product=$observer->getEvent()->getData('product');
$item = ( $item->getParentItem() ? $item->getParentItem() : $item );
if ($product->getData('rent_price')) {
$base_price= $product->getData('rent_price');
$number_of_days=??? how get the value of two custom options (type date)???
$price=$base_price * $number_of_days
} else {
$price = $product->getPrice();
}
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
$item->getProduct()->setIsSuperMode(true);
}
}
Update: using below did not work
$product_id = $product->getId();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customProduct = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);
foreach ( $customProduct ->getOptions() as $o) {
??
}
Update2: Using ObjectManager is problematic here and results to "We can't add this item to your shopping cart right now."
Best Answer
Custom attribute value can be fetched in observer if it added into event form using
$_POST['atttribute_code']