VirtueMart Forum

VirtueMart 2 + 3 + 4 => Plugins: Payment, Shipment and others => Topic started by: jibon57 on July 01, 2014, 18:28:31 PM

Title: How to show states after selecting country ?
Post by: jibon57 on July 01, 2014, 18:28:31 PM
Hello,

I am trying to extended the default VM shipping plugin & want to add states when user will choose countries. How I can get all the states list when user will choose one country as in "Tax & Calculation Rule" in VM ? Here is what I did:

I added new file in elements folder called "vmstates.php" so that I can get all available states list in xml file. Here is my vmstates.php file:


<?php
defined
('_JEXEC') or die();

class 
JElementVmStates extends JElement {

    
/**
     * Element name
     * @access protected
     * @var string
     */
    
var $_name 'states';

    function 
fetchElement($name$value, &$node$control_name) {

        
$db =  JFactory::getDBO();

        
$query 'SELECT `virtuemart_state_id` AS value, `state_name` AS text FROM `#__virtuemart_states`
                WHERE `published` = 1 ORDER BY `state_name` ASC '
        
;

        
$db->setQuery($query);
        
$fields $db->loadObjectList();
    $class = ($node->attributes('class') ? 'class="' $node->attributes('class') . '"' '');


    $class 'multiple="true" size="10"  ';
        return 
JHTML::_('select.genericlist'$fields$control_name '[' $name '][]'$class'value''text'$value$control_name $name);
    }

}


& my xml file:


<params addpath="/plugins/vmshipment/weight_countries/elements"/>
<param name="states" type="vmstates" scope="com_virtuemart" default=""
               label="States" description="States"/>


But I am getting all the states list. Is there anyway to retrieve data according to country that should be select from top ?
Title: Re: How to show states after selecting country ?
Post by: GJC Web Design on July 01, 2014, 23:22:17 PM
the states arereturned by an ajax call dependent of the country chosen - have a look at the normal user registration and trace the JS used to trigger the call -
Title: Re: How to show states after selecting country ?
Post by: jibon57 on July 02, 2014, 06:16:23 AM
Quote from: GJC Web Design on July 01, 2014, 23:22:17 PM
the states arereturned by an ajax call dependent of the country chosen - have a look at the normal user registration and trace the JS used to trigger the call -

Thanks for your reply. Can you help me to give me more clear idea please ? I tried in all ways but really can't figure out :(
Title: Re: How to show states after selecting country ?
Post by: GJC Web Design on July 03, 2014, 00:22:35 AM
have a look at the JS in components/com_virtuemart/assets/js/vmsite.js from about line 39

but the basic call to get a country dependent state list is

/index.php?option=com_virtuemart&view=state&format=json&virtuemart_country_id=222

returns a JSON state list of that country

e.g.

{"222":[{"virtuemart_state_id":"65","virtuemart_vendor_id":"1","virtuemart_country_id":"222","virtuemart_worldzone_id":"0","state_name":"England","state_3_code":"ENG","state_2_code":"EN","ordering":"0","shared":"0","published":"1","created_on":"0000-00-00 00:00:00","created_by":"0","modified_on":"0000-00-00 00:00:00","modified_by":"0","locked_on":"0000-00-00 00:00:00","locked_by":"0"},{"virtuemart_state_id":"66","virtuemart_vendor_id":"1","virtuemart_country_id":"222","virtuemart_worldzone_id":"0","state_name":"Northern Ireland","state_3_code":"NOI","state_2_code":"NI","ordering":"0","shared":"0","published":"1","created_on":"0000-00-00 00:00:00","created_by":"0","modified_on":"0000-00-00 00:00:00","modified_by":"0","locked_on":"0000-00-00 00:00:00","locked_by":"0"},{"virtuemart_state_id":"67","virtuemart_vendor_id":"1","virtuemart_country_id":"222","virtuemart_worldzone_id":"0","state_name":"Scotland","state_3_code":"SCO","state_2_code":"SD","ordering":"0","shared":"0","published":"1","created_on":"0000-00-00 00:00:00","created_by":"0","modified_on":"0000-00-00 00:00:00","modified_by":"0","locked_on":"0000-00-00 00:00:00","locked_by":"0"},{"virtuemart_state_id":"68","virtuemart_vendor_id":"1","virtuemart_country_id":"222","virtuemart_worldzone_id":"0","state_name":"Wales","state_3_code":"WLS","state_2_code":"WS","ordering":"0","shared":"0","published":"1","created_on":"0000-00-00 00:00:00","created_by":"0","modified_on":"0000-00-00 00:00:00","modified_by":"0","locked_on":"0000-00-00 00:00:00","locked_by":"0"}]}
Title: Re: How to show states after selecting country ?
Post by: Pirated on July 03, 2014, 09:25:21 AM
Thanks - worked for me!

8) 8) 8) 8) 8)
Title: Re: How to show states after selecting country ?
Post by: jibon57 on July 03, 2014, 19:41:45 PM
Thanks for your reply GJC Web Design & Pirated. But Pirated will you please write down the code here what you did ? I ain't such expert really :) .  The main problem is I really can't understand how I will send request from xml file? In shipping system all those are handled by xml file. That's why it's making me confusion :( .  Thanks in advance :)