0

I'm getting this error when cart page loads (plugin is designed to work for cart page only):

Fatal error: Cannot redeclare custom_cart_min_qty() (previously declared in

/home/platne/serwer24774/public_html/staging/wp-content/plugins/my-plugin-slug/my-plugin-slug.php:199) in

/home/platne/serwer24774/public_html/staging/wp-content/plugins/my-plugin-slug/my-plugin-slug.php on line 199

It looks like my code executes twice. How can I prevent it the proper way, without only checking whether function is defined? Is it connected to me creating new class instance on page load perhaps?

I edited some irrelevant lines and descriptions to made this long code somewhat easier to inspect, mentioned function is now in different line.

acau_setting_2 is settings page checkbox value which if checked, causes custom_cart_min_qty() function to execute.

<?php

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
    die;
}

// Create settings class.
class my_plugin_name_settings
{   
    private $options;   
    private $settings_page_name;
    private $settings_menu_name;

    public function __construct()
    {               
        $this->settings_page_name = 'my-plugin-slug';
        $this->settings_menu_name = 'My Plugin Name';   

        // Initialize and register settings. 
        add_action( 'admin_init', array( $this, 'register_settings' ) );
        // Add settings page.
        add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
        // Add settings link to plugins page.
        add_action( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_settings_link' ) );
    }

    public function register_settings()
    {   
        register_setting( 'acau_settings', 'acau_settings', array( $this, 'sanitize' ) );

        // ID / title / callback / page
        add_settings_section( 'configuration_section', __( 'Configuration', 'my-plugin-slug' ), array( $this, 'print_section_info' ), $this->settings_page_name );

        // ID / title / callback / page / section
        add_settings_field( 'acau_setting_1', __( 'Irrelevant setting with integer value', 'my-plugin-slug' ), array( $this, 'acau_setting_1_callback' ), $this->settings_page_name, 'configuration_section' );
        add_settings_field( 'acau_setting_2', __( 'Cart minimum quantity', 'my-plugin-slug' ), array( $this, 'acau_setting_2_callback' ), $this->settings_page_name, 'configuration_section' );     

    }

    public function add_settings_page()
    {
        // This page will be under "Settings"
        add_options_page(
            'Settings Admin', $this->settings_menu_name, 'manage_options', $this->settings_page_name, array( $this, 'create_settings_page' )
        );
    }

    /**
     * Get the option that is saved or the default.
     *
     * @param string $index. The option we want to get.
     */
    public function acau_get_settings( $index = false ) {
        $defaults = [ 'acau_setting_1' => 1000,'acau_setting_2' => true ];
        $settings = get_option( 'acau_settings', $defaults );
        $settings = wp_parse_args( $settings, $defaults );

        if ( $index && isset( $settings[ $index ] ) ) {
            return $settings[ $index ];
        }

        return $settings;
    }

    public function create_settings_page()
    {      
        $this->options = $this->acau_get_settings();        

        ?>
        <div class="wrap">
            <h1>My Plugin Name</h1>
            <form method="post" action="options.php">
            <?php
                // This prints out all hidden setting fields
                settings_fields( 'acau_settings' );
                do_settings_sections( $this->settings_page_name );
                submit_button();
            ?>
            </form>
        </div>
        <?php
    }

    function add_settings_link( $links ) {
        $links = array_merge( array(
            '<a href="' . esc_url( admin_url( '/options-general.php?page=' . $this->settings_page_name ) ) . '">' . __( 'Settings' ) . '</a>'
        ), $links );
        return $links;
    }

    /**
     * Sanitize each setting field as needed
     *
     * @param array $input Contains all settings fields as array keys
     */
    public function sanitize( $input )
    {
        $new_input = array();
        if( isset( $input['acau_setting_1'] ) )
            $new_input['acau_setting_1'] = absint( $input['acau_setting_1'] );
        if( isset( $input['acau_setting_2'] ) )
            $new_input['acau_setting_2'] = ( $input['acau_setting_2'] == 1 ? 1 : 0 );           

        return $new_input;
    }

    /** 
     * Print the Section text
     */

    public function print_section_info()
    {
        return;
        // print 'Enter your settings below:';
    }

    /** 
     * Get the settings option array and print one of its values
     */
    public function acau_setting_1_callback()
    {
        printf(
            '<input type="text" id="acau_setting_1" name="acau_settings[acau_setting_1]" value="%1$s" />
            <p class="description">%2$s</p>',
            isset( $this->options['acau_setting_1'] ) ? esc_attr( $this->options['acau_setting_1']) : '',
            __( 'Irrelevant setting 1 description.', 'my-plugin-slug' )
        );
    }

    public function acau_setting_2_callback() {
        printf(
        '<fieldset>
            <label><input id="acau_setting_2" type="checkbox" name="acau_settings[acau_setting_2]" value="1" %1$s />%2$s</label>
        </fieldset>',
        isset( $this->options['acau_setting_2'] ) && ( 1 == $this->options['acau_setting_2'] )  ? 'checked="checked" ':'',
        __( 'Change minimum product quantity on cart page from 0 to 1.', 'my-plugin-slug' )     
        );
    }

}

if( is_admin() )    
    $my_settings_page = new my_plugin_name_settings();

// Only if WooCommerce is active.
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) || ( get_site_option('active_sitewide_plugins') && array_key_exists( 'woocommerce/woocommerce.php', get_site_option('active_sitewide_plugins') ) ) ) {

    // Run plugin.
    add_action( 'template_redirect', 'run_plugin' );
    function run_plugin() {

        if (! is_cart() ) return; // Only if it's cart page.

        // Enqueue CSS and js file. Localize to use php variables from plugin settings.
        add_action( 'wp_enqueue_scripts', 'my_plugin_name' );
        function my_plugin_name( ) {

            $plugin_abbv_name = 'acau';
            $plugin_slug = 'my-plugin-slug-for-woocommerce';
            $plugin_short_slug = 'my-plugin-slug';

            wp_enqueue_style( $plugin_short_slug . '-style', plugins_url() . '/' . $plugin_slug . '/css/' . $plugin_slug . '.css');
            wp_enqueue_script( $plugin_short_slug . '-script', plugins_url() . '/' . $plugin_slug . '/js/' . $plugin_slug . '.js', '', array('jquery'));

            $acau_settings_page = new my_plugin_name_settings();

            // js variable name => variable value
            $data = array (
                'acau_setting_1' => $acau_settings_page->acau_get_settings('acau_setting_1'),               
            );      

            wp_localize_script( $plugin_short_slug . '-script', 'phpVars', $data );

            // Cart page minimum qty = 1 instead of 0.              
            if (1 == $acau_settings_page->acau_get_settings('acau_setting_2') ) {               
                add_filter( 'woocommerce_quantity_input_args', 'custom_cart_min_qty', 10, 2 );
                function custom_cart_min_qty( $args, $product ) {
                   $args['min_value'] = 1;
                   return $args;
                }
            }       

        }   
    }
}

?>
1

Don't define functions within functions. function run_plugin(), function my_plugin_name(), and custom_cart_min_qty(). This is not how you should write this code. Your add_action() and add_filter hooks already call these functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.