Jump to content

Hide specific shipping methods for specific roles


Lyse

Recommended Posts

I use the Wholesale Suite Premium Prices plugin with WooCommerce. I have 6 specific wholesale roles out of 15 that I wish to hide two specific shipping methods from being selected for the 6 exceptions. I'm just trying this on my staging server at this time using a code snippet example that I found and modified for my specific conditions. Would the following work for this purpose?

/* Hide specific shipping methods for specific wholesale roles */
add_filter( 'woocommerce_package_rates', function( $shipping_rates ) {
// User role and shipping method ID to hide for the user role
    $role_shipping_method_arr = array(
    'ws_silvia_silver' => array( 'Silvia Premium Standard Shipping (Tracking Service)'),
    'ws_silvia_silver_pst_exempt' => array( 'Silvia Premium Standard Shipping (Tracking Service)'),
    'ws_silvia_silver_tax_exempt' => array( 'Silvia Premium Standard Shipping (Tracking Service)'),
    'ws_silvia_silver' => array( 'Silvia Union Standard Shipping (Tracking Service)'),
    'ws_silvia_silver_pst_exempt' => array( 'Silvia Union Standard Shipping (Tracking Service)'),
    'ws_silvia_silver_tax_exempt' => array( 'Silvia Union Standard Shipping (Tracking Service)'),
    'ws_silvia_gold' =>    array( 'Silvia Premium Standard Shipping (Tracking Service)'),
    'ws_silvia_gold_pst_exempt' => array( 'Silvia Premium Standard Shipping (Tracking Service)'),
    'ws_silvia_gold_tax_exempt' => array( 'Silvia Premium Standard Shipping (Tracking Service)'),
    'ws_silvia_gold' =>    array( 'Silvia Union Standard Shipping (Tracking Service)'),
    'ws_silvia_gold_pst_exempt' => array( 'Silvia Union Standard Shipping (Tracking Service)'),
    'ws_silvia_gold_tax_exempt' => array( 'Silvia Union Standard Shipping (Tracking Service)'),
    );

    // Getting the current user role
    $curr_user = wp_get_current_user(); 
    $curr_user_data = get_userdata($current_user->ID); 

    // Wholesale Suite Roles 
    if (isset($current_user) && class_exists('WWP_Wholesale_Roles')) {
        $wwp_wholesale_roles = WWP_Wholesale_Roles::getInstance();
        $wwp_wholesale_role = $wwp_wholesale_roles->getUserWholesaleRole(); 

// Loop through the user role and shipping method pair
    foreach( $role_shipping_method_arr as $role => $shipping_methods_to_hide ) {

// Check if defined role exist in current user role or not
    if( in_array( $role, $current_user->roles) ) {

// Loop through all the shipping rates
    foreach( $shipping_rates as $shipping_method_key => $shipping_method ) {
$shipping_id = $shipping_method->get_id();

// Unset the shipping method if found
    if( in_array( $shipping_id, $shipping_methods_to_hide) ) {
unset($shipping_rates[$shipping_method_key]);
}
}
}
}
}
    return $shipping_rates;
});

Any insights as to how to accomplish this would be greatly appreciated. Lyse

Link to comment
Share on other sites

Isn't your array faulty?  You have multiple entries with the same index such as "ws_silvia_silver".  Only the last one will exist once you have created this array.  And why the secondary array to hold the "title" values?    As for the original question, I leave that to people with experience in your plugin.

Link to comment
Share on other sites

I use both retail and wholesale on my website. As part of the wholesale, because I live in Canada and have two taxes that can also be exempted, so I end up with 15 wholesale roles because of taxes. Of these 15 wholesale roles, 6 of them get special shipping methods that offers reduced flat rate shipping. The 6 wholesale roles that get reduced flat rate shipping are the wholesale membership levels: ws_silvia_premium, ws_silvia_premium_PST_exempt, ws_silvia_premium_tax_exempt,  ws_silvia_union, ws_silvia_union_PST_exempt, ws_silvia_union_tax_exempt.  I want to be able to hide the special shipping methods for the remaining wholesale roles (which I included in the code). The other option could be to only show the special shipping methods to only those 6 roles.  Hope this clarifies what I'm trying to do.

Link to comment
Share on other sites

Not quite sure but let me give you this to think about.  Why not use two static arrays and choose one or the other when you first find out what kind of customer you are working with?  Copy the correct static array to your "shipping" array and use that one only in your code all the time for this customer.

Link to comment
Share on other sites

Take the array you already built and break it down into two separate ones.  Then use a 3rd array to hold your chosen set of data from 1 of the two separate ones.  That's the easy way.  OTOH - if you knew more about array handling you could use a multi-dimensional array to hold the two arrays in one array and simply use the correct index to only reference the set of data in that side of the array.

