Use custom field to calculate shipping fee in Woocommerce - php

I'm building a plugin that allow a user to place an order as a gift for someone else (the beneficiary). The gift is delivered to the address of the beneficiary.
I add new fields after the checkout billing form like this.
add_action('woocommerce_after_checkout_billing_form', 'woocommerce_after_checkout_billing_form');
function woocommerce_after_checkout_billing_form($checkout) {
foreach ($this->get_gift_fields() as $key => $field) {
woocommerce_form_field($key, $field, $checkout->get_value($key));
};
}
function get_gift_fields(){
return array(
'gift_firstname' => array(
'type' => 'text',
'label' => __('Prénom'),
'required' => true,
'class' => array('form-row-first'),
),
'gift_lastname' => array(
'type' => 'text',
'label' => __('Nom'),
'required' => true,
'class' => array('form-row-last'),
),
'gift_country' => array(
'type' => 'country',
'label' => __('Pays'),
'required' => true,
'default' => 'FR',
),
'gift_address_1' => array(
'type' => 'text',
'label' => __('Numéro de voie et nom de la rue'),
'placeholder' => __('Numéro et nom de rue'),
'required' => true,
),
'gift_address_2' => array(
'type' => 'text',
'label' => __('Compléments d\'adresse'),
'placeholder' => __('Appartement, bureau, etc. (optionnel)'),
'label_class' => array('screen-reader-text'),
),
'gift_postcode' => array(
'type' => 'text',
'label' => __('Code postal'),
'required' => true,
),
'gift_city' => array(
'type' => 'text',
'label' => __('Ville'),
'required' => true,
)
);
}
I then have a custom method that calculates shipping using only the country.
I tried the answer from this question, but for exemple the state field appears on the billing form, which make the shipping fees calculation not working.
How can I use my custom fields only, especially the gift_country field, to calculate shipping ?
Thanks!

Related

Can I show two boxes in Magento Admin Grid Mass Action or select a date before action?

I have created a Magento Admin Grid and am using Mass Action to set the start and stop dates.
Now my Mass Action runs as chosen in dropdown list select start or stop datepicker box will show after select first box.
Can I show two boxes at same time (dropdown list and datepicker) or select date before dropdown list?
My code:
$this->getMassactionBlock()->addItem('setstartdate', array(
'label' => $this->__('Set Start Date'),
'url' => $this->getUrl('*/*/massSetstartdate', array('' => '')),
'confirm' => $this->__('Are you sure?'),
'additional' => array(
'setstartdate' => array(
'name' => 'setstartdate',
'time' => true,
'input' => 'datetime',
'type' => 'datetime',
'class' => 'required-entry',
'label' => Mage::helper('setcustomdate')->__('Set start Date'),
// 'gmtoffset' => true,
'image' => '/skin/adminhtml/default/default/images/grid-cal.gif',
'format' => 'yyyy-MM-dd H:mm:ss'
)
)
));
$this->getMassactionBlock()->addItem('setstopdate', array(
'label' => $this->__('Set Stop Date'),
'url' => $this->getUrl('*/*/massSetstopdate', array('' => '')),
'confirm' => $this->__('Are you sure?'),
'additional' => array(
'setstopdate' => array(
'name' => 'setstopdate',
'time' => true,
'input' => 'datetime',
'type' => 'datetime',
'class' => 'required-entry',
'label' => Mage::helper('setcustomdate')->__('Set Stop Date'),
// 'gmtoffset' => true,
'image' => '/skin/adminhtml/default/default/images/grid-cal.gif',
'format' => 'yyyy-MM-dd H:mm:ss'
)
)
));
Edit 1 update prepareColumns
protected function _prepareColumns() {
$helper = Mage::helper('setupdate');
$this->addColumn('data_id', array(
'header' => $helper->__('Data No.'),
'index' => 'data_id'
));
$this->addColumn('startdate', array(
'header'=> $helper->__('Start'),// store
'index' => 'startdate',
'filter_index' => 't1.start_date',
'format' => 'F',
'type' => 'datetime',
));
$this->addColumn('stopdate', array(
'header'=> $helper->__('Stop'),// store
'index' => 'stopdate',
'filter_index' => 't1.stop_date',
'format' => 'F',
'type' => 'datetime',
));
return parent::_prepareColumns();
}

