Add dynamic custom fee magento 2 - magento2

I am building a custom fee module for magento 2. I add a checkbox to the checkout page, if user ticks that checkbox, I will add a custom fee to the order. I successfully added the custom fee to order following this tutorial:
https://magento.stackexchange.com/questions/92774/how-to-add-fee-to-order-totals-in-magento-2
However, my custom fee is dynamic since it is fetched from external API. Let's take a look at:
app\code\Sugarcode\Test\Model\Total\Fee.php
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Sugarcode\Test\Model\Total;
class Fee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{
/**
* Collect grand total address amount
*
* #param \Magento\Quote\Model\Quote $quote
* #param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
* #param \Magento\Quote\Model\Quote\Address\Total $total
* #return $this
*/
protected $quoteValidator = null;
public function __construct(\Magento\Quote\Model\QuoteValidator $quoteValidator)
{
$this->quoteValidator = $quoteValidator;
}
public function collect(
\Magento\Quote\Model\Quote $quote,
\Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
\Magento\Quote\Model\Quote\Address\Total $total
) {
parent::collect($quote, $shippingAssignment, $total);
$exist_amount = 0; //$quote->getFee();
// $fee = 100; //Excellence_Fee_Model_Fee::getFee();
// TAKE A LOOK AT THIS:
$fee = getFeeFromExternalAPI()
// I successfully get the FEE but how to update the value of checkbox in the UI???
$balance = $fee - $exist_amount;
$total->setTotalAmount('fee', $balance);
$total->setBaseTotalAmount('fee', $balance);
$total->setFee($balance);
$total->setBaseFee($balance);
$total->setGrandTotal($total->getGrandTotal() + $balance);
$total->setBaseGrandTotal($total->getBaseGrandTotal() + $balance);
return $this;
}
protected function clearValues(Address\Total $total)
{
$total->setTotalAmount('subtotal', 0);
$total->setBaseTotalAmount('subtotal', 0);
$total->setTotalAmount('tax', 0);
$total->setBaseTotalAmount('tax', 0);
$total->setTotalAmount('discount_tax_compensation', 0);
$total->setBaseTotalAmount('discount_tax_compensation', 0);
$total->setTotalAmount('shipping_discount_tax_compensation', 0);
$total->setBaseTotalAmount('shipping_discount_tax_compensation', 0);
$total->setSubtotalInclTax(0);
$total->setBaseSubtotalInclTax(0);
}
/**
* #param \Magento\Quote\Model\Quote $quote
* #param Address\Total $total
* #return array|null
*/
/**
* Assign subtotal amount and label to address object
*
* #param \Magento\Quote\Model\Quote $quote
* #param Address\Total $total
* #return array
* #SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
{
return [
'code' => 'fee',
'title' => 'Fee',
'value' => 100
];
}
/**
* Get Subtotal label
*
* #return \Magento\Framework\Phrase
*/
public function getLabel()
{
return __('Fee');
}
Please take a look at this function getFeeFromExternalAPI. Instead of using static fee (100$), I get the fee from external API. I got the fee BUT how to update the checkbox value in the UI (HTML). How can I notify the UI to change?
I am thinking of firing an event after I get the response successfully. And then I observe that event. However, the observer is PHP file. How can I update UI from PHP file

Related

TYPO3 typoscript set includeCSS trough userFunc

I have a site that has a dark theme and light theme set in function of the daylight ...
in my constants i define sunset and sunrise and I have a Class that I reach through userFunc, my class:
<?php
namespace Vendor\Extension\Utility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
/**
* will return theme class in various instances of the page
*/
class ThemeClass extends ContentObjectRenderer
{
/**
* #var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
*/
public $cObj;
/**
* Returns theme class according to daylight
*
* #var string $content
* #var array $config
* #return string
**/
protected function class($content, array $config = null)
{
// init
$sunRise = $this->cObj->stdWrapValue('sunRise', $config, null);
$sunSet = $this->cObj->stdWrapValue('sunSet', $config, null);
$format = 'h:i a';
$now = date_create_from_format($format, Date($format));
$open = date_create_from_format($format, $sunRise);
$close = date_create_from_format($format, $sunSet);
return $theme = ($now > $open && $now < $close) ? 'light' : 'dark' ;
}
/**
* Returns body tag according to theme
*
* #var string $content
* #var array $config
* #return string
**/
public function bodyTag($content, array $config = null)
{
$theme = $this->class($content, $config);
return '<body class="'.$theme.'">';
}
/**
* Returns css according to theme
*
* #var string $content
* #var array $config
* #return string
**/
public function themeCss($content, array $config = null)
{
$theme = $this->class($content, $config);
return 'EXT:extension/Resources/Public/Css/_'.$theme.'.css">';
}
}
I use the theme overrideing the bodytag like this:
page.bodyTag >
page.bodyTagCObject = USER
page.bodyTagCObject {
userFunc = Vendor\Extension\Utility\ThemeClass->bodyTag
sunRise = {$extension.config.daylight.sunrise}
sunSet = {$extension.config.daylight.sunset}
}
I expected that something similar would work here :
page.includeCSS {
theme.cObject = USER
theme.cObject {
userFunc = Vendor\Extension\Utility\ThemeClass->themeCss
sunRise = {$extension.config.daylight.sunrise}
sunSet = {$extension.config.daylight.sunset}
}
}
but it doesn't work within includeCSS ... I tried a few 'side-steps' of this but cannot get anything to work ...
You could register a USER or USER_INT somewhere in the page object and in you user function get the AssetCollector to register CSS.

Issue with the ParamConverter annotation

I am working on a mailbox between two users for my application. I made the controller, the form and the view, everything is working except when I add the entity Ad in the MessageController to return informations about the ad, for the view.
The error message is :
App\Entity\Ad object not found by the #ParamConverter annotation.
I did exactly the same thing when I managed the booking of an ad, it all worked perfectly fine, I really don't get what's wrong with the rest.
For example, my BookingController which is working :
/**
* #Route("/ads/{id}/booking", name="ad_booking")
* #IsGranted("ROLE_USER")
*
* #return Response
*/
public function booking(Ad $ad, Request $request, ObjectManager $manager)
{
$booking = new Booking;
$form = $this->createForm(BookingType::class, $booking);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$user = $this->getUser();
$booking->setBooker($user)
->setAd($ad)
;
if(!$booking->isAvailableDate())
{
$this->addFlash(
'warning',
'Attention, les dates que vous avez choisies ne sont pas disponibles, elles ont déjà été réservées.'
);
}
else
{
$manager->persist($booking);
$manager->flush();
$this->addFlash(
'success',
"Votre réservation a bien été effectuée !"
);
return $this->redirectToRoute('booking_show', [
'id' => $booking->getId(),
'withAlert' => true
]);
}
}
return $this->render('booking/booking.html.twig', [
'ad' => $ad,
'bookingForm' => $form->createView()
]);
}
My MessageController that doesn't work :
namespace App\Controller;
use App\Entity\Ad;
use App\Entity\Message;
use App\Form\MessageType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MessageController extends AbstractController
{
/**
* #Route("/ads/{id}/message", name="message_provider")
* #IsGranted("ROLE_USER")
*
* #return Response
*/
public function message(Ad $ad, Request $request, ObjectManager $manager)
{
$message = New Message();
$form = $this->createForm(MessageType::class, $message);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$ad = $this->getAd();
$user = $this->getUser();
$message->setSender($user)
->setAd($ad)
;
$manager->persist($message);
$manager->flush();
$this->addFlash(
'success',
"Votre message n°{$message->getId()} a bien été envoyé."
);
return $this->redirectToRoute('message_show', [
'id' => $message->getId()
]);
}
return $this->render('message/new.html.twig', [
'form' => $form->createView()
]);
}
/**
* #Route("/message/{id}", name="message_show")
*
* #return Response
*/
public function showmessage(Message $message)
{
return $this->render('message/show.html.twig', [
'message' => $message,
]);
}
}
and my Message entity
namespace App\Entity;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\MessageRepository")
*/
class Message
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="text")
*/
private $content;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="messages")
* #ORM\JoinColumn(nullable=false)
*/
private $receiver;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="sentMessages")
* #ORM\JoinColumn(nullable=false)
*/
private $sender;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Ad", inversedBy="messages")
* #ORM\JoinColumn(nullable=false)
*/
private $ad;
/**
* #ORM\Column(type="datetime")
*/
private $createdAt;
I still got the App\Entity\Ad object not found by the #ParamConverter annotation. error message, even when I tried to define the #ParamConverter in the annotation. I can't see what's wrong.
Not sure if it's related but it seems to me, you never use the object $ad created from {id} in your function message. I think you use the id of your Ad or an object Ad on the form $this->getAd() and you never use the one on the route {id}. Maybe it messed up, try to clarify what is $ad on this function (from the route {id} or from the form).
If I had to bet, I would say you don't need this line:$ad = $this->getAd(); because you don't identify you current used $ad on the form but on the route.
(It should be a comment but I can't comment yet)

How can i access a temporarily argument (password-reenter) in a Validator with PHP in TYPO3 without make an extra sql-field?

TYPO3-Version: 8.7.7
I want to access $this->request->getArguments() in a Validator for TYPO3 in PHP.
I set a temporary field in fluid with:
<label for="reenter_password" class="reenter_password{rm:hasError(property:'reenter_password',then:' text-danger')}">
Reenter Password*
</label><br>
<f:form.password name="reenter_password" id="reenter_password"/>
If i set property instead of name in <f:form.password name="reenter_password" id="reenter_password"/> i get the following Error:
#1297759968: Exception while property mapping at property path "": Property "reenter_password" was not found in target object of type "RM\RmRegistration\Domain\Model\User".
I don't want to set a Model-Property, because this property should only use for checking with the passwortfield for equality and shouldn't get a TCA or SQL-Table for Storage.
Here is my Action, where i call the Validators:
/**
* Complete New User Registeration
*
* #param User $newRegisteredUser
* #validate $newRegisteredUser \RM\RmRegistration\Validation\Validator\NewRegisteredUser
* #validate $newRegisteredUser \RM\RmRegistration\Validation\Validator\CompleteProfileUser
*/
public function completeNewRegisteredUserAction(User $newRegisteredUser)
{
// Store Changes, Update and Persist
$newRegisteredUser->setPassword($this->saltThisPassword($newRegisteredUser->getPassword()));
$this->userRepository->update($newRegisteredUser);
$this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\PersistenceManager')->persistAll();
}
In the Validator i can get to the Password-Field with:
\TYPO3\CMS\Core\Utility\GeneralUtility::_POST()['tx_rmregistration_registration']['reenter_password']
But is it possible to get the Value temporary to the UserModel to check it in the Validator like this:
// Compare Entered Passwords
if ($user->getPassword() == $user->getReenteredPassword()) {
return TRUE;
} else {
return FALSE;
}
Make an own (view) model for the password verification process or give the $newRegisteredUser a (transient or not) property for the reenterd password. Then use an own validator (see here).
class UserValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator {
/**
* Object Manager
*
* #var \TYPO3\CMS\Extbase\Object\ObjectManager
* #inject
*/
protected $objectManager;
/**
* Is valid
*
* #param mixed $user
* #return boolean
*/
public function isValid($user) {
if ($user->getPassword() !== $user->getPasswordConfirmation()) {
$error = $this->objectManager->get(
'TYPO3\CMS\Extbase\Validation\Error',
'Password and password confirmation do not match.', time());
$this->result->forProperty('passwordConfirmation')->addError($error);
return FALSE;
}
return TRUE;
}
}
And use in controller like this:
/**
* action create
*
* #param \Vendor\Ext\Domain\Model\User $user
* #validate $user \Vendor\Ext\Domain\Validator\UserValidator
*
* #return void
*/
public function createAction(\Vendor\Ext\Domain\Model\User $user) {
}

Eloquent query build of laravel cashier, why was it done this way?

i was looking at the source code of laravel cashier and saw this:
/**
* Get a subscription instance by name.
*
* #param string $subscription
* #return \Laravel\Cashier\Subscription|null
*/
public function subscription($subscription = 'default')
{
return $this->subscriptions->sortByDesc(function ($value) {
return $value->created_at->getTimestamp();
})
->first(function ($value) use ($subscription) {
return $value->name === $subscription;
});
}
why the 2 anonymous functions? why not just (bit naive):
$this->subscriptions->where('name', $value)

get settings in validator - typo3

I have an extension with backend configuration options.I need to validate a phone number in AddAction and UpdateAction.I can configure the phone number format in backend (say us phone number/indian phone number etc).How can i get the settings in validator?
I have a custom validator to validate phone numbers.Here is my code
<?php
namespace vendor\Validation\Validator;
class UsphonenumberValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
{
protected $supportedOptions = array(
'pattern' => '/^([\(]{1}[0-9]{3}[\)]{1}[ ]{1}[0-9]{3}[\-]{1}[0-9]{4})$/'
);
public function isValid($property) {
$settings = $this->settings['phone'];
$pattern = $this->supportedOptions['pattern'];
$match = preg_match($pattern, $property);
if ($match >= 1) {
return TRUE;
} else {
$this->addError('Phone number you are entered is not valid.', 1451318887);
return FALSE;
}
}
}
$settings returns null
In cases where the extbase configuration of your extension isn't implemented by default you should retrieve it yourself by using the \TYPO3\CMS\Extbase\Configuration\ConfigurationManager.
Here is an example how you can obtain the settings of your extension:
<?php
namespace MyVendor\MyExtName\Something;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Object\ObjectManager;
class Something {
/**
* #var string
*/
static protected $extensionName = 'MyExtName';
/**
* #var null|array
*/
protected $settings = NULL;
/**
* Gets the Settings
*
* #return array
*/
public function getSettings() {
if (is_null($this->settings)) {
$this->settings = [];
/* #var $objectManager \TYPO3\CMS\Extbase\Object\ObjectManager */
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
/* #var $configurationManager \TYPO3\CMS\Extbase\Configuration\ConfigurationManager */
$configurationManager = $objectManager->get(ConfigurationManager::class);
$this->settings = $configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
self::$extensionName
);
}
return $this->settings;
}
}
I recommend that you implement such functionality in general. So you could retrieve any configuration of any extension as a Service inside your extension or something similar to this.
Good luck!