Why add_action works only from functions.php not plugin? - php

When I put my code to functions.php
starting with
add_action( 'woocommerce_order_status_completed', 'do_something' );
here is my code
It Works! But when I put it in plugin
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
if ( ! class_exists( 'WC_Some_class' ) ) {
class WC_Some_class {
public function __construct() {
add_action( 'woocommerce_order_status_completed', 'do_something' );
}
script ...
}
}
// finally instantiate our plugin class and add it to the set of globals
$WC_Some_class = new WC_Some_class();
}
}
This does not work. Why is it?

This is one of those instances where by removing bits of your code and being a little over enthusiastic with the delete key, has led to pretty important information being lost.
However i'll take a stab in the dark and presume you're moving the function from functions.php INTO the class. Therefore your add_action has to know that it needs to call the function (method) of a class, NOT a globally defined function.
Try:
add_action( 'woocommerce_order_status_completed', array(&$this, 'do_something') );

Related

Access the order object from woocommerce_order_details_after_order_table hook

In WooCommerce, I am making a custom merchant plugin and I have added this hook:
add_action( 'woocommerce_order_details_after_order_table', 'track_me' );
Inside the track_me() function, I want to retrieve the order to do some things.
The function does what I want when I want, but I'm currently getting the order_id from the URL which seems janky.
I cannot figure out how to properly retrieve the $order object or the $order_id which would be enough.
It's almost certainly something obvious as hours of searching the Internet has been fruitless. I just don't know what the obvious something is...
UPDATE 1: Following the advice of #LoicTheAztec, I did the following:
do_action( 'woocommerce_order_details_after_order_table_items', $order );
class Order_MY extends WC_Order
{
function __construct()
{
add_action( 'woocommerce_order_details_after_order_table', array( $this, 'track_me' ) );
}
function track_me( $order )
{
// My code here
}
}
The first place the script fails is on the do_action line where PHP complains $order is an undefined variable.
The script also fails at extending WC_Order: Class 'WC_Order' not found
New question... is there something I need to do to make sure I have access to the woocommerce classes in my plugin?
The WC_Order Object instance $order variable is included in the hook. You see that on order/order-details.php template file (line 76):
do_action( 'woocommerce_order_details_after_order_table_items', $order );
So you can use it in your hooked function this way:
add_action( 'woocommerce_order_details_after_order_table', 'track_me' );
function track_me( $order ) {
// The Order ID
$order_id = $order->get_id();
// your code goes below
}
Now in a plugin, inside a class, you will replace:
add_action( 'woocommerce_order_details_after_order_table', 'track_me' );
by:
add_action( 'woocommerce_order_details_after_order_table', array( $this, 'track_me' ) );
Addition:
As you can't get the $order object from your plugin you should also try:
add_action( 'woocommerce_order_details_after_order_table', 'track_me' );
function track_me( $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) {
global $order;
}
// The Order ID
$order_id = $order->get_id();
// your code goes below
}
I cant tell you anything more
I am not an expert but maybe sth. like this could work:
global $order_id;
$order = wc_get_order($order_id);

Wordpress child theme can't prevent parent theme error

