需要帮助过滤 WordPress 插件的输出 Class
Need help filtering output of a WordPress Plugin Class
下面我有一个class"WC_Deposits_Add_To_Cart"。我想在我的 functions.php 中使用 add_filter 编辑此 class 的输出。
问题是我对如何正确构建我的过滤器函数来更改 class 的输出有点困惑。我知道如何过滤函数,但不知道 classes。
查看脚本似乎没有返回或回显 html 语法,而且我看到没有变量附加到 html 输出。我想在 class 的最底部编辑 html 语法的输出。
例如 html 从 <div id='wc-deposits-options-form'>
开始到第 176 行的最后一次收盘 div。
我该怎么做?
我遍历了 wordpress codex 和其他论坛,但我没有看到像下面这样输出 html 的 add_filte 函数过滤器 classes 的示例。
任何指导表示赞赏。
<?php
/*Copyright: © 2014 Abdullah Ali.
License: GNU General Public License v3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
class WC_Deposits_Add_To_Cart
{
public function __construct(&$wc_deposits)
{
// Add the required styles
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
add_action('wp_enqueue_scripts', array($this, 'enqueue_inline_styles'));
// Hook the add to cart form
add_action('woocommerce_before_add_to_cart_button', array($this, 'before_add_to_cart_button'));
add_filter('woocommerce_add_cart_item_data', array($this, 'add_cart_item_data'), 10, 2);
}
/**
* @brief Load the deposit-switch logic
*
* @return void
*/
public function enqueue_scripts()
{
if (is_product()) {
global $post;
$product = wc_get_product($post->ID);
if ($product && isset($product->wc_deposits_enable_deposit) && $product->wc_deposits_enable_deposit === 'yes')
{
wp_enqueue_script('wc-deposits-add-to-cart', WC_DEPOSITS_PLUGIN_URL . '/assets/js/add-to-cart.js');
$message_deposit = get_option('wc_deposits_message_deposit');
$message_full_amount = get_option('wc_deposits_message_full_amount');
$message_deposit = stripslashes($message_deposit);
$message_full_amount = stripslashes($message_full_amount);
$script_args = array(
'message' => array(
'deposit' => __($message_deposit, 'woocommerce-deposits'),
'full' => __($message_full_amount, 'woocommerce-deposits')
)
);
if ($product->is_type('variable') && $product->wc_deposits_amount_type !== 'fixed') {
foreach($product->get_children() as $variation_id) {
$variation = $product->get_child($variation_id);
if (!is_object($variation)) continue;
$tax = get_option('wc_deposits_tax_display', 'no') === 'yes' ?
$variation->get_price_including_tax() - $variation->get_price_excluding_tax() : 0;
$amount = woocommerce_price($variation->get_price_excluding_tax() *
($product->wc_deposits_deposit_amount / 100.0) + $tax);
$script_args['variations'][$variation_id] = array($amount);
}
}
wp_localize_script('wc-deposits-add-to-cart', 'wc_deposits_add_to_cart_options', $script_args);
}
}
}
/**
* @brief Enqueues front-end styles
*
* @return void
*/
public function enqueue_inline_styles()
{
if (is_product()) {
global $post;
$product = wc_get_product($post->ID);
if ($product && isset($product->wc_deposits_enable_deposit) && $product->wc_deposits_enable_deposit === 'yes')
{
// prepare inline styles
$colors = wc_deposits_woocommerce_frontend_colours();
$gstart = $colors['primary'];
$gend = wc_deposits_adjust_colour($colors['primary'], 15);
$style = "@media only screen {
#wc-deposits-options-form input.input-radio:enabled ~ label { color: {$colors['secondary']}; }
#wc-deposits-options-form div a.wc-deposits-switcher {
background-color: {$colors['primary']};
background: -moz-gradient(center top, {$gstart} 0%, {$gend} 100%);
background: -moz-linear-gradient(center top, {$gstart} 0%, {$gend} 100%);
background: -webkit-gradient(linear, left top, left bottom, from({$gstart}), to({$gend}));
background: -webkit-linear-gradient({$gstart}, {$gend});
background: -o-linear-gradient({$gstart}, {$gend});
background: linear-gradient({$gstart}, {$gend});
}
#wc-deposits-options-form .amount { color: {$colors['highlight']}; }
#wc-deposits-options-form .deposit-option { display: inline; }
}";
wp_add_inline_style('wc-deposits-frontend-styles', $style);
}
}
}
public function before_add_to_cart_button()
{
global $product;
if ($product && isset($product->wc_deposits_enable_deposit) && $product->wc_deposits_enable_deposit === 'yes')
{
$tax = get_option('wc_deposits_tax_display', 'no') === 'yes' ?
$product->get_price_including_tax() - $product->get_price_excluding_tax() : 0;
if ($product->wc_deposits_amount_type === 'fixed') {
$amount = woocommerce_price($product->wc_deposits_deposit_amount + $tax);
if ($product->is_type('booking') && $product->has_persons() && $product->wc_deposits_enable_per_person === 'yes') {
$suffix = __('per person', 'woocommerce-deposits');
} else if ($product->is_type('booking')) {
$suffix = __('per booking', 'woocommerce-deposits');
} else if (!$product->is_sold_individually()) {
$suffix = __('per item', 'woocommerce-deposits');
} else {
$suffix = '';
}
} else {
if ($product->is_type('booking')) {
$amount = '<span class=\'amount\'>' . round($product->wc_deposits_deposit_amount, 2) . '%' . '</span>';
$suffix = __('of total value', 'woocommerce-deposits');
} else if ($product->is_type('variable')) {
$min_variation = $product->get_child($product->min_price_variation_id);
$max_variation = $product->get_child($product->max_price_variation_id);
$tax_min = get_option('wc_deposits_tax_display', 'no') === 'yes' ? $min_variation->get_price_including_tax() - $min_variation->get_price_excluding_tax() : 0;
$tax_max = get_option('wc_deposits_tax_display', 'no') === 'yes' ? $max_variation->get_price_including_tax() - $max_variation->get_price_excluding_tax() : 0;
$amount_min = woocommerce_price($min_variation->get_price_excluding_tax() * ($product->wc_deposits_deposit_amount / 100.0) + $tax_min);
$amount_max = woocommerce_price($max_variation->get_price_excluding_tax() * ($product->wc_deposits_deposit_amount / 100.0) + $tax_max);
$amount = $amount_min . ' – ' . $amount_max;
} else {
$amount = woocommerce_price($product->get_price_excluding_tax() * ($product->wc_deposits_deposit_amount / 100.0) + $tax);
}
if (!$product->is_sold_individually()) {
$suffix = __('per item', 'woocommerce-deposits');
} else {
$suffix = '';
}
}
$default = get_option('wc_deposits_default_option', 'deposit');
$deposit_checked = ($default === 'deposit' ? 'checked=\'checked\'' : '');
$full_checked = ($default === 'full' ? 'checked=\'checked\'' : '');
$deposit_text = get_option('wc_deposits_button_deposit');
$full_text = get_option('wc_deposits_button_full_amount');
if ($deposit_text === false) $deposit_text = __('Pay Deposit', 'woocommerce-deposits');
if ($full_text === false) $full_text = __('Full Amount', 'woocommerce-deposits');
$deposit_text = stripslashes($deposit_text);
$full_text = stripslashes($full_text);
?>
<div id='wc-deposits-options-form'>
<hr class='separator' />
<label class='deposit-option'>
<?php echo __('Deposit Option:', 'woocommerce-deposits'); ?>
<span id='deposit-amount'><?php echo $amount; ?></span>
<span id='deposit-suffix'><?php echo $suffix; ?></span>
</label>
<div class="deposit-options switch-toggle switch-candy switch-woocommerce-deposits">
<input id='pay-deposit' name='deposit-radio' type='radio' <?php echo $deposit_checked; ?> class='input-radio' value='deposit'>
<label for='pay-deposit' onclick=''><span><?php _e($deposit_text, 'woocommerce-deposits'); ?></span></label>
<?php if (isset($product->wc_deposits_force_deposit) && $product->wc_deposits_force_deposit === 'yes') { ?>
<input id='pay-full-amount' name='deposit-radio' type='radio' class='input-radio' disabled>
<label for='pay-full-amount' onclick=''><span><?php _e($full_text, 'woocommerce-deposits'); ?></span></label>
<?php } else { ?>
<input id='pay-full-amount' name='deposit-radio' type='radio' <?php echo $full_checked; ?> class='input-radio' value='full'>
<label for='pay-full-amount' onclick=''><span><?php _e($full_text, 'woocommerce-deposits'); ?></span></label>
<?php } ?>
<a class='wc-deposits-switcher'></a>
</div>
<span class='deposit-message' id='wc-deposits-notice'></span>
</div>
<?php
}
}
public function add_cart_item_data($cart_item_meta, $product_id)
{
$product = wc_get_product($product_id);
if ($product->wc_deposits_enable_deposit === 'yes')
{
if (!isset($_POST['deposit-radio'])) {
$default = get_option('wc_deposits_default_option');
$_POST['deposit-radio'] = $default ? $default : 'deposit';
}
$cart_item_meta['deposit'] = array(
'enable' => $product->wc_deposits_force_deposit === 'yes' ? 'yes' : ($_POST['deposit-radio'] === 'full' ? 'no' : 'yes')
);
}
return $cart_item_meta;
}
}
在这里,我正在尝试在我的 functions.php
中构建我的过滤器功能
class WC_Deposits_Add_To_Cart {
public function save_posts_hook() {
return '<h1>My function works!</h1>';
}
}
$my_class = new WC_Deposits_Add_To_Cart;
add_action( 'before_add_to_cart_button', array( $my_class, 'save_posts_hook' ) );
那么是不是其他人写了 WC_Deposits_Add_To_Cart class 而你试图挂钩它?
现在 class 创建时将在 before_add_to_cart_button 方法中输出 html。
可以在__construct方法中看到
add_action('woocommerce_before_add_to_cart_button', array($this, 'before_add_to_cart_button'));
如果您想在 html 之前添加一些 html,请执行此操作。
add_action('woocommerce_before_add_to_cart_button', 'my_function_name', 1)
或者在
之后添加html
add_action('woocommerce_before_add_to_cart_button', 'my_function_name', 11)
第三个参数是优先级,它决定了触发动作的顺序,默认为 10。
更新:如果您想覆盖 class 的输出,您可以删除他们的操作:
$my_class = new WC_Deposits_Add_To_Cart;
remove_action('woocommerce_before_add_to_cart_button', array($my_class, 'before_add_to_cart_button'));
add_action('woocommerce_before_add_to_cart_button', 'new_html_output_function');
请注意,在他们的 class 启动后,您的 remove_action 必须 运行。一种方法是向 woocommerce_before_add_to_cart_button
添加一个操作,在他们添加后肯定会 运行,如上所示:
add_action('woocommerce_before_add_to_cart_button', 'after_button', 1)
function after_button(){
$my_class = new WC_Deposits_Add_To_Cart;
remove_action('woocommerce_before_add_to_cart_button', array($my_class, 'before_add_to_cart_button'));
global $product;
if ($product && isset($product->wc_deposits_enable_deposit) && $product->wc_deposits_enable_deposit === 'yes') {
$tax = get_option('wc_deposits_tax_display', 'no') === 'yes' ? $product->get_price_including_tax() - $product->get_price_excluding_tax() : 0;
if ($product->wc_deposits_amount_type === 'fixed') {
$amount = woocommerce_price($product->wc_deposits_deposit_amount + $tax);
if ($product->is_type('booking') && $product->has_persons() && $product->wc_deposits_enable_per_person === 'yes') {
$suffix = __('per person', 'woocommerce-deposits');
} else if ($product->is_type('booking')) {
$suffix = __('per booking', 'woocommerce-deposits');
} else if (!$product->is_sold_individually()) {
$suffix = __('per item', 'woocommerce-deposits');
} else {
$suffix = '';
}
} else {
if ($product->is_type('booking')) {
$amount = '<span class=\'amount\'>' . round($product->wc_deposits_deposit_amount, 2) . '%' . '</span>';
$suffix = __('of total value', 'woocommerce-deposits');
} else if ($product->is_type('variable')) {
$min_variation = $product->get_child($product->min_price_variation_id);
$max_variation = $product->get_child($product->max_price_variation_id);
$tax_min = get_option('wc_deposits_tax_display', 'no') === 'yes' ? $min_variation->get_price_including_tax() - $min_variation->get_price_excluding_tax() : 0;
$tax_max = get_option('wc_deposits_tax_display', 'no') === 'yes' ? $max_variation->get_price_including_tax() - $max_variation->get_price_excluding_tax() : 0;
$amount_min = woocommerce_price($min_variation->get_price_excluding_tax() * ($product->wc_deposits_deposit_amount / 100.0) + $tax_min);
$amount_max = woocommerce_price($max_variation->get_price_excluding_tax() * ($product->wc_deposits_deposit_amount / 100.0) + $tax_max);
$amount = $amount_min . ' – ' . $amount_max;
} else {
$amount = woocommerce_price($product->get_price_excluding_tax() * ($product->wc_deposits_deposit_amount / 100.0) + $tax);
}
if (!$product->is_sold_individually()) {
$suffix = __('per item', 'woocommerce-deposits');
} else {
$suffix = '';
}
}
$default = get_option('wc_deposits_default_option', 'deposit');
$deposit_checked = ($default === 'deposit' ? 'checked=\'checked\'' : '');
$full_checked = ($default === 'full' ? 'checked=\'checked\'' : '');
$deposit_text = get_option('wc_deposits_button_deposit');
$full_text = get_option('wc_deposits_button_full_amount');
if ($deposit_text === false)
$deposit_text = __('Pay Deposit', 'woocommerce-deposits');
if ($full_text === false)
$full_text = __('Full Amount', 'woocommerce-deposits');
$deposit_text = stripslashes($deposit_text);
$full_text = stripslashes($full_text);
?>
<div id='wc-deposits-options-form'>
<hr class='separator' />
<label class='deposit-option'>
<?php
echo __('Deposit Option:', 'woocommerce-deposits');
?>
<span id='deposit-amount'><?php
echo $amount;
?></span>
<span id='deposit-suffix'><?php
echo $suffix;
?></span>
</label>
<div class="deposit-options switch-toggle switch-candy switch-woocommerce-deposits">
<input id='pay-deposit' name='deposit-radio' type='radio' <?php
echo $deposit_checked;
?> class='input-radio' value='deposit'>
<label for='pay-deposit' onclick=''><span><?php
_e($deposit_text, 'woocommerce-deposits');
?></span></label>
<?php
if (isset($product->wc_deposits_force_deposit) && $product->wc_deposits_force_deposit === 'yes') {
?>
<input id='pay-full-amount' name='deposit-radio' type='radio' class='input-radio' disabled>
<label for='pay-full-amount' onclick=''><span><?php
_e($full_text, 'woocommerce-deposits');
?></span></label>
<?php
} else {
?>
<input id='pay-full-amount' name='deposit-radio' type='radio' <?php
echo $full_checked;
?> class='input-radio' value='full'>
<label for='pay-full-amount' onclick=''><span><?php
_e($full_text, 'woocommerce-deposits');
?></span></label>
<?php
}
?>
<a class='wc-deposits-switcher'></a>
</div>
<span class='deposit-message' id='wc-deposits-notice'></span>
</div>
<?php
}
}
一种选择是使用输出缓冲。
add_action('woocommerce_before_add_to_cart_button', 'capture_add_to_cart_output_start', 9);
function capture_add_to_cart_output_start(){
ob_start();
add_action('woocommerce_before_add_to_cart_button', 'capture_add_to_cart_output', 11);
}
function capture_add_to_cart_output(){
$html = ob_get_clean();
$html = str_replace('deposit-options', 'deposit-options bootstrap_class', $html);
echo $html;
}
下面我有一个class"WC_Deposits_Add_To_Cart"。我想在我的 functions.php 中使用 add_filter 编辑此 class 的输出。
问题是我对如何正确构建我的过滤器函数来更改 class 的输出有点困惑。我知道如何过滤函数,但不知道 classes。
查看脚本似乎没有返回或回显 html 语法,而且我看到没有变量附加到 html 输出。我想在 class 的最底部编辑 html 语法的输出。
例如 html 从 <div id='wc-deposits-options-form'>
开始到第 176 行的最后一次收盘 div。
我该怎么做?
我遍历了 wordpress codex 和其他论坛,但我没有看到像下面这样输出 html 的 add_filte 函数过滤器 classes 的示例。
任何指导表示赞赏。
<?php
/*Copyright: © 2014 Abdullah Ali.
License: GNU General Public License v3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
class WC_Deposits_Add_To_Cart
{
public function __construct(&$wc_deposits)
{
// Add the required styles
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
add_action('wp_enqueue_scripts', array($this, 'enqueue_inline_styles'));
// Hook the add to cart form
add_action('woocommerce_before_add_to_cart_button', array($this, 'before_add_to_cart_button'));
add_filter('woocommerce_add_cart_item_data', array($this, 'add_cart_item_data'), 10, 2);
}
/**
* @brief Load the deposit-switch logic
*
* @return void
*/
public function enqueue_scripts()
{
if (is_product()) {
global $post;
$product = wc_get_product($post->ID);
if ($product && isset($product->wc_deposits_enable_deposit) && $product->wc_deposits_enable_deposit === 'yes')
{
wp_enqueue_script('wc-deposits-add-to-cart', WC_DEPOSITS_PLUGIN_URL . '/assets/js/add-to-cart.js');
$message_deposit = get_option('wc_deposits_message_deposit');
$message_full_amount = get_option('wc_deposits_message_full_amount');
$message_deposit = stripslashes($message_deposit);
$message_full_amount = stripslashes($message_full_amount);
$script_args = array(
'message' => array(
'deposit' => __($message_deposit, 'woocommerce-deposits'),
'full' => __($message_full_amount, 'woocommerce-deposits')
)
);
if ($product->is_type('variable') && $product->wc_deposits_amount_type !== 'fixed') {
foreach($product->get_children() as $variation_id) {
$variation = $product->get_child($variation_id);
if (!is_object($variation)) continue;
$tax = get_option('wc_deposits_tax_display', 'no') === 'yes' ?
$variation->get_price_including_tax() - $variation->get_price_excluding_tax() : 0;
$amount = woocommerce_price($variation->get_price_excluding_tax() *
($product->wc_deposits_deposit_amount / 100.0) + $tax);
$script_args['variations'][$variation_id] = array($amount);
}
}
wp_localize_script('wc-deposits-add-to-cart', 'wc_deposits_add_to_cart_options', $script_args);
}
}
}
/**
* @brief Enqueues front-end styles
*
* @return void
*/
public function enqueue_inline_styles()
{
if (is_product()) {
global $post;
$product = wc_get_product($post->ID);
if ($product && isset($product->wc_deposits_enable_deposit) && $product->wc_deposits_enable_deposit === 'yes')
{
// prepare inline styles
$colors = wc_deposits_woocommerce_frontend_colours();
$gstart = $colors['primary'];
$gend = wc_deposits_adjust_colour($colors['primary'], 15);
$style = "@media only screen {
#wc-deposits-options-form input.input-radio:enabled ~ label { color: {$colors['secondary']}; }
#wc-deposits-options-form div a.wc-deposits-switcher {
background-color: {$colors['primary']};
background: -moz-gradient(center top, {$gstart} 0%, {$gend} 100%);
background: -moz-linear-gradient(center top, {$gstart} 0%, {$gend} 100%);
background: -webkit-gradient(linear, left top, left bottom, from({$gstart}), to({$gend}));
background: -webkit-linear-gradient({$gstart}, {$gend});
background: -o-linear-gradient({$gstart}, {$gend});
background: linear-gradient({$gstart}, {$gend});
}
#wc-deposits-options-form .amount { color: {$colors['highlight']}; }
#wc-deposits-options-form .deposit-option { display: inline; }
}";
wp_add_inline_style('wc-deposits-frontend-styles', $style);
}
}
}
public function before_add_to_cart_button()
{
global $product;
if ($product && isset($product->wc_deposits_enable_deposit) && $product->wc_deposits_enable_deposit === 'yes')
{
$tax = get_option('wc_deposits_tax_display', 'no') === 'yes' ?
$product->get_price_including_tax() - $product->get_price_excluding_tax() : 0;
if ($product->wc_deposits_amount_type === 'fixed') {
$amount = woocommerce_price($product->wc_deposits_deposit_amount + $tax);
if ($product->is_type('booking') && $product->has_persons() && $product->wc_deposits_enable_per_person === 'yes') {
$suffix = __('per person', 'woocommerce-deposits');
} else if ($product->is_type('booking')) {
$suffix = __('per booking', 'woocommerce-deposits');
} else if (!$product->is_sold_individually()) {
$suffix = __('per item', 'woocommerce-deposits');
} else {
$suffix = '';
}
} else {
if ($product->is_type('booking')) {
$amount = '<span class=\'amount\'>' . round($product->wc_deposits_deposit_amount, 2) . '%' . '</span>';
$suffix = __('of total value', 'woocommerce-deposits');
} else if ($product->is_type('variable')) {
$min_variation = $product->get_child($product->min_price_variation_id);
$max_variation = $product->get_child($product->max_price_variation_id);
$tax_min = get_option('wc_deposits_tax_display', 'no') === 'yes' ? $min_variation->get_price_including_tax() - $min_variation->get_price_excluding_tax() : 0;
$tax_max = get_option('wc_deposits_tax_display', 'no') === 'yes' ? $max_variation->get_price_including_tax() - $max_variation->get_price_excluding_tax() : 0;
$amount_min = woocommerce_price($min_variation->get_price_excluding_tax() * ($product->wc_deposits_deposit_amount / 100.0) + $tax_min);
$amount_max = woocommerce_price($max_variation->get_price_excluding_tax() * ($product->wc_deposits_deposit_amount / 100.0) + $tax_max);
$amount = $amount_min . ' – ' . $amount_max;
} else {
$amount = woocommerce_price($product->get_price_excluding_tax() * ($product->wc_deposits_deposit_amount / 100.0) + $tax);
}
if (!$product->is_sold_individually()) {
$suffix = __('per item', 'woocommerce-deposits');
} else {
$suffix = '';
}
}
$default = get_option('wc_deposits_default_option', 'deposit');
$deposit_checked = ($default === 'deposit' ? 'checked=\'checked\'' : '');
$full_checked = ($default === 'full' ? 'checked=\'checked\'' : '');
$deposit_text = get_option('wc_deposits_button_deposit');
$full_text = get_option('wc_deposits_button_full_amount');
if ($deposit_text === false) $deposit_text = __('Pay Deposit', 'woocommerce-deposits');
if ($full_text === false) $full_text = __('Full Amount', 'woocommerce-deposits');
$deposit_text = stripslashes($deposit_text);
$full_text = stripslashes($full_text);
?>
<div id='wc-deposits-options-form'>
<hr class='separator' />
<label class='deposit-option'>
<?php echo __('Deposit Option:', 'woocommerce-deposits'); ?>
<span id='deposit-amount'><?php echo $amount; ?></span>
<span id='deposit-suffix'><?php echo $suffix; ?></span>
</label>
<div class="deposit-options switch-toggle switch-candy switch-woocommerce-deposits">
<input id='pay-deposit' name='deposit-radio' type='radio' <?php echo $deposit_checked; ?> class='input-radio' value='deposit'>
<label for='pay-deposit' onclick=''><span><?php _e($deposit_text, 'woocommerce-deposits'); ?></span></label>
<?php if (isset($product->wc_deposits_force_deposit) && $product->wc_deposits_force_deposit === 'yes') { ?>
<input id='pay-full-amount' name='deposit-radio' type='radio' class='input-radio' disabled>
<label for='pay-full-amount' onclick=''><span><?php _e($full_text, 'woocommerce-deposits'); ?></span></label>
<?php } else { ?>
<input id='pay-full-amount' name='deposit-radio' type='radio' <?php echo $full_checked; ?> class='input-radio' value='full'>
<label for='pay-full-amount' onclick=''><span><?php _e($full_text, 'woocommerce-deposits'); ?></span></label>
<?php } ?>
<a class='wc-deposits-switcher'></a>
</div>
<span class='deposit-message' id='wc-deposits-notice'></span>
</div>
<?php
}
}
public function add_cart_item_data($cart_item_meta, $product_id)
{
$product = wc_get_product($product_id);
if ($product->wc_deposits_enable_deposit === 'yes')
{
if (!isset($_POST['deposit-radio'])) {
$default = get_option('wc_deposits_default_option');
$_POST['deposit-radio'] = $default ? $default : 'deposit';
}
$cart_item_meta['deposit'] = array(
'enable' => $product->wc_deposits_force_deposit === 'yes' ? 'yes' : ($_POST['deposit-radio'] === 'full' ? 'no' : 'yes')
);
}
return $cart_item_meta;
}
}
在这里,我正在尝试在我的 functions.php
中构建我的过滤器功能class WC_Deposits_Add_To_Cart {
public function save_posts_hook() {
return '<h1>My function works!</h1>';
}
}
$my_class = new WC_Deposits_Add_To_Cart;
add_action( 'before_add_to_cart_button', array( $my_class, 'save_posts_hook' ) );
那么是不是其他人写了 WC_Deposits_Add_To_Cart class 而你试图挂钩它?
现在 class 创建时将在 before_add_to_cart_button 方法中输出 html。
可以在__construct方法中看到
add_action('woocommerce_before_add_to_cart_button', array($this, 'before_add_to_cart_button'));
如果您想在 html 之前添加一些 html,请执行此操作。
add_action('woocommerce_before_add_to_cart_button', 'my_function_name', 1)
或者在
之后添加htmladd_action('woocommerce_before_add_to_cart_button', 'my_function_name', 11)
第三个参数是优先级,它决定了触发动作的顺序,默认为 10。
更新:如果您想覆盖 class 的输出,您可以删除他们的操作:
$my_class = new WC_Deposits_Add_To_Cart;
remove_action('woocommerce_before_add_to_cart_button', array($my_class, 'before_add_to_cart_button'));
add_action('woocommerce_before_add_to_cart_button', 'new_html_output_function');
请注意,在他们的 class 启动后,您的 remove_action 必须 运行。一种方法是向 woocommerce_before_add_to_cart_button
添加一个操作,在他们添加后肯定会 运行,如上所示:
add_action('woocommerce_before_add_to_cart_button', 'after_button', 1)
function after_button(){
$my_class = new WC_Deposits_Add_To_Cart;
remove_action('woocommerce_before_add_to_cart_button', array($my_class, 'before_add_to_cart_button'));
global $product;
if ($product && isset($product->wc_deposits_enable_deposit) && $product->wc_deposits_enable_deposit === 'yes') {
$tax = get_option('wc_deposits_tax_display', 'no') === 'yes' ? $product->get_price_including_tax() - $product->get_price_excluding_tax() : 0;
if ($product->wc_deposits_amount_type === 'fixed') {
$amount = woocommerce_price($product->wc_deposits_deposit_amount + $tax);
if ($product->is_type('booking') && $product->has_persons() && $product->wc_deposits_enable_per_person === 'yes') {
$suffix = __('per person', 'woocommerce-deposits');
} else if ($product->is_type('booking')) {
$suffix = __('per booking', 'woocommerce-deposits');
} else if (!$product->is_sold_individually()) {
$suffix = __('per item', 'woocommerce-deposits');
} else {
$suffix = '';
}
} else {
if ($product->is_type('booking')) {
$amount = '<span class=\'amount\'>' . round($product->wc_deposits_deposit_amount, 2) . '%' . '</span>';
$suffix = __('of total value', 'woocommerce-deposits');
} else if ($product->is_type('variable')) {
$min_variation = $product->get_child($product->min_price_variation_id);
$max_variation = $product->get_child($product->max_price_variation_id);
$tax_min = get_option('wc_deposits_tax_display', 'no') === 'yes' ? $min_variation->get_price_including_tax() - $min_variation->get_price_excluding_tax() : 0;
$tax_max = get_option('wc_deposits_tax_display', 'no') === 'yes' ? $max_variation->get_price_including_tax() - $max_variation->get_price_excluding_tax() : 0;
$amount_min = woocommerce_price($min_variation->get_price_excluding_tax() * ($product->wc_deposits_deposit_amount / 100.0) + $tax_min);
$amount_max = woocommerce_price($max_variation->get_price_excluding_tax() * ($product->wc_deposits_deposit_amount / 100.0) + $tax_max);
$amount = $amount_min . ' – ' . $amount_max;
} else {
$amount = woocommerce_price($product->get_price_excluding_tax() * ($product->wc_deposits_deposit_amount / 100.0) + $tax);
}
if (!$product->is_sold_individually()) {
$suffix = __('per item', 'woocommerce-deposits');
} else {
$suffix = '';
}
}
$default = get_option('wc_deposits_default_option', 'deposit');
$deposit_checked = ($default === 'deposit' ? 'checked=\'checked\'' : '');
$full_checked = ($default === 'full' ? 'checked=\'checked\'' : '');
$deposit_text = get_option('wc_deposits_button_deposit');
$full_text = get_option('wc_deposits_button_full_amount');
if ($deposit_text === false)
$deposit_text = __('Pay Deposit', 'woocommerce-deposits');
if ($full_text === false)
$full_text = __('Full Amount', 'woocommerce-deposits');
$deposit_text = stripslashes($deposit_text);
$full_text = stripslashes($full_text);
?>
<div id='wc-deposits-options-form'>
<hr class='separator' />
<label class='deposit-option'>
<?php
echo __('Deposit Option:', 'woocommerce-deposits');
?>
<span id='deposit-amount'><?php
echo $amount;
?></span>
<span id='deposit-suffix'><?php
echo $suffix;
?></span>
</label>
<div class="deposit-options switch-toggle switch-candy switch-woocommerce-deposits">
<input id='pay-deposit' name='deposit-radio' type='radio' <?php
echo $deposit_checked;
?> class='input-radio' value='deposit'>
<label for='pay-deposit' onclick=''><span><?php
_e($deposit_text, 'woocommerce-deposits');
?></span></label>
<?php
if (isset($product->wc_deposits_force_deposit) && $product->wc_deposits_force_deposit === 'yes') {
?>
<input id='pay-full-amount' name='deposit-radio' type='radio' class='input-radio' disabled>
<label for='pay-full-amount' onclick=''><span><?php
_e($full_text, 'woocommerce-deposits');
?></span></label>
<?php
} else {
?>
<input id='pay-full-amount' name='deposit-radio' type='radio' <?php
echo $full_checked;
?> class='input-radio' value='full'>
<label for='pay-full-amount' onclick=''><span><?php
_e($full_text, 'woocommerce-deposits');
?></span></label>
<?php
}
?>
<a class='wc-deposits-switcher'></a>
</div>
<span class='deposit-message' id='wc-deposits-notice'></span>
</div>
<?php
}
}
一种选择是使用输出缓冲。
add_action('woocommerce_before_add_to_cart_button', 'capture_add_to_cart_output_start', 9);
function capture_add_to_cart_output_start(){
ob_start();
add_action('woocommerce_before_add_to_cart_button', 'capture_add_to_cart_output', 11);
}
function capture_add_to_cart_output(){
$html = ob_get_clean();
$html = str_replace('deposit-options', 'deposit-options bootstrap_class', $html);
echo $html;
}