skip to Main Content

I am creating a WooCommerce plugin for my requirements. When I activate my plugin I created two (2) user meta fields on User Form and saving the data, it’s working perfectly.

Now I am trying to add custom dropdown field on checkout page, but it’s not working. I searched a lot and asked to chatgpt but nothing worked. It even didn’t show me the label or heading.

Here is the code of plugin main file:

<?php
defined('ABSPATH') || exit;

// Checking if Woocommerce installed or not
if(!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
    exit;
}

if(!defined('WCSF_PLUGIN_FILE')) {
    define('WCSF_PLUGIN_FILE', __FILE__);
}
if(!defined('WCSF_PLUGIN_URL')) {
    define('WCSF_PLUGIN_URL', plugin_dir_url(__FILE__));
}

// Load plugin text domain
if(!function_exists('wcsf_plugin_load_textdomain')) {
    add_action('init', 'wcsf_plugin_load_textdomain');
    function wcsf_plugin_load_textdomain() {
        load_plugin_textdomain('wcsf', false, dirname(plugin_basename(__FILE__)).'/languages');
    }
}

// Plugin activation Hook
if(!function_exists('wcsf_plugin_activation')) {
    function wcsf_plugin_activation() {
        // Adding Roles
        wcsf_user_roles();
    }

    register_activation_hook(__FILE__, 'wcsf_plugin_activation');
}

//=========> Delete Data on uninstall
register_uninstall_hook('uninstall.php','wcsf_plugin_activation');

if(!function_exists('wcsf_user_roles')) {
    function wcsf_user_roles() {
        // My roles
    }
}

if(!function_exists('wcsf_enqueue_global_scripts')) {
    function wcsf_enqueue_global_scripts() {
        wp_enqueue_script('wcsf-custom-script', plugins_url('/public/js/custom-script.js', __FILE__), array('jquery'), '1.0', true);
        wp_enqueue_style('wcsf-custom-style', plugins_url('/public/css/custom-style.css', __FILE__), array(), '1.0');
    }

    add_action('wp_enqueue_scripts', 'wcsf_enqueue_global_scripts');
}

if(!function_exists('wcsf_custom_franchiser_fields_added')) {
    function wcsf_custom_franchiser_fields_added() {
       // user meta field code
   }

    add_action('user_new_form', 'wcsf_custom_franchiser_fields_added');
}

After that I tried (Hooks / Filters):

Code Trying 01

add_action('woocommerce_after_checkout_shipping_form','wcsf_city_checkout_page');
function wcsf_city_checkout_page() {
  echo '<div id="custom_checkout_field"><h2>' . esc_html__('Custom City','wcsf') . '</h2></div>';
}

Code Trying 02

add_action('woocommerce_checkout_fields','wcsf_city_checkout_page');
function wcsf_city_checkout_page() {
    echo '<div id="custom_checkout_field"><h2>' . esc_html__('Custom City','wcsf') . '</h2> 
    </div>';
}

Code Trying 03

add_action('woocommerce_after_checkout_shipping_form','wcsf_city_checkout_page');
function wcsf_city_checkout_page() {
   echo '<div id="custom_checkout_field"><h2>' . esc_html__('Custom City','wcsf') . '</h2></div>';
}

Code Trying 04

add_filter('woocommerce_checkout_fields','wcsf_city_checkout_page');
function wcsf_city_checkout_page($checkout) {
   $checkout['shipping']['custom_city'] = array(
      'label'     => __( 'Custom City', 'example' ),
      'type'      => 'text',
      'placeholder'   => _x( 'Type', 'placeholder', 'example'),
      'required'  => false,
      'class'     => array( 'custom_city' ),
      'clear'     => true
     );
  return $checkout;
}

And much more codes. I am using WordPress custom theme twentytwo and also tried these codes in active theme’s functions.php file, but none of them code is working. I even write die; or exit; in callback functions, but nothing happened.

I searched a lot and tried a lot of codes (From stack overflow) but nothing happened and surprised why these hooks or filters not working.

Did I miss something, including WooCommerce file etc. ?

Note: I used PHP 8, WordPress 6.1.4 and latest Woocommerce. and just Woocmmerce show me this kind of warning and its just showing on checkout page only.