Trying to update a Wordpress instance to PHP 7.2 and I've got a parent theme that uses the deprecated $this in its functions.php.
function paperback_filter_page_templates( $page_templates, $this, $post ) {
if( ! class_exists( 'Easy_Digital_Downloads' ) ) :
unset( $page_templates['inc/edd/home-downloads.php'] );
endif;
return $page_templates;
}
add_filter( 'theme_page_templates', 'paperback_filter_page_templates', 10, 3 );
I've been unsuccessful...
1: Overriding the function in the child theme's functions.php:
(removing $this from the param and with higher priority add_filter)
function paperback_filter_page_templates( $page_templates, $post ) {
if( ! class_exists( 'Easy_Digital_Downloads' ) ) :
unset( $page_templates['inc/edd/home-downloads.php'] );
endif;
return $page_templates;
}
add_filter( 'theme_page_templates', 'paperback_filter_page_templates', 15, 2 );
2: Using a function that removes the filter in the child theme's functions.php:
function rm_paperback_filter_page_templates() {
remove_filter( 'theme_page_templates', 'paperback_filter_page_templates', 10);
}
add_action( 'wp_loaded', 'rm_paperback_filter_page_templates', 15 );
(have tried hooking this action into init, setup_theme, after_setup_theme...)
I can remove the offending function from the parent theme to avoid a catastrophic php 7.2 upgrade for now (parent theme authors/company have been acquired so an update isn't coming) and I know to look for parent themes with pluggable functions from now on... but it'd be nice to know if a real fix is possible!

remove_action From PHP Class in WooCommerce Memberships

I have previously used a solution described here: remove_action From PHP Class for removing an action in the WooCommerce membership plugin.
However, the solution no longer works, as WooComemerce have changed the code behind the membership plugin.
So this is the new code.
Main woocommerce-memberships.php
public function includes() {
// load post types
require_once( $this->get_plugin_path() . '/includes/class-wc-memberships-post-types.php' );
// load user messages helper
require_once( $this->get_plugin_path() . '/includes/class-wc-memberships-user-messages.php' );
// load helper functions
require_once( $this->get_plugin_path() . '/includes/functions/wc-memberships-functions.php' );
// init general classes
$this->rules = $this->load_class( '/includes/class-wc-memberships-rules.php', 'WC_Memberships_Rules' );
$this->plans = $this->load_class( '/includes/class-wc-memberships-membership-plans.php', 'WC_Memberships_Membership_Plans' );
$this->emails = $this->load_class( '/includes/class-wc-memberships-emails.php', 'WC_Memberships_Emails' );
$this->user_memberships = $this->load_class( '/includes/class-wc-memberships-user-memberships.php', 'WC_Memberships_User_Memberships' );
$this->capabilities = $this->load_class( '/includes/class-wc-memberships-capabilities.php', 'WC_Memberships_Capabilities' );
$this->member_discounts = $this->load_class( '/includes/class-wc-memberships-member-discounts.php', 'WC_Memberships_Member_Discounts' );
$this->restrictions = $this->load_class( '/includes/class-wc-memberships-restrictions.php', 'WC_Memberships_Restrictions' );
Main instance
function wc_memberships() {
return WC_Memberships::instance();
}
From included class-wc-memberships-restrictions.php file
/**
* Returns the general content restrictions handler.
*
* #since 1.9.0
*
* #return null|\WC_Memberships_Posts_Restrictions
*/
public function get_posts_restrictions_instance() {
if ( ! $this->posts_restrictions instanceof WC_Memberships_Posts_Restrictions ) {
$this->posts_restrictions = wc_memberships()->load_class( '/includes/frontend/class-wc-memberships-posts-restrictions.php', 'WC_Memberships_Posts_Restrictions' );
}
return $this->posts_restrictions;
}
Then in class-wc-memberships-posts-restrictions.php
public function __construct() {
// decide whether attempting to access restricted content has to be redirected
add_action( 'wp', array( $this, 'handle_restriction_modes' ) );
// restrict the post by filtering the post object and replacing the content with a message and maybe excerpt
add_action( 'the_post', array( $this, 'restrict_post' ), 0 );
How do i remove the 'the_post' action?
So far i have the following in functions.php theme file:
function weteach_remove_actions(){
if(is_singular( 'post' )) {
if( function_exists( 'wc_memberships' ) ){
remove_action( 'the_post', array( wc_memberships()->restrictions, 'restrict_post' ));
}
}
return;
}
add_action( 'the_post', 'weteach_remove_actions', 1 );
Which gives me a "blank-page"-error.
Could you tell us what the error message was? My guess is that restrictions and post_restrictions aren't the same property and so you aren't finding the restrict_post method in the right class.
Edited now that I have looked at Memberships, this seems to work for me:
function so_41431558_remove_membership_post_restrictions(){
if( function_exists( 'wc_memberships' ) && version_compare( WC_Memberships::VERSION, '1.9.0', '>=' ) && is_singular( 'post' ) ){
remove_action( 'the_post', array( wc_memberships()->get_restrictions_instance()->get_posts_restrictions_instance(), 'restrict_post' ), 0 );
}
}
add_action( 'wp_head', 'so_41431558_remove_membership_post_restrictions', 1 );
Your add_action attempt is happening on priority 1, which is after the function has already run the Memberships method on priority 0, so even if the rest of your code was correct it would be too late.
So 1. I think we need to go to an earlier hook.
And 2. I think we need to use the new method for accessing the post restrictions class instance.
edited to add
and 3. I've switched to a direct version compare condition
and 4. I misread where the get_posts_restrictions_instance() method was... it is accessed via wc_memberships()->get_restrictions_instance()->get_posts_restrictions_instance()

need help removing action from plugin file

Hello I am trying to remove an action from a wordpress plugin file. The plugin is called Woocommerce Points and Rewards. I have found the action I want to remove in one of the class files. When I comment out the "add_action" it does exactly what I want. But I am trying to remove the action from functions.php in my child them. I have been reading on this and I think my problem is I need to "globalize" the class variable that the action is in; but I am not sure what that class variable is…
here is the code where it adds the action (part of a file):
class WC_Points_Rewards_Cart_Checkout {
/**
* Add cart/checkout related hooks / filters
*
* #since 1.0
*/
public function __construct() {
// Coupon display
add_filter( 'woocommerce_cart_totals_coupon_label', array( $this, 'coupon_label' ) );
// Coupon loading
add_action( 'woocommerce_cart_loaded_from_session', array( $this, 'points_last' ) );
add_action( 'woocommerce_applied_coupon', array( $this, 'points_last' ) );
// add earn points/redeem points message above cart / checkout
add_action( 'woocommerce_before_cart', array( $this, 'render_earn_points_message' ), 15 );
add_action( 'woocommerce_before_cart', array( $this, 'render_redeem_points_message' ), 16 );
add_action( 'woocommerce_before_checkout_form', array( $this, 'render_earn_points_message' ), 5 );
add_action( 'woocommerce_before_checkout_form', array( $this, 'render_redeem_points_message' ), 6 );
// handle the apply discount submit on the cart page
add_action( 'wp', array( $this, 'maybe_apply_discount' ) );
// handle the apply discount AJAX submit on the checkout page
add_action( 'wp_ajax_wc_points_rewards_apply_discount', array( $this, 'ajax_maybe_apply_discount' ) );
}
The function I want to remove is this one:
add_action( 'woocommerce_before_cart', array( $this, 'render_redeem_points_message' ), 16 );
so far no luck in getting it removed; here is what I have in functions.php:
global $woocommerce, $wc_points_rewards;
/*
global $this;
*/
remove_action( 'woocommerce_before_cart', array( $this, 'render_redeem_points_message' ), 16 );
so - I am sure this can be done in this way at least I have read that it can be done, I think I just have some thing wrong on this…
I tried globalizing $this, but that just gave me an error message...
if you need to see the entire file or something else please just let me know…
So I am hoping someone on here can help me identify what I am doing wrong…
** UPDATE Monday 8/18 ********
Looking for where class is instantiated; I have found this in the "woo commerce-points-and-rewards.php" file; this looks like this may be it but not sure what I am looking at;
does this look like where the "WC_Points_Rewards_Cart_Checkout" is instantiated?
And if so I am not sure how i use this to write my "remove action" in functions.php...
private function includes() {
// product class
require( 'classes/class-wc-points-rewards-product.php' );
$this->product = new WC_Points_Rewards_Product();
// cart / checkout class
require( 'classes/class-wc-points-rewards-cart-checkout.php' );
$this->cart = new WC_Points_Rewards_Cart_Checkout();
// order class
require( 'classes/class-wc-points-rewards-order.php' );
$this->order = new WC_Points_Rewards_Order();
// discount class
require( 'classes/class-wc-points-rewards-discount.php' );
$this->discount = new WC_Points_Rewards_Discount();
// actions class
require( 'classes/class-wc-points-rewards-actions.php' );
$this->actions = new WC_Points_Rewards_Actions();
// manager class
require( 'classes/class-wc-points-rewards-manager.php' );
// points log access class
require( 'classes/class-wc-points-rewards-points-log.php' );
if ( is_admin() )
$this->admin_includes();
}
Thanks so much...
Try this:
// Use the class name instead of a globalized $this
remove_action( 'woocommerce_before_cart', array( 'WC_Points_Rewards_Cart_Checkout', 'render_redeem_points_message' ), 16 );
As $this is an internal referrer to the class it is used in, globalizing it may not be a good thing.
Does the plugin allow you to extend the class with your own and use it instead?
you found a solution for this? having the same problem with another wc plugin ;)
alright. found an answer in the wc docs. in my case:
function wc_move_checkout_addons() {
remove_action( 'woocommerce_checkout_after_customer_details', array( $GLOBALS['wc_checkout_add_ons']->frontend, 'render_add_ons' ) );
add_action( 'woocommerce_checkout_before_customer_details', array( $GLOBALS['wc_checkout_add_ons']->frontend, 'render_add_ons' ) );
}
add_action( 'init', 'wc_move_checkout_addons' );
so in your case it should be something like that:
function wc_remove_message() {
remove_action( 'woocommerce_before_cart', array( $GLOBALS['wc_points_rewards_cart_checkout']->frontend, 'render_redeem_points_message' ) );
}
add_action( 'init', 'wc_remove_message' );
In case anyone else is wondering, I just accomplished removing the action in this plugin in the following way.
I wanted to remove the action defined in class-wc-points-rewards.php on line 34:
add_action( 'woocommerce_single_product_summary', array( $this, 'render_product_message' ) );
To do that, I looked at woocommerce-points-and-rewards.php. On line 46 it shows:
$GLOBALS['wc_points_rewards'] = new WC_Points_Rewards();
And in the same file for the definition of the WC_Points_Rewards class, on line 249-250 it shows:
require( 'includes/class-wc-points-rewards-product.php' );
$this->product = new WC_Points_Rewards_Product();
With that information, I was able to remove the action:
remove_action( 'woocommerce_single_product_summary', array( $GLOBALS['wc_points_rewards']->product, 'render_product_message' ) );
I was able to figure out how to remove the render_redeem_points_message effectively with the following code:
/* Removes render_redeem_points_message() */
function wc_remove_points_message() {
global $woocommerce;
global $wc_points_rewards;
// Removes message from cart page
remove_action( 'woocommerce_before_cart', array( $wc_points_rewards->cart, 'render_redeem_points_message' ), 16 );
// Removes message from checkout page
remove_action( 'woocommerce_before_checkout_form', array( $wc_points_rewards->cart, 'render_redeem_points_message' ), 6 );
}
// Removes action on init
add_action( 'init', 'wc_remove_points_message' );
To share a bit more, I wanted to create a minimum purchase amount to be able to redeem points:
/* Adds Minimum Order for Points Redemption */
function wc_min_order_points_message() {
global $woocommerce;
global $wc_points_rewards;
// Get cart subtotal, excluding tax
$my_cart_total = $woocommerce->cart->subtotal_ex_tax;
if ($my_cart_total < 30) { // $30 minimum order
remove_action( 'woocommerce_before_cart', array( $wc_points_rewards->cart, 'render_redeem_points_message' ), 16 );
remove_action( 'woocommerce_before_checkout_form', array( $wc_points_rewards->cart, 'render_redeem_points_message' ), 6 );
} // endif $my_cart_total
}
// Adds action for cart and checkout pages instead of on init
add_action( 'woocommerce_before_cart', 'wc_min_order_points_message' );
add_action( 'woocommerce_before_checkout_form', 'wc_min_order_points_message' );
I hope this helps anyone else trying to similarly expand on the WooCommerce Points and Rewards plugin.
SOLUTION-1:
In this case, as we have the plugin object's global instance, then we can do it easily:
remove_action('woocommerce_before_cart',array($GLOBALS['wc_points_rewards']->cart,'render_redeem_points_message'),16);
SOLUTION-2:
We will not be lucky always like the above solution getting the instance of the class object if any plugin author creates the class object anonymously without storing it to any global variables or keeping no method which can return it's own instance. In those cases, we can use the following :)
//keeping this function in our functions.php
function remove_anonymous_object_action( $tag, $class, $method, $priority=null ){
if( empty($GLOBALS['wp_filter'][ $tag ]) ){
return;
}
foreach ( $GLOBALS['wp_filter'][ $tag ] as $filterPriority => $filter ){
if( !($priority===null || $priority==$filterPriority) )
continue;
foreach ( $filter as $identifier => $function ){
if( is_array( $function)
and is_a( $function['function'][0], $class )
and $method === $function['function'][1]
){
remove_action(
$tag,
array ( $function['function'][0], $method ),
$filterPriority
);
}
}
}
}
And calling the following line appropriately when we need (may be with a hook or something):
//-->Actual Target: this line does not work;
//remove_action( 'personal_options', array('myCRED_Admin','show_my_balance') );
//-->But instead this line will work ;)
remove_anonymous_object_action('personal_options','myCRED_Admin','show_my_balance');
exactly this function worked for me
if (class_exists('WC_Points_Rewards')) {
global $woocommerce, $wc_points_rewards;
remove_action( 'woocommerce_before_cart', array( $wc_points_rewards->cart, 'render_earn_points_message' ), 15 );
remove_action( 'woocommerce_before_cart', array( $wc_points_rewards->cart, 'render_redeem_points_message' ), 16 );
}

global post variable not working in wordpress plugin

I understand that the Wordpress global $post may not work until a certain point because it hasnt been loaded, but Im wondering is there a workaround for this.
Basically Im building a plugin specifically for a client. At the moment the variable is showing nothing.
This code is in a plugin in the plugins folder.
Im looking to make sure it only loads javascript (Ive left out that bit) when on specific pages (selected by the user), at the moment its loading on all pages.
My other option is to do it all on a php template, but to be honest I wanted to write it as a plugin with a view to customizing it for more generic use in the future, plus I have little experience with plugins so Im trying to improve that side of things also.
function include_js() {
global $post;
print_r($post);
if(is_object($post) && $post->ID == 14 ){
// do stuff
wp_enqueue_script('include-map', plugin_dir_url( __FILE__ ) . 'map.js');
}
}
add_action( 'init', 'include_js' );
EDIT: I realised my main issue is that I want to include the javascript and because of that I need wp_enqueue_script , but I can only seem to get that work if you use the init action, which happens before the loop.
After seeing your edit, try hooking to wp_enqueue_scripts instead of init. Like this:
function include_js() {
global $post;
print_r( $post );
if ( is_object( $post ) && $post->ID == 14 ) {
// do stuff
wp_enqueue_script( 'include-map', plugin_dir_url( __FILE__ ) . 'map.js' );
}
}
add_action( 'wp_enqueue_scripts', 'include_js' );
Ref: http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts
You should test $wp_query->queried_object_id instead of $post->id:
function include_js() {
global $wp_query;
if ( $wp_query->queried_object_id == 14 ) {
// do stuff
//...
}
}
add_action( 'wp_enqueue_scripts', 'include_js' );
There is no need for any globals etc. You can simply use is_single($post) to check if you are on a specific single post and then enqueue your script. You should always always use the wp_enqueue_scripts hook to hook your function to to enqueue scripts and styles
function include_js() {
if(is_single(14)) {
// do stuff
wp_enqueue_script('include-map', plugin_dir_url( __FILE__ ) . 'map.js');
}
}
add_action( 'wp_enqueue_scripts', 'include_js', 999 );
Remeber also, always add priority when adding custom scripts/styles. See add_action( $hook, $function_to_add, $priority, $accepted_args )

Categories