php中的action类,来自PHP类的remove_action

本文介绍了一种在Woocommerce中调整产品购买限制消息显示优先级的方法,通过更改PHP代码实现对显示顺序的控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我试图删除一个动作并以不同的优先级添加它.

以下是有助于生成消息的所有代码段:

包括必需的前端文件

private function frontend_includes() {

require_once( $this->get_plugin_path() . '/includes/wc-memberships-template-functions.php' );

require_once( $this->get_plugin_path() . '/includes/class-wc-memberships-shortcodes.php' );

WC_Memberships_Shortcodes::initialize();

$this->frontend = $this->load_class( '/includes/frontend/class-wc-memberships-frontend.php', 'WC_Memberships_Frontend' );

$this->checkout = $this->load_class( '/includes/frontend/class-wc-memberships-checkout.php', 'WC_Memberships_Checkout' );

$this->restrictions = $this->load_class( '/includes/frontend/class-wc-memberships-restrictions.php', 'WC_Memberships_Restrictions' );

}

获取产品购买受限消息

/**

* @param int $post_id Optional. Defaults to current post.

* @return string

*/

public function get_product_purchasing_restricted_message( $post_id = null ) {

if ( ! $post_id ) {

global $post;

$post_id = $post->ID;

}

$products = $this->get_products_that_grant_access( $post_id );

$message = $this->get_restriction_message( 'product_purchasing_restricted', $post_id, $products );

/**

* Filter the product purchasing restricted message

*

* @since 1.0.0

* @param string $message The restriction message

* @param int $product_id ID of the product being restricted

* @param array $products Array of product IDs that grant access to this product

*/

return apply_filters( 'wc_memberships_product_purchasing_restricted_message', $message, $post_id, $products );

}

限制类,处理前端的内容限制

class WC_Memberships_Restrictions {

/** @var array associative array of content conditions for current user **/

private $user_content_access_conditions;

/** @var array of post IDs that content restriction has been applied to **/

private $content_restriction_applied = array();

/** @var string Product content restriction password helper **/

private $product_restriction_password = null;

/** @var bool Product thumbnail removed helper **/

private $product_thumbnail_restricted = false;

public function __construct() {

// Desired action to remove and re-prioritize

add_action( 'woocommerce_single_product_summary', array( $this, 'single_product_purchasing_restricted_message' ), 30 );

}

}

实际上,我只需要在WC_Memberships_Restrictions类的操作中将优先级从30更改为15.问题在于,没有明确的方法来调用移除.有什么建议么?

解决方法:

好了,您提供的代码表明WC_Memberships_Restrictions类的实例存储在主类的stricts属性中.

$this->restrictions = $this->load_class( '/includes/frontend/class-wc-memberships-restrictions.php', 'WC_Memberships_Restrictions' );

从那里,我只需要从主插件文件的底部查找如何访问主要Membership类的实例:

/**

* Returns the One True Instance of Memberships

*

* @since 1.0.0

* @return WC_Memberships

*/

function wc_memberships() {

return WC_Memberships::instance();

}

这意味着现在要访问限制类的实例,我们需要访问主类的限制属性.虽然听起来像泥泞,但基本上意味着:

wc_memberships()->restrictions

知道这一点,我们就可以知道从该类中删除和添加操作:

function so_41431558_change_hook_priority(){

if( function_exists( 'wc_memberships' ) ){

remove_action( 'woocommerce_single_product_summary', array( wc_memberships()->restrictions, 'single_product_purchasing_restricted_message' ), 30 );

add_action( 'woocommerce_single_product_summary', array( wc_memberships()->restrictions, 'single_product_purchasing_restricted_message' ), 15 );

}

}

add_action( 'woocommerce_single_product_summary', 'so_41431558_change_hook_priority', 1 );

标签:woocommerce,hook-woocommerce,wordpress,php

来源: https://codeday.me/bug/20191111/2023050.html