$arr['retail'][key] = value

$arr['retail'][key] = value;

$arr['wholesale'][key] = value;

$arr['wholesale'][key] = value;

Link to comment
Share on other sites

So my first two arrays would look like this"

$arr['retail']
	[customer] = Silvia Premium Standard Shipping (Tracking Service), Silvia Union Standard Shipping (Tracking Service); 
	[customer_pst_exempt] = Silvia Premium Standard Shipping (Tracking Service), Silvia Union Standard Shipping (Tracking Service);
    [customer_tax_exempt] = Silvia Premium Standard Shipping (Tracking Service), Silvia Union Standard Shipping (Tracking Service);
    [ws_silvia_gold] = Silvia Premium Standard Shipping (Tracking Service), Silvia Union Standard Shipping (Tracking Service);
    [ws_silvia_gold_pst_exempt] = Silvia Premium Standard Shipping (Tracking Service), Silvia Union Standard Shipping (Tracking Service);
    [ws_silvia_gold_tax_exempt] = Silvia Premium Standard Shipping (Tracking Service), Silvia Union Standard Shipping (Tracking Service);

$arr['wholesale']
	[wholesale_customer] = Silvia Premium Standard Shipping (Tracking Service), Silvia Union Standard Shipping (Tracking Service); 
	[wholesale_pst_exempt] = Silvia Premium Standard Shipping (Tracking Service), Silvia Union Standard Shipping (Tracking Service);
    [wholesale_tax_exempt] = Silvia Premium Standard Shipping (Tracking Service), Silvia Union Standard Shipping (Tracking Service);
	[ws_silvia_silver] = Silvia Premium Standard Shipping (Tracking Service), Silvia Union Standard Shipping (Tracking Service); 
	[ws_silvia_silver_pst_exempt] = Silvia Premium Standard Shipping (Tracking Service), Silvia Union Standard Shipping (Tracking Service);
    [ws_silvia_silver_tax_exempt] = Silvia Premium Standard Shipping (Tracking Service), Silvia Union Standard Shipping (Tracking Service);
    [ws_silvia_gold] = Silvia Premium Standard Shipping (Tracking Service), Silvia Union Standard Shipping (Tracking Service);
    [ws_silvia_gold_pst_exempt] = Silvia Premium Standard Shipping (Tracking Service), Silvia Union Standard Shipping (Tracking Service);
    [ws_silvia_gold_tax_exempt] = Silvia Premium Standard Shipping (Tracking Service), Silvia Union Standard Shipping (Tracking Service);

 

Edited by Lyse
Link to comment
Share on other sites

No.

You need quotes on all string values.  That means the indices (ie, 'customer') and the values. And - why are you showing a comma-separated value in each array element?  What are they?

OTOH  - you have the idea I believe.  Now you have one array that contains two sub-arrays and you will only reference one of them when dealing with a single customer/user.

Link to comment
Share on other sites

So I've added the quotes for the string values.  I show two shipping methods as values for each key customer.  Here's the updated code:

$arr['retail']
	["customer"] = "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)"; 
	["customer_pst_exempt"] = "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)";
    ["customer_tax_exempt"] = "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)";
    
$arr['wholesale']
	["wholesale_customer"] = "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)"; 
	["wholesale_pst_exempt"] = "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)";
    ["wholesale_tax_exempt"] = "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)";
	["ws_silvia_silver"] = "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)"; 
	["ws_silvia_silver_pst_exempt"] = "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)";
    ["ws_silvia_silver_tax_exempt"] = "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)";
    ["ws_silvia_gold"] = "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)";
    ["ws_silvia_gold_pst_exempt"] = "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)";
    ["ws_silvia_gold_tax_exempt"] = "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)";

 

Link to comment
Share on other sites

Well, you can't concatenate those values into one array element.  You have to make the 'value' an array of its own as you were doing before.

As in:

$arr['wholesale']
	["wholesale_customer"] = array(
				"Silvia Premium Standard Shipping (Tracking Service)", 
				"Silvia Union Standard Shipping (Tracking Service)"
				); 

And you would reference one of these as $arr['wholesale']['wholesale_customer'][0] or .....[1].

Edited by ginerjm
Link to comment
Share on other sites

