Before proceeding cart checkout user have to enter shipping address and shipping address contains shipping rate and tax rates that you can set from your shopify admin panel.
here are the procedure :
1. Layout for shipping address file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="com.android.allvino.Activities.RegistrationActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarShippingAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="@color/colorPrimaryDark"
app:layout_scrollFlags="scroll|enterAlways|snap"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:text="@string/text_shipping_add"
android:textSize="@dimen/text_size_mid"
android:id="@+id/tvTitle"
android:layout_centerInParent="true"
android:textColor="@color/white"
android:gravity="center"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="wrap_content"
android:background="@drawable/grapesicon"
android:layout_marginRight="@dimen/padding_ten"
android:layout_toLeftOf="@id/tvTitle"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:background="@color/list_item_color"
android:layout_marginTop="@dimen/padding_ten"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/five_margin_top"
android:text="@string/text_customer_info"
android:paddingLeft="@dimen/margin_twenty"
android:textSize="@dimen/text_size_large"
android:textColor="@color/dark_red"
android:singleLine="true"/>
<TextView
android:id="@+id/tvCustomerName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/padding_ten"
android:text="Caleb "
android:paddingLeft="@dimen/margin_twenty"
android:textSize="@dimen/text_size_large"
android:textColor="@color/dark_gray"
android:singleLine="true"/>
</LinearLayout>
<LinearLayout
android:id="@+id/registration_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginTop="@dimen/margin_twenty"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_6dp"
android:layout_marginTop="@dimen/spacing_6dp">
<EditText
android:id="@+id/etShippingFname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_fname"
android:paddingLeft="@dimen/margin_twenty"
android:textColorHint="@color/dark_gray"
android:layout_margin="@dimen/padding_ten"
android:textSize="@dimen/text_size_mid"
android:maxLength="15"
android:inputType="text|textCapWords|textNoSuggestions"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_6dp"
android:layout_marginTop="@dimen/spacing_6dp">
<EditText
android:id="@+id/etShippingLname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/padding_ten"
android:maxLength="15"
android:hint="@string/hint_lname"
android:paddingLeft="@dimen/margin_twenty"
android:textColorHint="@color/dark_gray"
android:textSize="@dimen/text_size_mid"
android:inputType="text|textCapWords|textNoSuggestions"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_6dp"
android:layout_marginTop="@dimen/spacing_6dp">
<EditText
android:id="@+id/etShippingAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/padding_ten"
android:hint="@string/text_shipping_address"
android:paddingLeft="@dimen/margin_twenty"
android:textColorHint="@color/dark_gray"
android:textSize="@dimen/text_size_mid"
android:maxLength="40"
android:inputType="text|textCapWords|textNoSuggestions"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/five_margin_top"
android:layout_marginTop="@dimen/five_margin_top">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/padding_ten"
android:text="@string/text_usa"
android:editable="false"
android:paddingLeft="@dimen/margin_twenty"
android:inputType="none"
android:focusable="false"
android:textSize="@dimen/text_size_mid"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/five_margin_top">
<Spinner
android:id="@+id/etSpinnerState"
android:layout_width="match_parent"
android:layout_height="@dimen/logo_margin_top"
android:layout_margin="@dimen/padding_ten"
android:paddingLeft="@dimen/margin_twenty"
android:textColorHint="@color/dark_gray"
android:textSize="@dimen/text_size_mid"
android:spinnerMode="dropdown"
android:background="@drawable/search_view_gray"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/five_margin_top">
<EditText
android:id="@+id/etShippingCity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/padding_ten"
android:hint="@string/text_city"
android:maxLength="15"
android:editable="false"
android:paddingLeft="@dimen/margin_twenty"
android:textColorHint="@color/dark_gray"
android:textSize="@dimen/text_size_mid"
android:inputType="text|textCapWords|textNoSuggestions"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/etShippingZipcode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/padding_ten"
android:hint="@string/text_shipping_zipcode"
android:paddingLeft="@dimen/margin_twenty"
android:maxLength="6"
android:textColorHint="@color/dark_gray"
android:textSize="@dimen/text_size_mid"
android:inputType="number"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<Button
android:layout_width="@dimen/btn_login_width"
android:id="@+id/btnShippingCont"
android:text="@string/text_cont"
android:textColor="@color/white"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/padding_ten"
android:textSize="@dimen/text_size_mid"
android:background="@drawable/dark_gray_btn_corner"
android:layout_marginBottom="@dimen/margin_twenty"
android:layout_height="@dimen/btn_height" />
</LinearLayout>
</ScrollView>
</LinearLayout>
2. Enter fields for shipping address :
private void setShippingAddress(){
Customer customer = AppSharedPrefrences.getCustomer(ShippingAddressActivity.this);
if (customer != null) {
final Address address = new Address();
address.setFirstName(etShippingFname.getText().toString());
address.setLastName(etShippingLname.getText().toString());
address.setAddress1(etShippingAddress.getText().toString());
address.setCity(etShippingCity.getText().toString());
address.setProvinceCode(mListCodes.get(etSpinnerState.getSelectedItemPosition()));
address.setZip(etShippingZipcode.getText().toString());
address.setCountryCode("US");
ArrayList<Address> addressArrayList = new ArrayList<>();
addressArrayList.add(address);
customer.setAddresses(addressArrayList);
updateCustomerDataOnShopify(customer);
}else{
AppLogs.printLogs("default address :","customer null");
}
}
3. After setting shipping address you have to update customer so that you can sync with web also :
private void updateCustomerDataOnShopify(Customer aCustomer){
SampleApplication.getBuyClient().updateCustomer(AppSharedPrefrences.getCustomer(ShippingAddressActivity.this), new Callback<Customer>() {
@Override
public void success(Customer response) {
AppLogs.printLogs("default address saved:", response.getAddresses().get(response.getAddresses().size()-1).toJsonString());
}
@Override
public void failure(BuyClientError error) {
AppLogs.printLogs(" customer not update :",error.toString());
}
});
Intent returnIntent = new Intent();
setResult(RESULT_OK,returnIntent);
finish();
}
4. Update customer is callback method of shopify sdk :
CancellableTask updateCustomer(Customer customer, Callback<Customer> callback);
Note : if user already have entered shipping address you can get it back like this :
private void getShippingAddress(){
Customer customer = AppSharedPrefrences.getCustomer(ShippingAddressActivity.this);
if (customer != null) {
if (customer.getAddresses().size() !=0){
if (customer.getAddresses().get(customer.getAddresses().size()-1).toString() != null) {
AppLogs.printLogs("customer address :", customer.getAddresses().get(customer.getAddresses().size()-1).toJsonString());
setAutoFiiledAddress(customer.getAddresses().get(customer.getAddresses().size()-1));
}
}
}else{
AppLogs.printLogs("default address :","customer null");
}
}
5. Here are the method save customer locally so that you can easily access customer :
public static void saveCustomer(Customer aCustomer,Activity aActivity){
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(aActivity);
mEditor= mSharedPreferences.edit();
mGson = new Gson();
String json = mGson.toJson(aCustomer);
mEditor.putString(key_customer_info, json);
mEditor.commit();
}
public static Customer getCustomer(Activity aActivity){
mGson = new Gson();
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(aActivity);
String json = mSharedPreferences.getString(key_customer_info, "");
Customer customer = mGson.fromJson(json, Customer.class);
return customer;
}
In this tutorial we learn how to update shipping address now in our next tutorial we ll learn about checkout process.
0 Comment(s)