Conditional hook

时间:2014-09-23 作者:zero

是否可以在我的子主题函数中使用条件挂钩。php?这是因为我只希望在存在特定条件时执行特定挂钩。

我尝试过执行以下代码,但不起作用

<?php
function check_product_in_cart() {

    global $woocommerce;
    $box_found = false;
    $voucher_found = false;
    $other_products_found = false;

    //start of the loop that fetches the cart items
    foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values[\'data\'];
        $terms = get_the_terms( $_product->id, \'product_cat\' );

        // second level loop search, in case some items have several categories
        foreach ($terms as $term) {
            $_categoryid = $term->term_id;

            //check if BOX product category is in card
            if ( $_categoryid === 103 ) {
                //category is in cart!
                $box_found = true;
            } elseif ( $_categoryid === 106 ) { //check if VOUCHER product category is in card
                //category is in cart!
                $voucher_found = true;
            }else{
                $other_products_found = true;
            }
        }
    }
    return array($box_found, $voucher_found, $other_products_found);
}

remove_action( \'woocommerce_checkout_after_customer_details\', array( $wc_checkout_add_ons->admin, \'render_add_ons\' ) );
function edit_position_of_checkout_addons(){
    list($box_found, $voucher_found, $other_products_found) = check_product_in_cart();
    if(!$voucher_found){
        add_action( \'woocommerce_after_order_notes\', array( $wc_checkout_add_ons->admin, \'render_add_ons\' ) );
    }
}
add_action(\'woocommerce_checkout_after_customer_details\', \'edit_position_of_checkout_addons\');
?>

1 个回复
SO网友:Diogo Gomes

你把代码放在哪里?可能没有执行此代码。

您应该将所有内容包装在一个函数中,并使用wp\\u head hook触发它。。。

<?php
add_action( \'wp_head\', \'woocommerce_conditional_hook\');
function woocommerce_conditional_hook(){
    function check_product_in_cart() {

        global $woocommerce;
        $box_found = false;
        $voucher_found = false;
        $other_products_found = false;

    //start of the loop that fetches the cart items
        foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
            $_product = $values[\'data\'];
            $terms = get_the_terms( $_product->id, \'product_cat\' );

        // second level loop search, in case some items have several categories
            foreach ($terms as $term) {
                $_categoryid = $term->term_id;

            //check if BOX product category is in card
                if ( $_categoryid === 103 ) {
                //category is in cart!
                    $box_found = true;
            } elseif ( $_categoryid === 106 ) { //check if VOUCHER product category is in card
                //category is in cart!
                $voucher_found = true;
            }else{
                $other_products_found = true;
            }
        }
    }
    return array($box_found, $voucher_found, $other_products_found);
}

    list($box_found, $voucher_found, $other_products_found) = check_product_in_cart();
    if(!$voucher_found){
        remove_action( \'woocommerce_checkout_after_customer_details\', array( $wc_checkout_add_ons->admin, \'render_add_ons\' ) );
        add_action( \'woocommerce_after_order_notes\', array( $wc_checkout_add_ons->admin, \'render_add_ons\' ) );
    }else{
        add_action( \'woocommerce_after_order_notes\', array( $wc_checkout_add_ons->admin, \'render_add_ons\' ) );
    }
}
?>

结束

相关推荐

hooks & filters and variables

我是updating the codex page example for action hooks, 在游戏中完成一些可重用的功能(最初是针对这里的一些Q@WA)。但后来我遇到了一个以前没有意识到的问题:在挂接到一个函数以修改变量的输出后,我再也无法决定是要回显输出还是只返回它。The Problem: 我可以修改传递给do_action 用回调函数钩住。使用变量修改/添加的所有内容仅在回调函数中可用,但在do_action 在原始函数内部调用。很高兴:我将其修改为一个工作示例,因此您可以将其复制/粘贴