I am creating a custom form for my extension. In the form I have few fields that I save in database. My problem is that once I save the fields they get saved in DB but after the page refresh the data in the fields gets empty.
I want these fields data to remain in the fields. How do I do that? Is there a way to do that.
Form code is in this file app\code\community\CompanyNmae\ModuleName\Block\Adminhtml\Suretaxconfig\Edit\Form.php
and save config method is in this file
app\code\community\CompanyNmae\ModuleName\controllers\Adminhtml\ModuleController.php
Code for Form.php is here. There no HTML or PHTML used in the project for my extension.
<?php
class CompanyName_Modulename_Block_Adminhtml_ModuleConfig_Edit_Form extends Mage_Adminhtml_Block_Widget_Form{
protected function _prepareForm() {
$form = new Varien_Data_Form (
array('id'=>'edit_form', 'action'=>$this->getData('action'), 'method'=>'post')
);
$fieldset = $form->addFieldset('base_fieldset', array('legend'=>Mage::helper('modulename')->__('ModuleName Settings'),
'class'=>'fieldset-wide'));
$fieldset->addField('companycode', 'text', array(
'name' => 'companycode',
'label' => Mage::helper('modulename')->__('Client Number'),
'title' => Mage::helper('modulename')->__('Client Number'),
'required' => true,
'after_element_html' => '<small>Enter Client Number</small>'
));
$fieldset->addField('validationkey', 'text', array(
'name' => 'validationkey',
'label' => Mage::helper('modulename')->__('Validation Key'),
'title' => Mage::helper('modulename')->__('Validation Key'),
'required' => true,
'after_element_html' => '<small>Enter Company Validation Key </small>'
));
$fieldset->addField('select_provider_type', 'select', array(
'label' => Mage::helper('modulename')->__('Default Provider Type'),
'class' => 'required-entry',
'required' => true,
'name' => 'providertype',
'onclick' => "",
'onchange' => "",
'value' => '1',
'values' => array('70' => '70', '99' => '99'),
'disabled' => false,
'readonly' => false,
'after_element_html' => '<small>Default Provider Type</small>',
'tabindex' => 1
));
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
you can fetch data from database and fill that data into fields or you can use session and store form post values into it before saving the data into database and when it redirects on your form page you can check if values are exists then you fill them in form
Your question is little confusable, I'm assuming that you want to keep fields data field after the form submit....
Is this is the scenario then you can use PHP Sessions to store data in sessions and check on page load if session is set then display it.
Like:
PHP
session_start();
// you can set session on form submit using if(isset($_POST)){//set session here}
$_SESSION['example'] = "Example Value";
HTML
<input type="text" name="example" value="<?=(isset($_SESSION['example']) ? $_SESSION['example'] : ''); ?>"/>
Related
I'm so beginner in Prestashop 1.7, I wanted to add a dropdown select section in my banner module to select the way to open the banner link.
but the selected value is never passed to the HTML, the code below IS passed but the one under isn't, can you please assist me?
[enter image description here][1]
array(
'type' => 'text',
'lang' => true,
'label' => $this->trans('Banner description', array(), 'Modules.Banner.Admin'),
'name' => 'BANNER_DESC',
'desc' => $this->trans('Please enter a short but meaningful description for the banner.', array(), 'Modules.Banner.Admin')
)
array(
'type' => 'select', //select
'lang' => true,
'label' => $this->trans('Banner tab', array(), 'Modules.Banner.Admin'),
'name' => 'BANNER_TAB',
'required'=>'true',
'options' => array(
'query' => array(
array('key' => '_blank', 'name' => 'New tab'),
array('key' => '_self', 'name' => 'Same tab'),
),
'id' => 'key',
'name' => 'name'
),
'desc' => $this->trans('Please select the way to open the link.', array(), 'Modules.Banner.Admin')
)
This is how it looks in the Backoffice:
Here
You not only need to add a new field to your form but also handle saving the data from it.
Take a look at a few examples:
https://github.com/PrestaShop/ps_featuredproducts/blob/dev/ps_featuredproducts.php#L122
Notice how the module author managed to save each configuration field from the form. This is what you need to do.
If you want to have access to data in your view, you have to pass it:
https://github.com/PrestaShop/ps_featuredproducts/blob/dev/ps_featuredproducts.php#L244
Maybe after you added a new field, you forgot to handle the saving + passing to the view?
I am creating a module in prestashop 1.7 to save my settings.
Also I created a form to display my settings. Form sample is shown below:-
//display form function
public function renderCustomerForm()
{
$this->fields_form = array(
'legend' => array(
'title' => $this->l('Customer Settings'),
'icon' => 'icon-time'
),
'input'=>array(
array(
'type' => 'text',
'label' => $this->l('BusinessCustomerFlag'),
'name' => 'C_BUSINESS_FLAG',
'lang' => false,
'required' => true
),
),
'submit' => array(
'title' => $this->l('Save'),
'name' => 'submitCustomer',
'icon' => 'process-icon-save'
)
);
I am saving this values in configuration table using configuration class functions.
I know how to retrieve it but don't know how to show in the form. Please some one guide on this will be really helpful.
Add this line to helper on your module (before generateForm):
$helper->fields_value = $this->getFormValues();
and add function to define values:
public function getFormValues()
{
$fields_value = array();
$fields_value['C_BUSINESS_FLAG'] = "some data or retrieved data";
return $fields_value;
}
So I have an ecommerce using woocommerce and I use custom shipping for track shipping fee.
And I already add new input data (select). like you can see on below picture:
// Hook in
add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields');
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields($fields) {
$fields['billing']['billing_city'] = array(
'type' => 'select',
'label' => __('Kota / Kabupaten', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide', 'address-field'),
'clear' => true,
'options' => array(
'' => 'Pilih Kota / Kabupaten'
)
);
$fields['shipping']['shipping_city'] = array(
'type' => 'select',
'label' => __('Kota / Kabupaten', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide', 'address-field'),
'clear' => true,
'options' => array(
'' => 'Pilih Kota / Kabupaten'
)
);
$fields['billing']['billing_subdistrict'] = array(
'type' => 'select',
'label' => __('Kecamatan', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide', 'address-field'),
'clear' => true,
'options' => array(
'' => 'Pilih Kecamatan'
)
);
$fields['shipping']['shipping_subdistrict'] = array(
'type' => 'select',
'label' => __('Kecamatan', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide', 'address-field'),
'clear' => true,
'options' => array(
'' => 'Pilih Kecamatan'
)
);
return $fields;
}
Woocommerce default data had address_1,address_2,country,state,city but I need one more data called subdistrict. I don't need to save that data (subdistrict). But I need to use that value as parameter for track shipping fee.
I already create new class-custom-shipping-delivery.php.
and I already make sure that function work perfectly because I already try to set $subdistrict data manually.
//custom-shipping.php
$province = $package['destination']['state'];
$city = $package['destination']['city'];
$subdistrict= 'something';
//How to get the data from custom field (ajax)
//because I need to see the shipping fee result before Checkout (and update it to add rate)
$destination_code = $this->getDestinationCode($province,$city,$subdistrict);
$ongkir = $this->cek_ongkir($origin,$destination_code,$weight);
//print_r();
// send the final rate to the user.
$this->add_rate(array(
'id' => $this->id,
'label' => $this->title,
'cost' => $ongkir
));
Summary:
How to get Value from Subdistrict Input type select (on checkout page)?
Sorry I just edit from another person work so I'm not understand that code at all. But I think they forgot to get that value because they just hardcode it and I'm a newbie on wordpress so I don't know how to pass data on checkout form.
After searching the answer for a while then I get the answer.
So for Summary it:
Above answer use session to get the data from shipping custom field (ajax).
So when AJAX run. I send value of 'subdistrict' field to function and save it to session.
function ajax_process($subdistrict){
//some code
session_start();
$_SESSION['subdistrict'] = $subdistrict;
}
then for get the session data from other function I run this code :
#session_start();
$subdistrict = $_SESSION['subdistrict'];
Here is a plugin called Checkout Field Editor for Woocommerce.
https://docs.woocommerce.com/document/checkout-field-editor/
Looks like that plugin costs $49 for a single site license.
The other option is to code it yourself. Here is a tutorial.
https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
How can i add a new field in prestashop's back office?
Specific, i want to insert a text field in the BO: Orders->Statuses->Add New Order Status under the status name. Which files i have to modify in order to do that? Can anyone describes the full procedure?
Thanks
I am using Prestashop version 1.6.1.2 and added one text field using following steps. You need to make changes in core files. You have to add field in one table in database and do some changes in class and controller file.
Here are the steps to do the same. I have adde field 'my_custom_field'.
Add one field in order_state table
ALTER TABLE {YOUR_DB_PREFIX}order_state ADD my_custom_field VARCHAR(50) NOT NULL;
Change class file of order state. You need to define your field in file "classes/order/OrderState.php"
After code
public $deleted = 0;
add this code snipet
public $my_custom_field;
After code
'deleted' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
add this code snipet
'my_custom_field' => array('type' => self::TYPE_STRING),
open "controllers/admin/AdminStatusesController.php" file and do following changes
in function initOrderStatutsList()
after this code
'name' => array(
'title' => $this->l('Name'),
'width' => 'auto',
'color' => 'color'
),
add this code
'my_custom_field' => array(
'title' => $this->l('My Custom Field'),
'width' => 'auto',
),
in function renderForm()
after this code
array(
'type' => 'text',
'label' => $this->l('Status name'),
'name' => 'name',
'lang' => true,
'required' => true,
'hint' => array(
$this->l('Order status (e.g. \'Pending\').'),
$this->l('Invalid characters: numbers and').' !<>,;?=+()##"{}_$%:'
)
),
add this code
array(
'type' => 'text',
'label' => $this->l('My Custom field'),
'name' => 'my_custom_field',
),
Do changes suggested here. Hope this helps you :)
I would like to reload the form, or preferably just the values, by using 'onChange'. There are several elements in the form that needs to be updated when changing the project id.
$this->addElement('select', 'project_id', array(
'label' => t('Project'),
'multiOptions' => $data,
'filters' => array('Int'),
'value' => 0,
'required' => true,
'disableTranslator' => true,
'validators' => array(),
'onChange' => 'Javascript:window.location.reload(false)'
));
You can change your onChange to call a custom javascript function instead - which will change any fields you need to change, and then submmit the form.