Php 需要从插件文件中删除操作的帮助吗
您好,我正在尝试从wordpress插件文件中删除操作。该插件称为Woocommerce积分和奖励。我在一个类文件中找到了要删除的操作。当我注释掉“add_action”时,它正是我想要的。但是我正在尝试从我的孩子的functions.php中删除操作。我一直在读这篇文章,我认为我的问题是我需要“全球化”行动所处的阶级变量;但我不确定那个类变量是什么 以下是添加操作(文件的一部分)的代码: 我要删除的函数是:Php 需要从插件文件中删除操作的帮助吗,php,wordpress,Php,Wordpress,您好,我正在尝试从wordpress插件文件中删除操作。该插件称为Woocommerce积分和奖励。我在一个类文件中找到了要删除的操作。当我注释掉“add_action”时,它正是我想要的。但是我正在尝试从我的孩子的functions.php中删除操作。我一直在读这篇文章,我认为我的问题是我需要“全球化”行动所处的阶级变量;但我不确定那个类变量是什么 以下是添加操作(文件的一部分)的代码: 我要删除的函数是: add_action( 'woocommerce_before_cart', arra
add_action( 'woocommerce_before_cart', array( $this, 'render_redeem_points_message' ), 16 );
到目前为止,它还没有被移除;下面是我在functions.php中的内容:
global $woocommerce, $wc_points_rewards;
/*
global $this;
*/
remove_action( 'woocommerce_before_cart', array( $this, 'render_redeem_points_message' ), 16 );
所以-我确信这是可以做到的,至少我已经读到了,这是可以做到的,我想我只是在这方面有一些错误
我尝试全球化$this,但这只是给了我一个错误消息
如果您需要查看整个文件或其他内容,请告诉我
所以我希望这里的人能帮我找出我做错了什么
**2018年8月8日星期一更新******
查找类实例化的位置;我在“woo commerce points and rewards.php”文件中发现了这一点;这看起来可能是这样,但不确定我在看什么
// 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 );
由于$this
是对其所用类的内部引用,因此将其全球化可能不是一件好事
该插件是否允许您使用自己的插件扩展类并使用它?您找到了解决方案吗?另一个wc插件也有同样的问题;) 好的。在wc文档中找到了答案。就我而言:
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' );
所以在你的情况下,应该是这样的:
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' );
如果其他人想知道,我只是通过以下方式完成了删除此插件中的操作 我想删除第34行的
class wc points rewards.php
中定义的操作:
add_action( 'woocommerce_single_product_summary', array( $this, 'render_product_message' ) );
为此,我查看了woocommerce points and rewards.php
。第46行显示:
$GLOBALS['wc_points_rewards'] = new WC_Points_Rewards();
require( 'includes/class-wc-points-rewards-product.php' );
$this->product = new WC_Points_Rewards_Product();
在同一文件中,第249-250行显示了WC\u积分\u奖励
类的定义:
$GLOBALS['wc_points_rewards'] = new WC_Points_Rewards();
require( 'includes/class-wc-points-rewards-product.php' );
$this->product = new WC_Points_Rewards_Product();
有了这些信息,我就可以删除该操作:
remove_action( 'woocommerce_single_product_summary', array( $GLOBALS['wc_points_rewards']->product, 'render_product_message' ) );
我能够通过以下代码找到如何有效地删除render_Redeme_points_消息:
/* 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' );
为了分享更多信息,我想创建一个能够兑换积分的最低购买金额:
/* 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' );
/*增加积分兑换的最低订单*/
函数wc_min_order_points_message(){
全球商业;
全球$wc_积分_奖励;
//获取购物车小计,不含税
$my\u cart\u total=$woocommerce->cart->ex\u tax小计;
如果($my_cart_total<30){/$30最低订单
移除购物车操作('woocommerce'在购物车之前',数组($wc\u points\u rewards->cart,'render\u Receive\u points\u message'),16);
移除行动('woocommerce\u before\u checkout\u form',数组($wc\u points\u rewards->cart,'render\u Redeme\u points\u message'),6);
}//endif$my\u cart\u总计
}
//添加购物车和签出页面的操作,而不是在init上
添加操作(“购物车前的woocommerce”、“wc\U min\U order\U points\U message”);
添加动作('woocommerce'u before'u checkout\u form'、'wc\u min\u order\u points\u message');
我希望这能帮助其他尝试类似扩展WooCommerce积分和奖励插件的人。解决方案-1: 在这种情况下,由于我们有插件对象的全局实例,因此我们可以轻松地完成:
remove_action('woocommerce_before_cart',array($GLOBALS['wc_points_rewards']->cart,'render_redeem_points_message'),16);
解决方案2:
如果任何插件作者匿名创建类对象,而不将其存储到任何全局变量,也不保留任何可以返回其自身实例的方法,那么我们就不会像上述解决方案那样幸运地获得类对象的实例。在这种情况下,我们可以使用以下方法:)
当我们需要时(可能是用钩子什么的)适当地拨打以下电话:
正是这个函数对我有用
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 );
}
哦,您还需要添加钩子操作的确切实例来删除钩子操作初始值设定项和类关系,可能通缉的类位于
$woocommerce
全局下的某个位置。否则,尝试以其他方式查找WC\u Points\u Rewards\u Cart\u Checkout
。你好,Ojrask:我想我可能已经找到了;我将其添加到了上面的问题中…谢谢,很好,谢谢更新。$this
是一个类,它保存了将钩子添加为类属性的类。我会尝试将$this->cart
(假设cart
包含钩子类)的类实例分配到一个变量(例如$thiscart=$this->cart
)中,您可以在删除操作(“…”,数组($thiscart,“…”)中使用该变量,…)
。请演示如何实例化类WC\u积分\u奖励\u购物车\u结帐
。你好,Brasofilo,我想我可能已经找到了它-我把它添加到了我上面的问题中…谢谢,Geesh…在另一个类中实例化了…看一看,可能会有用。
//-->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');
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 );
}