CakePHP input stopped working

I'm completely new with cake, so I have no clue what happened.
this code works :
echo $this->Form->input('phone',
array(
'required' => false,
'div' => 'form-group',
'class' => 'form-control',
'label' => array(
'class' => 'control-label',
'text' => __d('admin', 'Tel. numeris')
),
'placeholder' => __d('admin', 'Tel. numeris'),
'error' => array(
'attributes' => array(
'class' => 'alert alert-danger'
)
)
)
);
this one doesn't :
echo $this->Form->input('email',
array(
'required' => false,
'div' => 'form-group',
'class' => 'form-control',
'label' => array(
'class' => 'control-label',
'text' => __d('admin', 'El.paštas')
),
'placeholder' => __d('admin', 'El.paštas'),
'error' => array(
'attributes' => array(
'class' => 'alert alert-danger'
)
)
)
);
and for example if I change email to, lets say, emailas, it works too(but then it doesnt do anything) :
echo $this->Form->input('emailas',
array(
'required' => false,
'div' => 'form-group',
'class' => 'form-control',
'label' => array(
'class' => 'control-label',
'text' => __d('admin', 'El.paštas')
),
'placeholder' => __d('admin', 'El.paštas'),
'error' => array(
'attributes' => array(
'class' => 'alert alert-danger'
)
)
)
);
could someone please help me or at least tell me where to look? The input stopped working out of the blue, so maybe theres a possibility to somehow restart whole plugin? THANK YOU IN ADVANCE
P.S.
This is how the input field looks atm vs how it should look like : http://imgur.com/bBrkgxM
Try this:
echo $this->Form->input('email',
array(
'required' => false,
'div' => 'form-group',
'class' => 'form-control',
'type' => 'email',
'label' => array(
'class' => 'control-label',
'text' => __d('admin', 'El.paštas')
),
'placeholder' => __d('admin', 'El.paštas'),
'error' => array(
'attributes' => array(
'class' => 'alert alert-danger'
)
)
)
);
And make sure you have email field in database and nowhere is overriding email field (in controller / model)

ZF2's custom validator's $context suddenly gives 'bar'=>array() when posting bar[0][foo]='xyz'

