Home Forums WoodMart support forum Help with extending a theme’s Class

Help with extending a theme’s Class

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #435659

    [email protected]
    Participant

    Hello,

    I would like to extend the functionality of your free shipping progress bar in a way that it would be hidden for specific user roles.

    I’ve found that the layout for the shipping progress bar is generated in the class-main.php file located here: “woodmart/inc/integrations/woocommerce/modules/shipping-progress-bar/class-main.php“.

    What I need to be done is simply to check if the current user is a b2b customer and if so, I don’t want the shipping progress bar to be rendered, because I have free shipping disabled for this user type.

    The code is the following:

    
    $current_user = wp_get_current_user();
    		
    $disabledFreeShippingRoles = array('b2b_customer','b2b_customer_p_de_aucar');
    		
    	foreach( $disabledFreeShippingRoles as $role ) {
    		 if( in_array( $role, $current_user->roles) ) {
    		     return;
    		 }
    	}
    

    I added the same code to the following functions in the class-main.php file:

    output_shipping_progress_bar();
    output_shipping_progress_bar_in_mini_cart()
    render_shipping_progress_bar();

    If I keep this modification in the main theme files, everything works as expected, but when I try to copy the same file to the same folder structure in the child theme, it doesn’t work.

    I understand that this might be out of your support scope, but perhaps you can give me a hint on how I can have this modification in the child theme so that I can safely update the main theme without having to re-do all these modifications?

    Thanks

    #435768

    Luke Nielsen
    Keymaster

    Hello,

    That file cannot be overridden via child theme so better to use the below code to make such changes as you want. Just enter it into the functions.php file in your child theme.

    use XTS\Modules\Layouts\Main as Builder;
    use  XTS\Modules\Shipping_Progress_Bar\Main as Shipping_Progress;
    add_action( 'init', 'wd_update_shipping_bar' );
    function wd_update_shipping_bar() {
    	remove_action( 'wp', array( Shipping_Progress::get_instance(), 'output_shipping_progress_bar' ), 100 );
    	remove_action( 'init', array( Shipping_Progress::get_instance(), 'output_shipping_progress_bar_in_mini_cart' ), 100 );
    
    	add_action( 'wp', 'wd_output_shipping_progress_bar', 100 );
    	add_action( 'init', 'wd_output_shipping_progress_bar_in_mini_cart', 100 );
    }
    
    function wd_output_shipping_progress_bar() {
    	if ( ! woodmart_get_opt( 'shipping_progress_bar_enabled' ) ) {
    		return;
    	}
    
    	$current_user = wp_get_current_user();
    
    	$disabledFreeShippingRoles = array('b2b_customer','b2b_customer_p_de_aucar');
    
    	foreach( $disabledFreeShippingRoles as $role ) {
    		if( in_array( $role, $current_user->roles) ) {
    			return;
    		}
    	}
    
    	if ( woodmart_get_opt( 'shipping_progress_bar_location_card_page' ) && ! Builder::get_instance()->has_custom_layout( 'cart' ) ) {
    		add_action( 'woocommerce_before_cart_table', array( Shipping_Progress::get_instance(), 'render_shipping_progress_bar_with_wrapper' ) );
    	}
    
    	if ( woodmart_get_opt( 'shipping_progress_bar_location_single_product' ) ) {
    		add_action( 'woocommerce_single_product_summary', array( Shipping_Progress::get_instance(), 'render_shipping_progress_bar_with_wrapper' ), 29 );
    	}
    
    	if ( woodmart_get_opt( 'shipping_progress_bar_location_checkout' ) ) {
    		add_action( 'woocommerce_checkout_before_customer_details', array( Shipping_Progress::get_instance(), 'render_shipping_progress_bar_with_wrapper' ) );
    	}
    }
    
    /**
     * Update fragments shipping progress bar.
     *
     * @return void
     */
    	function wd_output_shipping_progress_bar_in_mini_cart() {
    	if ( ! woodmart_get_opt( 'shipping_progress_bar_enabled' ) ) {
    		return;
    	}
    
    		$current_user = wp_get_current_user();
    
    		$disabledFreeShippingRoles = array('b2b_customer','b2b_customer_p_de_aucar');
    
    		foreach( $disabledFreeShippingRoles as $role ) {
    			if( in_array( $role, $current_user->roles) ) {
    				return;
    			}
    		}
    
    	if ( woodmart_get_opt( 'shipping_progress_bar_location_mini_cart' ) ) {
    		add_action( 'woocommerce_widget_shopping_cart_before_buttons', array( Shipping_Progress::get_instance(), 'render_shipping_progress_bar' ) );
    	}
    
    	add_filter( 'woocommerce_add_to_cart_fragments', array( Shipping_Progress::get_instance(), 'get_shipping_progress_bar_fragments' ), 40 );
    	add_filter( 'woocommerce_update_order_review_fragments', array( Shipping_Progress::get_instance(), 'get_shipping_progress_bar_checkout_fragments' ), 10 );
    }

    Kind Regards

    #436004

    [email protected]
    Participant

    Excellent! Thank you very much for your suggestion.

    #436006

    Luke Nielsen
    Keymaster

    Hello,

    You are welcome!

    Always remember that you can reach out to us with any questions you may have.

    We wish you a splendid day!

    Kind Regards

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Help with extending a theme’s Class’ is closed to new replies.