Here's how I understood your last comments:

    $role_shipping_method_arr = array(
        $arr['retail']
        ["customer"][0] = "Silvia Premium Standard Shipping (Tracking Service)"[1];
        ["customer_pst_exempt"][0] = "Silvia Premium Standard Shipping (Tracking Service)"[1];
        ["customer_tax_exempt"] = "Silvia Premium Standard Shipping (Tracking Service)";
        ["customer"][0] = "Silvia Union Standard Shipping (Tracking Service)"[1];
        ["customer_pst_exempt"][0] = "Silvia Union Standard Shipping (Tracking Service)"[1];
        ["customer_tax_exempt"[0]] = "Silvia Union Standard Shipping (Tracking Service)"[1];
                        
    $arr['wholesale']
        ["wholesale_customer"][0] = "Silvia Premium Standard Shipping (Tracking Service)"[1]; 
        ["wholesale_pst_exempt"][0] = "Silvia Premium Standard Shipping (Tracking Service)"[1];
        ["wholesale_tax_exempt"][0] = "Silvia Premium Standard Shipping (Tracking Service)"[1];
        ["ws_silvia_silver"][0] = "Silvia Premium Standard Shipping (Tracking Service)"[1]; 
        ["ws_silvia_silver_pst_exempt"][0] = "Silvia Premium Standard Shipping (Tracking Service)"[1];
        ["ws_silvia_silver_tax_exempt"][0] = "Silvia Premium Standard Shipping (Tracking Service)"[1];
        ["ws_silvia_gold"][0] = "Silvia Premium Standard Shipping (Tracking Service)"[1];
        ["ws_silvia_gold_pst_exempt"][0] = "Silvia Premium Standard Shipping (Tracking Service)"[1];
        ["ws_silvia_gold_tax_exempt"][0] = "Silvia Premium Standard Shipping (Tracking Service)"[1];
        ["wholesale_customer"][0] = "Silvia Union Standard Shipping (Tracking Service)"[1]; 
        ["wholesale_pst_exempt"][0] = "Silvia Union Standard Shipping (Tracking Service)"[1];
        ["wholesale_tax_exempt"][0] = "Silvia Union Standard Shipping (Tracking Service)"[1];
        ["ws_silvia_silver"][0] = "Silvia Union Standard Shipping (Tracking Service)"[1]; 
        ["ws_silvia_silver_pst_exempt"][0] = Silvia Union Standard Shipping (Tracking Service)"[1];
        ["ws_silvia_silver_tax_exempt"][0] = "Silvia Union Standard Shipping (Tracking Service)"[1];
        ["ws_silvia_gold"][0] = "Silvia Union Standard Shipping (Tracking Service)"[1];
        ["ws_silvia_gold_pst_exempt"][0] = "Silvia Union Standard Shipping (Tracking Service)"[1];
        ["ws_silvia_gold_tax_exempt"][0] = "Silvia Union Standard Shipping (Tracking Service)"[1];

 

Link to comment
Share on other sites

You don't understand arrays at all, do you?

You can't have the same key twice.  One will replace the other. Does that make sense.

You should have only modified your code in the fashion that I showed you using one element as an example.  

Link to comment
Share on other sites

It makes a lot of sense not to duplicate keys... So like this...

    $role_shipping_method_arr = array(
        $arr['retail']
        ["customer"] array(
         "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)"); 
        ["customer_pst_exempt"] array(
         "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)"); 
        ["customer_tax_exempt"] array(
         "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)"); 
                        
    	$arr['wholesale']
        ["wholesale_customer"] = array(
         "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)"); 
        ["wholesale_pst_exempt"] array(
         "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)"); 
        ["wholesale_tax_exempt"] array(
         "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)"); 
        ["ws_silvia_silver"][0] array(
         "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)");  
        ["ws_silvia_silver_pst_exempt"] array(
         "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)"); ;
        ["ws_silvia_silver_tax_exempt"] array(
         "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)"); 
        ["ws_silvia_gold"] array(
         "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)"); 
        ["ws_silvia_gold_pst_exempt"] array(
         "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)"); 
        ["ws_silvia_gold_tax_exempt"] array(
         "Silvia Premium Standard Shipping (Tracking Service)", "Silvia Union Standard Shipping (Tracking Service)");

 

Link to comment
Share on other sites

So close.... 

Look closely at your code.  In PHP the semi marks the end of a line of code.  I see now that I did it to you so this is the correction, in pseudo-code.

$arr = array(
	['wholesale'] = array
	(
      ['wholesale_customer'] = array('ccccc','ddddd'),
      ['wholesale_pst_exempt'] = array('xxxx','yyyy'),
      ..
      ...
      ...
	),
	['retail'] = array
	(
		...,
		...,
	)
)

 

Link to comment
Share on other sites

So this is now what my arrays structure looks like:

    $role_shipping_method_arr = array(
        ['retail'] = array 
        (
        	['customer'] = array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)', 
        	['customer_pst_exempt'] array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)', 
        	['customer_tax_exempt'] array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)'
        ), 
                        
    	['wholesale'] = array 
        (
        	['wholesale_customer'] = array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)', 
        	['wholesale_pst_exempt'] = array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)', 
        	['wholesale_tax_exempt'] array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)', 
        	['ws_silvia_silver'] array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)',  
        	['ws_silvia_silver_pst_exempt'] array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)',
        	['ws_silvia_silver_tax_exempt'] array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)', 
        	['ws_silvia_gold'] array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)', 
        	['ws_silvia_gold_pst_exempt'] array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)',
        	['ws_silvia_gold_tax_exempt'] array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)'
        ); 

 

