Jump to content

Hide specific shipping methods for specific roles


Lyse

Recommended Posts

I do agree that it's much easier to see and follow the code when it's indented and outdented.  I used a code snippet from someone else to accomplish what I wanted and the fact that I'm not too proficient in PHP I do not date to change any thing, most particularly the structure. I have used your suggestion through this one and it's clearer now.

Here's the revised code snippet with line 655 commented out with the parse error.

/* 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
			/*Parse error: syntax error, unexpected '' (T_STRING), expecting '(' */
				'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

The testing was done after I removed the commented code. I simply recopied the arrays from the one that worked yesterday. I'm still no sure what "(" I was missing, but it works well now.

I get other parsing errors/notices about code following the arrays.

On my checkout page I see these two notices:

Notice: Undefined variable: current_user in /nas/content/staging/silviafindings/wp-content/themes/enfold-child/functions.php on line 723
Notice: Trying to get property 'ID' of non-object in /nas/content/staging/silviafindings/wp-content/themes/enfold-child/functions.php on line 723

In the PHP checker I see this:

PHP Syntax Check: Parse error: syntax error, unexpected '$wwp_wholesale_role' (T_VARIABLE) in your code on line...   for the line  $wwp_wholesale_role = $wwp_wholesale_roles->getUserWholesaleRole();

 

Link to comment
Share on other sites

The last topic you posted had a similar set of convoluted arrays for tax rates. Now you are trying to handle shipping methods by the same method.

Why doesn't your database reflect these rates and methods for each customer role so you can easily query for them.

Whaat is your data structure?

Link to comment
Share on other sites

In Canada we do have a complex tax system. Both WooCommerce and The Wholesale Suite Prices plugin do not integrate that complexity. So special tax rate tables and supporting functions had to be created to work around taxes and their exemptions per customer role. The shipping methods case is as a result of my client offering reduced flat shipping rates for wholesale customers only reaching membership purchasing levels. The Wholesale plugin allows mapping of the shipping methods by roles, however I would have to map all of the existing shipping methods for each wholesale role (15 * 7 shipping methods). The developer won't help with creating a function to do this more efficiently.

To create the rates and methods for each customer would require development that I can't do myself. So I revert to using functions to work around the specific requirements.

The data structure is MYSQL with tables.

 

Link to comment
Share on other sites

21 hours ago, Lyse said:

The data structure is MYSQL with tables.

That tells me absolutely nothing about your data structure. How do expect help with responses like that when we ask for more information?

A table structure such as this one would solve the problems in both your topics

+----------------+               +-------------------+                                   +---------------------+
| customer       |               |  role             |                                   | shipping_method     |
+----------------+               +-------------------+                                   +---------------------+
| cust_id        |       +-------| role_id           |--+                            +---| method_id           |
| cust_name      |       |       | role_name         |  |                            |   | method_description  | 
| role_id        |>------+       | PST_payable       |  |                            |   | tracking_available  |
| ...            |               | GST_payable       |  |                            |   +---------------------+
+----------------+               +-------------------+  |                            |
                                                        |                            |
                                                        |   +---------------------+  |
                                                        |   |  role_method        |  |
                                                        |   +---------------------+  |
                                                        |   |  role_method_id     |  |
                                                        +--<|  role_id            |  |
                                                            |  method_id          |>-+
                                                            +---------------------+

If, for example, your table data looks like this

customer                                                                        shipping_method
+---------+------------+---------+                                              +-----------+--------------------+--------------------+
| cust_id | cust_name  | role_id |                                              | method_id | method_description | tracking_available |
+---------+------------+---------+                                              +-----------+--------------------+--------------------+
|       1 | Customer A |       1 |                                              |         1 | Carrier pigeon     |                  0 |
|       2 | Customer B |       2 |                                              |         2 | Pony Express       |                  0 |
|       3 | Customer C |       3 |                                              |         3 | Drone              |                  1 |
|       4 | Customer D |      17 |                                              |         4 | Silvia Standard    |                  1 |
|       5 | Customer E |      15 |                                              |         5 | Silvia Premium     |                  1 |
|       6 | Customer F |      11 |                                              +-----------+--------------------+--------------------+
|       7 | Customer G |      18 |                                              
|       8 | Customer H |      17 |
|       9 | Customer I |       4 |                                              role_method
|      10 | Customer J |      10 |                                              +----------------+---------+-----------+
+---------+------------+---------+                                              | role_method_id | role_id | method_id |
                                                                                +----------------+---------+-----------+
                                                                                |              1 |       1 |         1 |
