0

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.

  1. Change role after a product purchase
  2. Shortcode for displaying the current currency and price of an individual product by ID.
  3. 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.com
 * @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' );
2
  • school_order_completed is not a standard WooCommerce hook. Is it from a plugin? Commented Apr 19, 2020 at 8:11
  • woocommerce_order_status_completed FIXED IT Commented Apr 19, 2020 at 9:01

1 Answer 1

0

I 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.