Edited by Lyse
Link to comment
Share on other sites

Think I've got them all now....

 

$role_shipping_method_arr = array(
        ['retail'] = array 
        (
        	['customer'] = array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)', 
        	['customer_pst_exempt'] = array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)', 
        	['customer_tax_exempt'] = array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)'
        ), 
                        
    	['wholesale'] = array 
        (
        	['wholesale_customer'] = array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)', 
        	['wholesale_pst_exempt'] = array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)', 
        	['wholesale_tax_exempt'] = array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)', 
        	['ws_silvia_silver'] = array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)',  
        	['ws_silvia_silver_pst_exempt'] = array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)',
        	['ws_silvia_silver_tax_exempt'] = array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)', 
        	['ws_silvia_gold'] = array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)', 
        	['ws_silvia_gold_pst_exempt'] = array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)',
        	['ws_silvia_gold_tax_exempt'] = array('Silvia Premium Standard Shipping (Tracking Service)', 'Silvia Union Standard Shipping (Tracking Service)'
        )
		); 
        

 

 

Edited by Lyse
Link to comment
Share on other sites

Ok - I've had a long day and may have been mis-directing you with some of this.  So - I built a script to  test it all out and here it is.

<?php
$role_shipping_method_arr = array
(
	'retail' => array
	(
		'customer' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'customer_pst_exempt' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'customer_tax_exempt' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		)
	),
	'wholesale' => array
	(
		'wholesale_customer' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'wholesale_pst_exempt' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'wholesale_tax_exempt' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'ws_silvia_silver' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'ws_silvia_silver_pst_exempt' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'ws_silvia_silver_tax_exempt' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'ws_silvia_gold' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'ws_silvia_gold_pst_exempt' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'ws_silvia_gold_tax_exempt' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		)
	)
);
echo "Array is<pre>",print_r($role_shipping_method_arr,true),'</pre>';
exit;

Upload this EXACT script and run it and see what you get.

  • Great Answer 1
Link to comment
Share on other sites

This is what I get:

Array is

Array
(
    [retail] => Array
        (
            [customer] => Array
                (
                    [0] => Silvia Premium Standard Shipping (Tracking Service)
                    [1] => Silvia Union Standard Shipping (Tracking Service)
                )

            [customer_pst_exempt] => Array
                (
                    [0] => Silvia Premium Standard Shipping (Tracking Service)
                    [1] => Silvia Union Standard Shipping (Tracking Service)
                )

            [customer_tax_exempt] => Array
                (
                    [0] => Silvia Premium Standard Shipping (Tracking Service)
                    [1] => Silvia Union Standard Shipping (Tracking Service)
                )

        )

    [wholesale] => Array
        (
            [wholesale_customer] => Array
                (
                    [0] => Silvia Premium Standard Shipping (Tracking Service)
                    [1] => Silvia Union Standard Shipping (Tracking Service)
                )

            [wholesale_pst_exempt] => Array
                (
                    [0] => Silvia Premium Standard Shipping (Tracking Service)
                    [1] => Silvia Union Standard Shipping (Tracking Service)
                )

            [wholesale_tax_exempt] => Array
                (
                    [0] => Silvia Premium Standard Shipping (Tracking Service)
                    [1] => Silvia Union Standard Shipping (Tracking Service)
                )

            [ws_silvia_silver] => Array
                (
                    [0] => Silvia Premium Standard Shipping (Tracking Service)
                    [1] => Silvia Union Standard Shipping (Tracking Service)
                )

            [ws_silvia_silver_pst_exempt] => Array
                (
                    [0] => Silvia Premium Standard Shipping (Tracking Service)
                    [1] => Silvia Union Standard Shipping (Tracking Service)
                )

            [ws_silvia_silver_tax_exempt] => Array
                (
                    [0] => Silvia Premium Standard Shipping (Tracking Service)
                    [1] => Silvia Union Standard Shipping (Tracking Service)
                )

            [ws_silvia_gold] => Array
                (
                    [0] => Silvia Premium Standard Shipping (Tracking Service)
                    [1] => Silvia Union Standard Shipping (Tracking Service)
                )

            [ws_silvia_gold_pst_exempt] => Array
                (
                    [0] => Silvia Premium Standard Shipping (Tracking Service)
                    [1] => Silvia Union Standard Shipping (Tracking Service)
                )

            [ws_silvia_gold_tax_exempt] => Array
                (
                    [0] => Silvia Premium Standard Shipping (Tracking Service)
                    [1] => Silvia Union Standard Shipping (Tracking Service)
                )

        )

)
Link to comment
Share on other sites

