I have been adding customization straight to the theme's functions.php
and also a little bit of CSS for a pricing table in Appearance --> Customize --> Custom CSS
..
So I've decided that all these mentioned are separate from the theme so I might as well make a plugin.
- Change role after a product purchase
- Shortcode for displaying the current currency and price of an individual product by ID.
- CSS
So far I have this (PLUGIN)
├── Awesome-Plugin
│ ├── awesome-plugin.php
│ ├── shortcodes.php
│ ├── woo-custom.php
│ ├── css
│ │ ├── css.php
│ │ ├── pricing-table.css
The shortcodes and css part is working, only the woo-custom.php code isn't working.
But it works perfectly when its in the theme's functions.php.
AM I DOING SOMETHING WRONG?
1. awesome-plugin.php
<?php
/**
* @package awesome-plugin
*/
/*
*Plugin Name: Awesome Plugin
*Text Domain: awesome-plugin
*/
define( 'AWESOMEPLUGIN_PATH', plugin_dir_path(__FILE__) );
/* Include woo-custom.php */
include AWESOMEPLUGIN_PATH . 'woo-custom.php';
/* Include shortcodes.php */
include AWESOMEPLUGIN_PATH . 'shortcodes.php';
/* Include CSS stylesheets */
include AWESOMEPLUGIN_PATH . 'css/css.php';
?>
2. woo-custom.php
<?php
/**
* Fired during plugin activation
*
* @link
* @since 0.1
* @package awesome-plugin
* @subpackage awesome-plugin/
*/
// 1. Change user roles after product purchase
// a) SuperAdmin Products
function user_role_change_superadmin( $order_id ) {
// Get order ID and User ID
$order = wc_get_order( $order_id );
$items = $order->get_items();
$products_to_check = array( '136962', '136963', '136964' );
foreach ( $items as $item ) {
if ( $order->user_id > 0 && in_array( $item['product_id'], $products_to_check ) ) {
$user = new WP_User( $order->user_id );
// Remove role
$user->remove_role( 'customer' );
// Add role
$user->add_role( 'superadmin' );
// Exit the loop
break;
}
}
}
add_action( 'school_order_completed', 'user_role_change_superadmin' );
I have been adding customization straight to the theme's functions.php
and also a little bit of CSS for a pricing table in Appearance --> Customize --> Custom CSS
..
So I've decided that all these mentioned are separate from the theme so I might as well make a plugin.
- Change role after a product purchase
- Shortcode for displaying the current currency and price of an individual product by ID.
- CSS
So far I have this (PLUGIN)
├── Awesome-Plugin
│ ├── awesome-plugin.php
│ ├── shortcodes.php
│ ├── woo-custom.php
│ ├── css
│ │ ├── css.php
│ │ ├── pricing-table.css
The shortcodes and css part is working, only the woo-custom.php code isn't working.
But it works perfectly when its in the theme's functions.php.
AM I DOING SOMETHING WRONG?
1. awesome-plugin.php
<?php
/**
* @package awesome-plugin
*/
/*
*Plugin Name: Awesome Plugin
*Text Domain: awesome-plugin
*/
define( 'AWESOMEPLUGIN_PATH', plugin_dir_path(__FILE__) );
/* Include woo-custom.php */
include AWESOMEPLUGIN_PATH . 'woo-custom.php';
/* Include shortcodes.php */
include AWESOMEPLUGIN_PATH . 'shortcodes.php';
/* Include CSS stylesheets */
include AWESOMEPLUGIN_PATH . 'css/css.php';
?>
2. woo-custom.php
<?php
/**
* Fired during plugin activation
*
* @link http://awesome-plugin
* @since 0.1
* @package awesome-plugin
* @subpackage awesome-plugin/
*/
// 1. Change user roles after product purchase
// a) SuperAdmin Products
function user_role_change_superadmin( $order_id ) {
// Get order ID and User ID
$order = wc_get_order( $order_id );
$items = $order->get_items();
$products_to_check = array( '136962', '136963', '136964' );
foreach ( $items as $item ) {
if ( $order->user_id > 0 && in_array( $item['product_id'], $products_to_check ) ) {
$user = new WP_User( $order->user_id );
// Remove role
$user->remove_role( 'customer' );
// Add role
$user->add_role( 'superadmin' );
// Exit the loop
break;
}
}
}
add_action( 'school_order_completed', 'user_role_change_superadmin' );
Share
Improve this question
edited Apr 19, 2020 at 8:07
Cullen Carstens
asked Apr 19, 2020 at 7:55
Cullen CarstensCullen Carstens
112 bronze badges
2
|
1 Answer
Reset to default 0I simply used the wrong hook for my function.
I replaced:
add_action( 'school_order_completed', 'user_role_change_superadmin' );
with:
add_action( 'woocommerce_order_status_completed', 'user_role_change_superadmin' );
And this fixed the problem Thanks @Jacob Peattie
school_order_completed
is not a standard WooCommerce hook. Is it from a plugin? – Jacob Peattie Commented Apr 19, 2020 at 8:11