I am implementing a shipping method for my store. My method is based on the subtotal value so I need to get subtotal value in my module. How can I call subtotal value in magento 2.2?
I find the answer by myself, here is how we can call subtotal and use it everywhere we need:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');
$grandTotal = $cart->getQuote()->getGrandTotal();
Related
I am having trouble to dynamically modify field in my form using DataMapper and EventSubscriber.
Here is my form:
A select field
A field which will be modified by the select field above.
I am using an EventSubscriber to dynamically modify my form using AJAX.
And a DataMapper to map my Value Object to the form and vice-versa.
So when i do that:
$moneyForm = $this->createForm(MoneyType::class);
Everything is working. But when i pass my Value Object as data class:
$money = new Money(199, 'USD');
$moneyForm = $this->createForm(MoneyType::class, $money);
I got an error here:
public function mapDataToForms($viewData, $forms)
{
$forms = iterator_to_array($forms);
$forms['money']->setData($viewData ? $viewData->money() : 0);
$forms['currency']->setData($viewData ? $viewData->currency() : 'USD');
}
This error says that: Notice: Undefined index:.
It seems like the form has been replaced by a new one and i don't understand it.
I don't know why when i use a data mapper alone, or event subscriber alone everything is working.
But when i try to mix, both of them i got this error.
Does anyone have a clue of what's going on here?
Thank you
So, I'm trying to understand Symfony forms. I'm searching the core code for "allow_delete" option to see how it works under the hood, but the only place where it can be found is in the CollectionType class and I cannot find any logic there.
Documentation states:
If set to true, then if an existing item is not contained in the
submitted data, it will be correctly absent from the final array of
items.
Where in the code exactly it influences the submitted data?
You can find the function in MergeCollectionListener.php beginning on line 91:
// Remove deleted items before adding to free keys that are to be
// replaced
if ($this->allowDelete) {
foreach ($itemsToDelete as $key) {
unset($dataToMergeInto[$key]);
}
}
$dataToMergeInto is set as $dataToMergeInto = $event->getForm()->getNormData();
which refers to a function in which is explained in FormInterface.php:
/**
* Returns the normalized data of the field.
*
* #return mixed When the field is not submitted, the default data is returned.
* When the field is submitted, the normalized submitted data is
* returned if the field is valid, null otherwise.
*/
public function getNormData();
I have a problem using orWhere in eloquent.
I have a team, and this team has som profiles. I want to get all the profiles which have the status = 1 or status = 2. But I cant get it to work.
My code is like this:
$profile = $this->team->profiles->where('status', 1)->find($id);
Thanks:-)
Instead of calling where() on the result of the relationship (which I assume is a collection) you should call it on the relationship object itself, which you can get by calling the function that defines this relation:
$profile = $this->team->profiles()->whereIn('status', [1,2])->find($id)
I also suggest to use whereIn instead of two wheres...
You can use, the Where In function with An Array
$profile = $this->team->profiles->whereIn('status', [1, 2])->get();
I simply need to be able to have a text field and validate if input is a decimal number like 100.00, 0.1, 10.6, etc.
I'm using Drupal 7.
Updated question to indicate 'programatic' solution (using Form API).
You could use #element_validate option for your field and write your own validation function. Maybe you could use is_float() or is_number() in your validation function.
So your code
$form['yourField'] = array(
....
'#element_validate'=>'myValidation'
);
....
function myValidation($element, &$form_state, $form){
if (!is_float( $element['#value']) )){
form_error($element, t('This field is not decimal.'));
}
}
I have problem with a setter in grails. I have two properties beforeTax and afterTax. I only want to store one property in the db beforeTax. In the ui I want the user to enter either before or after tax. So I made the afterTax a transient property like this:
double getAfterTax(){
return beforeTax * tax
}
void setAfterTax(double value){
beforeTax = value / tax
}
When I now enter the after tax value and want to save the object the validation fails (before tax can not be an empty value)
What am I doing wrong?
If I understand your question correctly, you want to compute beforeTax based on the value of afterTax?
You could use the event handler methods beforeXXX() where XXX is Validate, Insert, and Update to compute beforeTax. Then beforeTax's constraint can be nullable:false.
def beforeValidate() {
computeBeforeTax()
}
You have to flag one property as transient, in order to prevent GORM from trying to save this variable into DB. Try to add this line into your domain class, which contains afterTax.
static transients = ['afterTax']