工作代码,不工作-插件开发人员

时间:2020-04-19 作者:Cullen Carstens

我一直在为主题的functions.php 还有一点CSS,用于中的定价表Appearance --> Customize --> Custom CSS..

所以我决定,所有这些都是从主题中分离出来的,所以我不妨制作一个插件。

购买产品后更改角色,按ID显示单个产品的当前货币和价格的快捷码。到目前为止,我有这个(插件)

├── Awesome-Plugin
│   ├── awesome-plugin.php
│   ├── shortcodes.php
│   ├── woo-custom.php
│   ├── css
│   │   ├── css.php
│   │   ├── pricing-table.css
短代码和css部分正在工作,only the woo-custom.php code isn\'t working.<但是当它在主题的功能中时,它工作得很好。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\' );

1 个回复
最合适的回答,由SO网友:Cullen Carstens 整理而成

我只是在函数中使用了错误的钩子。

我替换了:

add_action( \'school_order_completed\', \'user_role_change_superadmin\' );
使用:

add_action( \'woocommerce_order_status_completed\', \'user_role_change_superadmin\' );
这解决了问题谢谢Jacob Peattie