I have a custom validator using $context that has been working for over a year.
The $context contains all posted parameters except for "bar" which is an empty array although it is posted as bar[0][description]. The array used to contain one or more arrays with key-value-pairs.
This seems to be the case since updating from version 2.3.2 to 2.5.1
I can't even check the 2.5 documentation since it's not available, Google didn't turn up anything close to the issue, ... was the behaviour changed or is this a bug?
Edit:
In .../Lorem/Form/FooForm.php I include a fieldset:
public function __construct() {
// ...
$this->add(array(
'type' => 'Collection',
'name' => 'bar',
'options' => array(
'label'=> '',
'count' => 1,
'should_create_template' => TRUE,
'allow_add' => TRUE,
'use_as_base_fieldset' => TRUE,
'target_element' => array(
'type' => 'Lorem\Fieldset\BarFieldset'
)
)
));
}
In .../Lorem/Fieldset/BarFieldset.php I have:
public function __construct() {
parent::__construct('bar');
$this->setHydrator(new ReflectionHydrator())
->setObject(new Bar());
$this->add(array(
'name' => 'description',
'type' => 'Textarea',
'attributes' => array(
'rows' => 3,
'cols' => 40,
),
));
$this->add(array(
'name' => 'amount',
'attributes' => array(
'type' => 'Text',
'class' => 'input-bar-amount',
'maxlength' => 9,
),
));
// ...
}
public function getInputFilterSpecification()
{
return array(
'description' => array(
'required' => TRUE,
'filters' => array(
array('name' => 'StripTags'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'break_chain_on_failure' => TRUE,
'options' => array(
'messages' => array(
'isEmpty' => 'Please enter a description.',
),
),
),
),
),
'amount' => array(
'required' => TRUE,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'break_chain_on_failure' => TRUE,
'options' => array(
'messages' => array(
'isEmpty' => 'Please enter amount.',
),
),
),
array(
'name' => 'Float',
'break_chain_on_failure' => TRUE,
'options' => array(
'locale' => 'de',
'messages' => array(
'floatInvalid' => 'Invalid input. String, Integer oder Float expected.',
'notFloat' => 'Input ist not a floating point number.',
),
),
),
array(
'name' => 'Between',
'break_chain_on_failure' => TRUE,
'options' => array(
'min' => -1000000,
'max' => 1000000,
'messages' => array(
'notBetween' => "Only values between '%min%' and '%max%' EUR are allowed.",
'notBetweenStrict' => "Only values between '%min%' and '%max%' EUR are allowed.",
),
),
),
),
),
// ...
);
}

How to configure ObjectMultiCheckBox to persist an empty element?

I have the following element in my form, and I tried all possible options found in the web to allow empty value for element:
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'directPractice',
'options' => array(
'label' => 'A. Check all direct practice field education assignments',
'label_attributes' => array(
'class' => 'label-multicheckbox-group'
),
'required' => false,
'allow_empty' => true,
'continue_if_empty' => false,
'object_manager' => $this->getObjectManager(),
'target_class' => 'OnlineFieldEvaluation\Entity\FieldEducationAssignments', //'YOUR ENTITY NAMESPACE'
'property' => 'directPractice', //'your db collumn name'
'disable_inarray_validator' => true,
'value_options' => array(
'1' => 'Adults',
'2' => 'Individuals',
'3' => 'Information and Referral',
'4' => 'Families',
array(
'value' => 'Other',
'label' => 'Other (specify)',
'label_attributes' => array(
'class' => 'bindto',
'data-bindit_id' => 'otherDirectPracticeTxt_ID'
),
'attributes' => array(
'id' => 'otherDirectPractice_ID',
),
)
),
),
'attributes' => array(
'value' => '1', //set checked to '1'
'multiple' => true,
)
));
And I am always getting the same error message when it is empty:
Validation failure 'directPractice':Array
(
[isEmpty] => Value is required and can't be empty
)
Ok, this is a best what I could come up after researching on it. Solution is inspired by these question.
Form: hidden field and ObjectMultiCheckBox
public function init()
{
$this->setAttribute('method', 'post');
$this->setAttribute('novalidate', 'novalidate');
//hidden field to return empty value. If checkbox selected, checkbox values will be stored with empty value in Json notation
$this->add(array(
'type' => 'Hidden',
'name' => 'otherLearningExperiences[]', // imitates checkbox name
'attributes' => array(
'value' => null
)
));
$this->add(array(
// 'type' => 'Zend\Form\Element\MultiCheckbox',
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'otherLearningExperiences',
'options' => array(
'label' => 'C. Check other learning experiences',
'object_manager' => $this->getObjectManager(),
'target_class' => 'OnlineFieldEvaluation\Entity\FieldEducationAssignments',
'property' => 'otherLearningExperiences',
'label_attributes' => array(
'id' => 'macro_practice_label',
'class' => 'control-label label-multicheckbox-group'
),
'value_options' => array(
array(
'value' => 'seminars',
'label' => 'Seminars, In-Service Training/Conferences',
'label_attributes' => array(
'class' => 'bindto',
'data-bindit_id' => 'seminarsTxt_ID'
),
'attributes' => array(
'id' => 'seminars_ID',
),
),
array(
'value' => 'other',
'label' => 'Other (specify)',
'label_attributes' => array(
'class' => 'bindto',
'data-bindit_id' => 'otherLeaningExperiencesTxt_ID'
),
'attributes' => array(
'id' => 'otherLeaningExperiences_ID',
),
)
),
),
'attributes' => array(
//'value' => '1', //set checked to '1'
'multiple' => true,
'empty_option' => '',
'required' =>false,
'allow_empty' => true,
'continue_if_empty' => false,
)
));
Validator
$inputFilter->add($factory->createInput(array(
'name' => 'otherLearningExperiences',
'required' => false,
'allow_empty' => true,
'continue_if_empty' => true,
)));
}
View
//hidden field to be posted for empty value in multicheckbox
echo $this->formHidden($form->get('otherLearningExperiences[]'));
$element = $form->get('otherLearningExperiences');
echo $this->formLabel($element);
echo $this->formMultiCheckbox($element, 'prepend');

