/** * Determine current product and initialize its recurring payment model * * @return \Magento\RecurringPayment\Block\Catalog\Product\View\Payment */ protected function _prepareLayout() { $product = $this->_registry->registry('current_product'); if ($product) { $this->_payment = $this->_paymentFactory->create()->importProduct($product); } return parent::_prepareLayout(); }
/** * Collect buy request and set it as custom option * * Also sets the collected information and schedule as informational static options * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function prepareProductRecurringPaymentOptions($observer) { $product = $observer->getEvent()->getProduct(); $buyRequest = $observer->getEvent()->getBuyRequest(); if (!$product->getIsRecurring()) { return; } /** @var \Magento\RecurringPayment\Model\RecurringPayment $payment */ $payment = $this->_recurringPaymentFactory->create(array('locale' => $this->_locale)); $payment->setStore($this->_storeManager->getStore())->importBuyRequest($buyRequest)->importProduct($product); if (!$payment) { return; } // add the start datetime as product custom option $product->addCustomOption(\Magento\RecurringPayment\Model\RecurringPayment::PRODUCT_OPTIONS_KEY, serialize(array('start_datetime' => $payment->getStartDatetime()))); // duplicate as 'additional_options' to render with the product statically $infoOptions = array(array('label' => $this->_fields->getFieldLabel('start_datetime'), 'value' => $payment->exportStartDatetime())); foreach ($payment->exportScheduleInfo() as $info) { $infoOptions[] = array('label' => $info->getTitle(), 'value' => $info->getSchedule()); } $product->addCustomOption('additional_options', serialize($infoOptions)); }