Deprecated: Creation of dynamic property WC_Order_Item_Shipping::$legacy_package_key is deprecated in pluginswoocommerceincludesclass-wc-checkout.php on line 604

Hope you guys understood my question and what I am facing the problem.

2

Answers


  1. If WooCommerce support is enabled for your theme, try the following approach instead:

    if ( ! defined( 'ABSPATH' ) ) {
        exit; // Exit if accessed directly.
    }
    
    if( ! defined('WCSF_PLUGIN_FILE') ) {
        define( 'WCSF_PLUGIN_FILE', __FILE__ );
    }
    
    if( ! defined('WCSF_PLUGIN_DIR') ) {
        define( 'WCSF_PLUGIN_DIR', __DIR__ );
    }
    
    // Plugin activation
    register_activation_hook(WCSF_PLUGIN_FILE, 'wcsf_plugin_activation');
    function wcsf_plugin_activation() {
        // some code
    }
    
    // Plugin deactivation
    register_deactivation_hook(WCSF_PLUGIN_FILE, 'wcsf_plugin_deactivation');
    function wcsf_plugin_deactivation() {
        // some code
    }
    
    // Plugin init hook.
    add_action( 'plugins_loaded', 'wcsf_plugin_init', 1 );
    function wcsf_plugin_init() {
    
        // Check that WooCommerce is enabled
        if ( ! class_exists( 'WooCommerce' ) ) {
            add_action( 'admin_notices', 'wcsf_woocommerce_required_plugin_activation' );
            return;
        } else {
            // Adding Roles
            wcsf_add_custom_user_roles();
        
            // Below, your WordPress/WooCommerce action and filter hooks
            add_action( 'wp_enqueue_scripts', 'wcsf_enqueue_global_scripts' );
            add_action( 'user_new_form', 'wcsf_custom_franchiser_fields_added' );
            add_action( 'woocommerce_shipping_fields', 'wcsf_custom_shipping_city_field' );
        }
    }
    
    // Required not activated WooCommerce plugin throw an Admin Error Notice.
    function wcsf_woocommerce_required_plugin_activation() {
        // WooCommerce
        echo '<div class="error"><p>' . sprintf( esc_html__( 'WCSF plugin requires %s to be installed and active.', 'wcsf' ), '<a href="https://wordpress.org/plugins/woocommerce/" target="_blank">WooCommerce</a>' ) . '</p></div>';
        return;
    }
    
    // Add custom user roles
    if( ! function_exists('wcsf_add_custom_user_roles') ) {
        function wcsf_add_custom_user_roles() {
            // Add some roles
        }
    }
    
    if(!function_exists('wcsf_enqueue_global_scripts')) {
        function wcsf_enqueue_global_scripts() {
            // wp_enqueue_script('wcsf-custom-script', plugins_url('/public/js/custom-script.js', __FILE__), array('jquery'), '1.0', true);
            // wp_enqueue_style('wcsf-custom-style', plugins_url('/public/css/custom-style.css', __FILE__), array(), '1.0');
        }
    }
    
    if(!function_exists('wcsf_custom_franchiser_fields_added')) {
        function wcsf_custom_franchiser_fields_added() {
           // user meta field code
       }
    }
    
    add_filter('woocommerce_shipping_fields', 'wcsf_custom_shipping_city_field');
    function wcsf_custom_shipping_city_field( $fields ) {
        $fields['custom_city'] = array(
            'label'     => __( 'Custom City', 'example' ),
            'type'      => 'text',
            'placeholder'   => _x( 'Type', 'placeholder', 'example'),
            'required'  => false,
            'class'     => array( 'custom_city' ),
            'clear'     => true
        );
        return $fields;
    }
    

    It should work.

    If WooCommerce is not enabled, or if WooCommerce support is not implemented in the theme, an error admin notice will be thrown:

    enter image description here

    Recommendation: you should try to choose something more explicit than WCSF as it can be already used by some other plugin (that why I have renamed some of your existing functions).

    Login or Signup to reply.
  2. You can switch to the legacy checkout page.

    Go to Pages->Checkout->Edit
    Remove the checkout block and replace it with the shortcode [woocommerce_checkout]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search