Search code examples
phpwoocommerceproductcountry

How to Display all the Available Shipping Countries in a Column in Products list WooCommerce?


I am using ALD – Dropshipping and Fulfillment for AliExpress and WooCommerce plugin. Is there any way to display all available shipping countries for a product, in Admin product list new column (I mean a column that contains list of all countries that are available for shipping in each product).

I tried a plugin named //Product Visibility by Country for WooCommerce// displays a column for countries, but the problem with this plugin is that you should add manually the available countries for each product.

I am asking if there is a function to display the countries that are already linked with each product.

In my case, I am using plugins to import data from different sources, and the products came automatically with the available shipping countries. So now, it is not easy to know which countries that are available for each product, I can only know by visiting the checkout page and do a manual check for each country.

I hope there is a function to display them in a column in the products list.


Solution

  • The shipping countries' data is imported from AliExpress using the plugin, To achieve the goal of displaying all available countries in a column, we must analyze all countries in each individual product page (because AliExpress has an individual page for each country) and filter which country is available and which country is unavailable, and generate the data. That will be complex and take more resources. However, You can use the code below the display the main shipping country that was chosen to import products, this code only displays one country and not all the available shipping countries.

    // Add a custom column to the product list in the WordPress admin
                function add_shipping_country_column( $columns ) {
                    $columns['shipping_country'] = 'Shipping Country';
    
                    return $columns;
                }
    
                add_filter( 'manage_product_posts_columns', 'add_shipping_country_column' );
    
        // Populate the custom column with shipping country data from the "_vi_wad_shipping_info" custom field
                function populate_shipping_country_column( $column, $post_id ) {
                    if ( $column == 'shipping_country' ) {
    
                        $ali_import_id = VI_WOOCOMMERCE_ALIDROPSHIP_DATA::product_get_id_by_woo_id( $post_id );
        // Get the post type to check if it's a product or another post type
    
        // Get the shipping information from the "_vi_wad_shipping_info" custom field
                        $shipping_info = ALD_Product_Table::get_post_meta( $ali_import_id, '_vi_wad_shipping_info', true );
    
        // Check if the shipping_info field exists and contains data
                        if ( ! empty( $shipping_info ) ) {
        // Unserialize the data
                            $shipping_info_data = maybe_unserialize( $shipping_info );
    
        // Check if the 'country' key exists
                            if ( isset( $shipping_info_data['country'] ) ) {
                                $shipping_country = $shipping_info_data['country'];
    
        // Display the shipping country
                                echo esc_html( $shipping_country );
                            } else {
                                echo 'N/A'; // Display N/A if the 'country' key is not found
                            }
                        } else {
                            echo 'N/A'; // Display N/A if the shipping_info field is empty
                        }
    
                    }
                }
    
                add_action( 'manage_product_posts_custom_column', 'populate_shipping_country_column', 10, 2 );