so the full code would be // Store selected items in session before checkout add_action('wp_ajax_store_selected_items', 'store_selected_items'); add_action('wp_ajax_nopriv_store_selected_items', 'store_selected_items'); function store_selected_items() { check_ajax_referer('woocommerce-cart', 'security'); if (isset($_POST['selected_items'])) { WC()->session->set('selected_checkout_items', $_POST['selected_items']); wp_send_json_success(); } wp_send_json_error(); } // Filter cart contents to only include selected items during checkout add_filter('woocommerce_get_cart_contents', 'filter_cart_contents_for_checkout', 20, 1); function filter_cart_contents_for_checkout($cart_contents) { // Only on checkout page if (is_checkout() && !is_wc_endpoint_url('order-received')) { $selected_items = WC()->session->get('selected_checkout_items'); if (!empty($selected_items)) { $filtered_contents = array(); foreach ($cart_contents as $key => $item) { if (in_array($key, $selected_items)) { $filtered_contents[$key] = $item; } } return $filtered_contents; } } return $cart_contents; } // 修改原 remove_selected_items_after_checkout 函数 function remove_selected_items_after_checkout($order_id) { $selected_items = WC()->session->get('selected_checkout_items'); if (!empty($selected_items)) { foreach ($selected_items as $cart_item_key) { WC()->cart->remove_cart_item($cart_item_key); } // 清除会话数据(关键!) WC()->session->__unset('selected_checkout_items'); // 手动触发购物车更新(确保未选中商品保留) WC()->cart->calculate_totals(); } } // Clear session data WC()->session->__unset('selected_checkout_items'); // AJAX remove selected items add_action('wp_ajax_remove_selected_cart_items', 'remove_selected_cart_items'); add_action('wp_ajax_nopriv_remove_selected_cart_items', 'remove_selected_cart_items'); function remove_selected_cart_items() { check_ajax_referer('woocommerce-cart', 'security'); if (isset($_POST['selected_items'])) { foreach ($_POST['selected_items'] as $cart_item_key) { WC()->cart->remove_cart_item(sanitize_text_field($cart_item_key)); } wp_send_json_success(); } wp_send_json_error(); } // Clear entire cart add_action('wp_ajax_clear_entire_cart', 'clear_entire_cart'); add_action('wp_ajax_nopriv_clear_entire_cart', 'clear_entire_cart'); function clear_entire_cart() { check_ajax_referer('woocommerce-cart', 'security'); WC()->cart->empty_cart(); wp_send_json_success(); } // Remove cart totals section add_action('template_redirect', 'remove_cart_totals_section'); function remove_cart_totals_section() { if (is_cart()) { remove_action('woocommerce_cart_collaterals', 'woocommerce_cart_totals', 10); } } // Enqueue necessary scripts add_action('wp_enqueue_scripts', 'partial_checkout_scripts'); function partial_checkout_scripts() { if (is_cart()) { wp_localize_script('wc-cart', 'wc_cart_params', array( 'ajax_url' => admin_url('admin-ajax.php'), 'cart_nonce' => wp_create_nonce('woocommerce-cart') )); } } // Restore original cart if checkout is abandoned add_action('template_redirect', 'restore_original_cart_if_abandoned'); function restore_original_cart_if_abandoned() { if (is_cart() && WC()->session->get('selected_checkout_items')) { // Clear the session to prevent filtering on next checkout attempt WC()->session->__unset('selected_checkout_items'); } } add_action('woocommerce_init', 'override_woocommerce_cart'); function override_woocommerce_cart() { if (!class_exists('WC_Cart') || !WC()->session) return; // 关键修复:检查 session 是否存在 class WC_Cart_Custom extends WC_Cart { public function empty_cart($force_session = false) { // 安全检查:确保 session 已初始化 if (WC()->session && WC()->session->get('selected_checkout_items')) { return; } parent::empty_cart($force_session); } } // 替换全局 cart 实例 $GLOBALS['woocommerce']->cart = new WC_Cart_Custom(); } correct?
最新发布
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值