I have add the array into my code snippet but get a Parse error: syntax error, unexpected '' (T_STRING), expecting '(' in /nas/content/staging/silviafindings/wp-content/themes/enfold-child/functions.php on line 655.  Line 655 is in the  customer array    'Silvia Premium Standard Shipping (Tracking Service)',

Here's the code snippet:

/* HIDE WHOLESALE PREMIUM & UNION SHIPPING METHODS FROM ALL OTHER ROLES */
/* Hide specific shipping methods for specific wholesale roles */
add_filter( 'woocommerce_package_rates', function( $shipping_rates ) {
// User role and shipping method ID to hide for the user role
$role_shipping_method_arr = array
(
	'retail' => array
	(
		'customer' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'customer_pst_exempt' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'customer_tax_exempt' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		)
	),
	'wholesale' => array
	(
		'wholesale_customer' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'wholesale_pst_exempt' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'wholesale_tax_exempt' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'ws_silvia_silver' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'ws_silvia_silver_pst_exempt' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'ws_silvia_silver_tax_exempt' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'ws_silvia_gold' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'ws_silvia_gold_pst_exempt' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		),
		'ws_silvia_gold_tax_exempt' => array
		(
			'Silvia Premium Standard Shipping (Tracking Service)',
			'Silvia Union Standard Shipping (Tracking Service)'
		)
	)
);  

    // Getting the current user role
    $curr_user = wp_get_current_user(); 
    $curr_user_data = get_userdata($current_user->ID); 

    // Wholesale Suite Roles 
    if (isset($current_user) && class_exists('WWP_Wholesale_Roles')) {
        $wwp_wholesale_roles = WWP_Wholesale_Roles::getInstance();
        $wwp_wholesale_role = $wwp_wholesale_roles->getUserWholesaleRole(); 

// Loop through the user role and shipping method pair
    foreach( $role_shipping_method_arr as $role => $shipping_methods_to_hide ) {

// Check if defined role exist in current user role or not
    if( in_array( $role, $current_user->roles) ) {

// Loop through all the shipping rates
    foreach( $shipping_rates as $shipping_method_key => $shipping_method ) {
$shipping_id = $shipping_method->get_id();

// Unset the shipping method if found
    if( in_array( $shipping_id, $shipping_methods_to_hide) ) {
unset($shipping_rates[$shipping_method_key]);
}
}
}
}
}
    return $shipping_rates;
});

 

Link to comment
Share on other sites

BTW - another fantastic style of coding is to use the TAB key a lot!!!  Makes is so much easier to read.

I prefer to place my braces on separate lines rather than at the end of a line (where they can be missed!) so this is how I write all of my code:

// Getting the current user role
$curr_user = wp_get_current_user(); 
$curr_user_data = get_userdata($current_user->ID); 
// Wholesale Suite Roles 
if (isset($current_user) && class_exists('WWP_Wholesale_Roles')) 
{
	$wwp_wholesale_roles = WWP_Wholesale_Roles::getInstance();
	$wwp_wholesale_role = $wwp_wholesale_roles->getUserWholesaleRole(); 
	// Loop through the user role and shipping method pair
	foreach( $role_shipping_method_arr as $role => $shipping_methods_to_hide ) 
	{
		// Check if defined role exist in current user role or not
		if( in_array( $role, $current_user->roles)) 
		{
			// Loop through all the shipping rates
			foreach( $shipping_rates as $shipping_method_key => $shipping_method ) 
			{
				$shipping_id = $shipping_method->get_id();
				// Unset the shipping method if found
				if( in_array( $shipping_id, $shipping_methods_to_hide) ) 
				{
					unset($shipping_rates[$shipping_method_key]);
				}
			}
		}
	}
}
return $shipping_rates;
}
);

Doesn't that make it easier to see where you are in that mess?

Of course having this whole thing embedded inside the call to "array_filter" doesn't make it any easier.  Can't you just define the function outside that WP call and reference it by name?  I don't use WP so I may be wrong.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.