woocommerce_logo1

Ok, lets start from creating  user interface  through customizer API. Hope you guys know about Customizer setting API API of WordPress.

This is how i build user interface to choose number of related products on theme.

First add this code inside “customize_register” hooks.

//create section  for woocommerce setting

$wp_customize->add_section( ‘woocommerce_settings’, array(

‘title’ => __( ‘Woocommerce Settings’, ‘theme_textdomain’ ),
‘priority’ => 40,
‘panel’ => ‘woo_set’,
) );

//setting to display number of related products
$wp_customize->add_setting( ‘woocommerce_products_num’, array(
‘default’ => ‘4’,
‘sanitize_callback’ => ‘theme_sanitize_text’,
) );

//setting to display number of related products
$wp_customize->add_setting( ‘woocommerce_products_num’, array(
‘default’ => ‘4’,
‘sanitize_callback’ => ‘Theme_sanitize_text’,
) );
$wp_customize->add_control( ‘woocommerce_products_num’, array(
‘label’ => __( ‘Number of Related products In product page’, ‘theme_textdomain’ ),
‘section’ => ‘woocommerce_settings’,
‘settings’ => ‘woocommerce_products_num’,
‘type’ => ‘select’,
‘choices’ => array(
‘1’ => ‘1’,
‘2’ => ‘2’,
‘3’ => ‘3’,
‘4’ => ‘4’,
‘5’ => ‘5’,
‘6’ => ‘6’,
‘7’ => ‘7’,
‘8’ => ‘8’,
‘9’ => ‘9’,
),
‘priority’ => 1,
) );

This above code will create multiple select option on backend.  To change number of related products i had used “woocommerce_output_related_products_args” filter hook.

Here is the example of it:

/**
* related products in woocommerce
*/
add_filter( ‘woocommerce_output_related_products_args’, ‘jk_related_products_argsS’ );

function jk_related_products_argSs( $args ) {
$related_product = get_theme_mod(‘woocommerce_products_num’,’4′);
$no_of_rows = $related_product/3;

if($no_of_rows>1 && $no_of_rows<=2) { $args[‘posts_per_page’] = get_theme_mod(‘woocommerce_products_num’); $args[‘rows’] = 2; } elseif ($no_of_rows >= 3 || $no_of_rows>2) {
$args[‘posts_per_page’] = get_theme_mod(‘woocommerce_products_num’);
$args[‘rows’] = 3;
} else {
$args[‘posts_per_page’] = get_theme_mod(‘woocommerce_products_num’);
$args[‘rows’] = 1;
}
return $args;
}

Finally you will be able to change number of related products on products page.

If you got any problems on this please feel free to contact me. And  also if you have effective ideas than this , then please let me know

Thanks

sushil adhikari

Advertisements