role                                                                            |              2 |       1 |         2 |
+---------+-------------------------------------+-------------+-------------+   |              3 |       1 |         3 |
| role_id | role_name                           | pst_payable | gst_payable |   |              4 |       2 |         1 |
+---------+-------------------------------------+-------------+-------------+   |              5 |       2 |         2 |
|       1 | Customer                            |           1 |           1 |   |              6 |       2 |         3 |
|       2 | Customer pst exempt                 |           0 |           1 |   |              7 |       3 |         1 |
|       3 | Customer tax exempt                 |           0 |           0 |   |              8 |       3 |         2 |
|       4 | Wholesale Customer                  |           1 |           1 |   |              9 |       3 |         3 |
|       5 | Wholesale Silvia Silver             |           1 |           1 |   |             10 |       4 |         1 |
|       6 | Wholesale Silvia Gold               |           1 |           1 |   |             11 |       4 |         2 |
|       7 | Wholesale Silvia Premium            |           1 |           1 |   |             12 |       4 |         3 |
|       8 | Wholesale Silvia Union              |           1 |           1 |   |             15 |       5 |         1 |
|       9 | Wholesale                           |           0 |           1 |   |             16 |       5 |         2 |
|      10 | Wholesale Silvia Silver pst exempt  |           0 |           1 |   |             17 |       5 |         3 |
|      11 | Wholesale Silvia Gold pst exempt    |           0 |           1 |   |             18 |       5 |         4 |
|      12 | Wholesale Silvia Premium pst exempt |           0 |           1 |   |             19 |       5 |         5 |
|      13 | Wholesale Silvia Union pst exempt   |           0 |           1 |   |             20 |       6 |         1 |
|      14 | Wholesale tax exempt                |           0 |           0 |                  :         :           : 
|      15 | Wholesale Silvia Silver tax exempt  |           0 |           0 |                  :         :           :  
|      16 | Wholesale Silvia Gold tax exempt    |           0 |           0 |                  :         :           :  
|      17 | Wholesale Silvia Premium tax exempt |           0 |           0 |   |             80 |      18 |         1 |
|      18 | Wholesale Silvia Union tax exempt   |           0 |           0 |   |             81 |      18 |         2 |
+---------+-------------------------------------+-------------+-------------+   |             82 |      18 |         3 |
                                                                                |             83 |      18 |         4 |
                                                                                |             84 |      18 |         5 |
                                                                                +----------------+---------+-----------+

... then a single query can get all you need for a customer. (This shows data for all customers for comparison)

SELECT cust_id
     , cust_name
     , role_id
     , role_name
     , pst_payable
     , gst_payable
     , GROUP_CONCAT(method_description ORDER BY method_id SEPARATOR ' / ') as shipping_methods
FROM customer c 
       JOIN
     role r USING (role_id)
       JOIN
     role_method rm USING (role_id)
       JOIN
     shipping_method sm USING (method_id)
GROUP BY c.cust_id;
+---------+------------+---------+-------------------------------------+-------------+-------------+--------------------------------------------------------------------------+
| cust_id | cust_name  | role_id | role_name                           | pst_payable | gst_payable | shipping_methods                                                         |
+---------+------------+---------+-------------------------------------+-------------+-------------+--------------------------------------------------------------------------+
|       1 | Customer A |       1 | Customer                            |           1 |           1 | Carrier pigeon / Pony Express / Drone                                    |
|       2 | Customer B |       2 | Customer pst exempt                 |           0 |           1 | Carrier pigeon / Pony Express / Drone                                    |
|       3 | Customer C |       3 | Customer tax exempt                 |           0 |           0 | Carrier pigeon / Pony Express / Drone                                    |
|       4 | Customer D |      17 | Wholesale Silvia Premium tax exempt |           0 |           0 | Carrier pigeon / Pony Express / Drone / Silvia Standard / Silvia Premium |
|       5 | Customer E |      15 | Wholesale Silvia Silver tax exempt  |           0 |           0 | Carrier pigeon / Pony Express / Drone / Silvia Standard / Silvia Premium |
|       6 | Customer F |      11 | Wholesale Silvia Gold pst exempt    |           0 |           1 | Carrier pigeon / Pony Express / Drone / Silvia Standard / Silvia Premium |
|       7 | Customer G |      18 | Wholesale Silvia Union tax exempt   |           0 |           0 | Carrier pigeon / Pony Express / Drone / Silvia Standard / Silvia Premium |
|       8 | Customer H |      17 | Wholesale Silvia Premium tax exempt |           0 |           0 | Carrier pigeon / Pony Express / Drone / Silvia Standard / Silvia Premium |
|       9 | Customer I |       4 | Wholesale Customer                  |           1 |           1 | Carrier pigeon / Pony Express / Drone                                    |
|      10 | Customer J |      10 | Wholesale Silvia Silver pst exempt  |           0 |           1 | Carrier pigeon / Pony Express / Drone / Silvia Standard / Silvia Premium |
+---------+------------+---------+-------------------------------------+-------------+-------------+--------------------------------------------------------------------------+

 

Edited by Barand
Extra info
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.