0

In my site I'm using the Product Archive Customizer plugin for WooCommerce. Great plugin. However, there is an option to set the qty of products shown per page. The next piece of code is responsible for that:

$wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'wc_pac_products_per_page', array(
                'label'    => __( 'Products per page', 'woocommerce-product-archive-customiser' ),
                'section'  => 'wc_pac',
                'settings' => 'wc_pac_products_per_page',
                'type'     => 'select',
                'choices'  => array(
                                '2'  => '2',
                                '3'  => '3',
                                ----------
                                '23' => '23',
                                '24' => '24',

                ),
            ) ) );

I do want to add another qty to the 'choices' array and I'm told it is bad practice to add that in the original plugin file. In that case it will be overwritten with an update of the plugin.

I read up on a lot of array related topics but I can't get it under my thick skull.

What code do I have to add to my themes functions.php file to achieve that?

1

I tried another solution and that did the trick. I found in the WooCommerce docs this little snippet:

// Display 24 products per page. Goes in functions.php add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 24;' ), 20 );.

It is overriding the settings in the customizer. I only have to change the number '24' to my liking.

0

I don't know what hook Woocommerce uses to add that customizer code, but in your functions.php you'll have to hook your own function on a later time, like this (the last number is the lower priority):

add_action ('the_name_of_that_hook','wpse_280836_mycontrol', 99)

Now in the function you are hooking, you must first remove the existing control and the add your own:

function wpse_280836_mycontrol () {
  $wp_customize->remove_control('wc_pac_products_per_page');
  $wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'wc_pac_products_per_page', array(
            'label'    => __( 'Products per page', 'woocommerce-product-archive-customiser' ),
            'section'  => 'wc_pac',
            'settings' => 'wc_pac_products_per_page',
            'type'     => 'select',
            'choices'  => array(
                            'mystuff'  => 'myotherstuff',
                            '3'  => '3',
                            ----------
                            '23' => '23',
                            '24' => '24',

            ),
        ) ) );

Please note, that I don't know what this control does, because I'm not a Woocommerce specialist. Somewhere else in the code there may be a check that only allows the given choices. That would make your modification invalid.

  • Thanks. This control lets me set the number of products displayed on a page. In the complete code the options are 1 - 24 and I want to add some! I'm trying your suggestion but can't get it to work yet. As it is a plugin which is used in the themes customizer I try to use de hook customize_register. That gives me a 500 error. I tried to find another hook but I'm stuck. Any suggestions? There is no possibility to address the choices array directly to add some values? – A3O Jun 28 '16 at 10:40
  • You could try using get_control, but you would still have to hook that somewhere. – cjbj Jun 28 '16 at 11:09
  • I tried another solution and that did the trick. I found in the WooCommerce docs this little snippet: // Display 24 products per page. Goes in functions.php add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 24;' ), 20 );. It is overriding the settings in the customizer. I only have to change the number '24' to my liking. Anyway, thanks for your help. – A3O Jun 28 '16 at 12:54
  • Please add your solution as an answer to this question and accept it. So this question does not turn up later as 'unsolved'. – cjbj Jun 28 '16 at 13:17

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.