Magento admin: is it possible to add mass action with datepicker field?

I'm trying to add a mass action to Customer grid to set a customer group (which will simulate subgroups) but the group assignment will have a limited time period.
I'm using Magento ver. 1.4.2.0.
In Customer grid definiton (in my class which extends Mage_Adminhtml_Block_Customer_Grid) I'm adding mass action like this:
/*...*/
$this->getMassactionBlock()->addItem('set_subgroup', array(
'label' => Mage::helper('customersubgroup')->__('Set Customer Subgroup'),
'url' => $this->getUrl('adminhtml/customersubgroup/massSetSubgroup'),
'additional' => array(
'subgroup' => array(
'name' => 'subgroup',
'type' => 'select',
'class' => 'required-entry',
'label' => Mage::helper('customer')->__('Group'),
'values' => $subgroups
),
'valid_from' => array(
'name' => 'valid_from',
'type' => 'date',
'class' => 'required-entry',
'label' => Mage::helper('customersubgroup')->__('Valid From'),
'gmtoffset' => true,
'format' => '%d.%m.%Y'
),
'valid_to' => array(
'name' => 'valid_to',
'type' => 'date',
'class' => 'required-entry',
'label' => Mage::helper('customersubgroup')->__('Valid To'),
'gmtoffset' => true,
'format' => '%d.%m.%Y'
)
)
));
/*...*/
There should be a customer group selectbox and two date fields as additional parameters of this mass action.
The date fields are rendered as text inputs but without a datepicker functionality (no calendar icon). Is it possible to add this functionality somehow?
Thanks in advance.
I should probably shoot myself to the head right now. I've forgotten the image property in item definition:
/*...*/
$this->getMassactionBlock()->addItem('set_subgroup', array(
'label' => Mage::helper('customersubgroup')->__('Set Customer Subgroup'),
'url' => $this->getUrl('adminhtml/customersubgroup/massSetSubgroup'),
'additional' => array(
'subgroup' => array(
'name' => 'subgroup',
'type' => 'select',
'class' => 'required-entry',
'label' => Mage::helper('customer')->__('Group'),
'values' => $subgroups
),
'valid_from' => array(
'name' => 'valid_from',
'type' => 'date',
'class' => 'required-entry',
'label' => Mage::helper('customersubgroup')->__('Valid From'),
'gmtoffset' => true,
'image' => '/skin/adminhtml/default/default/images/grid-cal.gif',
'format' => '%d.%m.%Y'
),
'valid_to' => array(
'name' => 'valid_to',
'type' => 'date',
'class' => 'required-entry',
'label' => Mage::helper('customersubgroup')->__('Valid To'),
'gmtoffset' => true,
'image' => '/skin/adminhtml/default/default/images/grid-cal.gif',
'format' => '%d.%m.%Y'
)
)
));
/*...*/
So YES, it is possible.

Categories