最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - working code, not working - Plugin Dev

programmeradmin3浏览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       
 * @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.

  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
 * @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
  • school_order_completed is not a standard WooCommerce hook. Is it from a plugin? – Jacob Peattie Commented Apr 19, 2020 at 8:11
  • woocommerce_order_status_completed FIXED IT – Cullen Carstens Commented Apr 19, 2020 at 9:01
Add a comment  | 

1 Answer 1

Reset to default 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

发布评论

评论列表(0)

  1. 暂无评论