Below, you’ll find a detailed guide on how to add the Cities Shipping Zones for WooCommerce Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Cities Shipping Zones for WooCommerce Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the Cities Shipping Zones for WooCommerce Plugin and the shortcodes it provides:
"Cities Shipping Zones for WooCommerce is a powerful plugin that enables efficient and customizable shipping zones by city for your WooCommerce store. Essential for optimized logistics."
- [csz_cities]
Cities Shipping Zones for WooCommerce [csz_cities] Shortcode
The Cities Shipping Zones for WooCommerce shortcode is used to generate a shipping address form. It dynamically populates the city, state, and country fields based on the user’s location. This shortcode is highly customizable, allowing you to modify labels, descriptions, and classes for each field. It also includes international shipping options.
Shortcode: [csz_cities]
Parameters
Here is a list of all possible csz_cities shortcode parameters and attributes:
international
– Determines if international shipping is available.country
– Specifies the shipping country.description
– Provides a description for the shipping address field.class
– Specifies the CSS class for the form row.label
– Sets the label for the city field.value
– Sets the default value for the shipping state field.country_description
– Provides a description for the country field.country_class
– Specifies the CSS class for the country field.country_label
– Sets the label for the country field.template
– Determines the template to be used for the form.
Examples and Usage
Basic example – A simple usage of the cities-shipping-zones-for-woocommerce plugin shortcode to display the shipping form with default settings.
[csz_cities /]
Advanced examples
Displaying the shipping form with the option to select international countries and a custom label for the city field.
[csz_cities international="yes" label="Select your city" /]
Displaying the shipping form with a custom description and a custom label for the country field.
[csz_cities description="Please provide your shipping details below." country_label="Choose your country" /]
Displaying the shipping form with a custom class for styling purposes.
[csz_cities class="my-custom-class" /]
Using the shortcode with a template for a popup display of the shipping form.
[csz_cities template="popup" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [csz_cities]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( 'csz_cities', [ $this, 'wc_csz_cities_shortcode' ] );
Shortcode PHP function:
function wc_csz_cities_shortcode( $atts ) {
if ( apply_filters( 'csz_cities_shortcode_enabled', true ) && ! is_checkout() && ! is_cart() ) {
$atts = shortcode_atts( [
'international' => array_keys( WC()->countries->get_shipping_countries() ) === [ WC()->countries->get_base_country() ] ? 'no' : 'yes',
'country' => isset( WC()->customer ) && WC()->customer->get_shipping_country() ? WC()->customer->get_shipping_country() : WC()->countries->get_base_country(),
'description' => __( 'Enter your address to view shipping options.', 'woocommerce' ),
'class' => [ 'form-row', 'address-field', 'form-row-first' ],
'label' => __( 'City', 'woocommerce' ),
'value' => isset( WC()->customer ) && WC()->customer->get_shipping_state() ? WC()->customer->get_shipping_state() : '',
'country_description' => '',
'country_class' => '',
'country_label' => __( 'Country / Region', 'woocommerce' ),
'template' => '',
], $atts, 'csz_cities' );
if ( 'popup' === $atts['template'] ) {
if ( is_product() ) {
return;
}
wp_enqueue_style( 'dashicons' );
add_action( 'wp_footer', [ $this, 'csz_modal_javascript' ], 20 );
$prefix = '<span class="dashicons dashicons-location"></span><a href="#" id="cities-update">' . __( 'Select an option…', 'woocommerce' ) . '</a><div class="cities-modal"><div class="cities-modal-container"><div class="cities-modal-content"><a href="#" class="cities-modal-close">×</a><h4 class="cities-modal-title">' . __( 'shipping address', 'woocommerce' ) . '</h4>';
$suffix = '</div></div></div>';
}
wp_enqueue_script( 'selectWoo' );
wp_enqueue_style( 'select2' );
wp_enqueue_script( 'wc-country-select' );
add_action( 'wp_footer', [ $this, 'csz_match_javascript' ], 20 );
$state_args = apply_filters( 'csz_cities_shortcode_state_args', [
'type' => 'state',
'required' => true,
'label' => $atts['label'],
'class' => $atts['class'],
'description' => ! empty( $atts['description'] ) ? $atts['description'] : ' ',
'country' => 'yes' === $atts['international'] ? $atts['country'] : WC()->countries->get_base_country(),
] );
ob_start();
if ( 'yes' === $atts['international'] ) {
woocommerce_form_field( 'shipping_country', apply_filters( 'csz_cities_shortcode_country_args', [
'type' => 'country',
'required' => true,
'label' => $atts['country_label'],
'class' => ! empty( $atts['country_class'] ) ? $atts['country_class'] : $atts['class'],
'description' => ! empty( $atts['country_description'] ) ? $atts['country_description'] : ' ',
] ), $atts['country'] );
}
if ( 'yes' === get_option( 'wc_csz_new_state_field' ) && in_array( WC()->countries->get_base_country(), get_option( 'wc_csz_countries_codes' ) ) ) {
$country_states = [];
include( 'i18n/cities/' . WC()->countries->get_base_country() . '.php' );
$country_states = apply_filters( 'csz_states', $country_states );
if ( apply_filters( 'csz_sort_states', true ) ) {
asort( $country_states );
}
woocommerce_form_field( 'shipping_new_state', apply_filters( 'csz_cities_shortcode_state_filter_args', [
'type' => 'select',
'options' => [ '' => '' ] + $country_states,
'class' => $atts['class'],
'input_class' => [ 'state_select' ],
'label' => __( 'State / County', 'woocommerce' ),
] ) );
}
woocommerce_form_field( 'shipping_state', $state_args, $atts['value'] );
return ( isset( $prefix ) ? $prefix : '' ) . ob_get_clean() . ( isset( $suffix ) ? $suffix : '' );
}
}
Code file location:
cities-shipping-zones-for-woocommerce/cities-shipping-zones-for-woocommerce/cities-shipping-zones-for-woocommerce.php
Conclusion
Now that you’ve learned how to embed the Cities Shipping Zones for WooCommerce Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply