0

I'm creating a plugin which will add "-" and "+" increment buttons around quantity input field. I left some essential lines to more clearly present the problem.

I created class qty_increment_buttons_settings with 2 settings in admin, numeric value and checkbox, using settings API. This numeric value is passed as a variable to js file. I wrote similar plugin before and it worked, but here it's more complex - I would like to use this new instance of class in my other functions, for example within qib_create_button to access checkbox value, through public function qib_get_settings defined within this class.

Also, is there any way I could specify true and false within add_action hooks, so I don't need to create 2 extra functions?

add_action( 'template_redirect', 'run_qib' );
function run_qib() {
    if (! is_cart() && ! is_product()) return; // Only if it's product or cart page.
    add_action( 'wp_enqueue_scripts', 'qty_increment_buttons' );
}

function qty_increment_buttons( ) {     

    $plugin_slug = 'qty-increment-buttons-for-woocommerce';
    $plugin_short_slug = 'qty-increment-buttons';

    add_action('wp_head', 'qib_apply_styles', 20);  
    wp_enqueue_script( $plugin_short_slug, plugins_url() . '/' . $plugin_slug . '/js/' . $plugin_slug . '.js', array('jquery'), '', true);

    $qib_settings_page = new qty_increment_buttons_settings();  

    add_action( 'woocommerce_before_add_to_cart_quantity', 'qib_create_button_before' );        
    add_action( 'woocommerce_after_add_to_cart_quantity', 'qib_create_button_after' );

    // js variable name => variable value       
    $data = array (
        'qib_button_size' => $qib_settings_page->qib_get_settings('qib_button_size'),               
    );

    wp_localize_script( $plugin_short_slug, 'phpVars', $data ); 

}

function qib_create_button_before() {           
    echo qib_create_button( true );
}

function qib_create_button_after() {
    echo qib_create_button( false );
}

function qib_create_button( $is_before ) {  
    $sign = ( 1 == $qib_settings_page->qib_get_settings('qib_minus_first') && $is_before === true ? '-' : '+' );
    return sprintf('<button type="button" class="%1$s" >%2$s</button>', $sign === '-' ? 'minus' : 'plus', $sign );  
}
0

I found a solution to this. In my other project I use this function in my settings class:

public function lal_get_settings( $index = false ) {

    $defaults = array ( 'lal_display_type' => 'icon', 'lal_add_fa' => false);
    $settings = get_option( 'lal_settingz', $defaults );

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

    return $settings;
}

Then outside the class in the main php file:

$args = array (
        'display_type' =>  $lal_settingz_page->lal_get_settings('lal_display_type'),
        'add_fa' =>  $lal_settingz_page->lal_get_settings('lal_add_fa')
);

Whenever I want to pass these settings as arguments, I use the following construction:

add_action('wp_enqueue_scripts', function() use ( $args ) { login_account_logout( $args ); }, 1);
function login_account_logout( $args ) {
if ( $args['display_type'] === 'icon' && $args['add_fa'] == 1 )
    wp_enqueue_style('lal-CDN-font-awesome', '//use.fontawesome.com/releases/v5.8.2/css/all.css');
}

I can pass these arguments further, to nested actions in the same way.

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.