0

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."

1
  • Have you try using $_POST method? Commented Nov 18, 2016 at 6:44

2 Answers 2

1

Custom attribute value can be fetched in observer if it added into event form using

$_POST['atttribute_code']

0
1

Once item (product) is quoted it will save the selected option Data(not all custom option data). so to get the custom option you need to load the product object by product_id which you can get from quote object.

Update: after getting the product id load the product as below.

$product = $objectManager->create('Magento\Catalog\Model\ProductFactory‌​')->create()->load($‌​product_id); 
foreach ($product->getOptions() as $options) {
                $optionData = $options->getValues();
                foreach ($optionData as $data) {
                    $optionPrice[] = $data->getPrice();
                    $optionDuration[] = $data->getTitle();
                }
            }
4
  • May you check the post update
    – Zinat
    Commented Nov 18, 2016 at 6:36
  • did you try to debug if you are really getting product_id??, check my updated answer. Commented Nov 18, 2016 at 6:47
  • The product_id is correct. Then adding your foreach results "We can't add this item to your shopping cart right now."
    – Zinat
    Commented Nov 18, 2016 at 7:11
  • @Zinat since you have asked for custom option of that product, so u must be getting the custom option from the code I have suggested.. We know your logic of adding the item into the cart so can't do mare... Commented Nov 18, 2016 at 11:55

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.