php - 避免 Woocommerce 中特定产品类别的购物车商品价格更新

标签 php wordpress woocommerce cart price

我在我的WordPress子主题functions.php下使用下面的脚本来覆盖价格,但下面的代码会影响所有产品。

如果产品属于某些特定的“衬衫”、“游戏”类别,您能否帮助我“不要运行”下面的代码?

function calculate_cart_total( $cart_object ) {

    foreach ( WC()->cart->get_cart() as $key => $value ) {
        if(is_array($valueArray)) {
            foreach ( $valueArray as $k => $value ) {
                if($value['wccpf_cost']) {
                    $additionalPrice = $value['wccpf_cost']['user_val'];
                }
            }
        }
    }

    $additionalPrice = $value['wccpf_cost']['user_val'];

    foreach ( WC()->cart->get_cart() as $key => $value ) {
        if( method_exists( $value['data'], "set_price" ) ) {
            $value['data']->set_price( $additionalPrice );
        } else {
            $value['data']->price = ($additionalPrice );
        }     
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_cart_total', 99 );

上述代码对于所有产品都能顺利运行,但我不想在所有产品上运行此代码,因为需要此代码

我尝试过像 is_product_category( array( 'shirts', 'games' ) ) 这样的条件标签,但它不起作用。

或者在 WordPress 中是否有任何特定的方法而不是 functions.php 我可以仅在需要时将上述代码运行到特定产品或类别?

我也做了一些谷歌搜索,但找不到完美的解决方案。

最佳答案

您的代码有一些错误...由于您的代码中未定义 $valueArray ,因此第一个 foreach 循环没有任何效果,并且代码在其后变为事件状态。要定位购物车商品类别,您可以使用 WordPress 条件函数 has_term()

请尝试以下操作(适用于 Woocommerce 3+):

// Updating cart item prices conditionally
add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_price', 100, 1 );
function custom_cart_item_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    
    // Your product categories to avoid
    $categories = array("shirts", "games");

    foreach ( $cart->get_cart() as $cart_item ) {
        if( isset($cart_item['wccpf_cost']['user_val']) && ! has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            $cart_item['data']->set_price($cart_item['wccpf_cost']['user_val']);
        }
    }
}

代码位于事件子主题(或事件主题)的 function.php 文件中。它应该有效。


但是,如果您想定位父产品类别,则需要在代码中使用 has_product_categories() 自定义条件函数:

// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
    $parent_term_ids = $categories_ids = array(); // Initializing
    $taxonomy        = 'product_cat';
    $product_id      = $product_id == 0 ? get_the_id() : $product_id;

    if( is_string( $categories ) ) {
        $categories = (array) $categories; // Convert string to array
    }

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
        $result = (array) term_exists( $category, $taxonomy );
        if ( ! empty( $result ) ) {
            $categories_ids[] = reset($result);
        }
    }

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
        if( $term->parent > 0 ){
            $parent_term_ids[] = $term->parent; // Set the parent product category
            $parent_term_ids[] = $term->term_id; // (and the child)
        } else {
            $parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
        }
    }
    return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}

// Updating cart item prices conditionally
add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_price', 100, 1 );
function custom_cart_item_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    
    // Your product categories to avoid
    $categories = array("shirts", "games");

    foreach ( $cart->get_cart() as $cart_item ) {
        if( isset($cart_item['wccpf_cost']['user_val']) && ! has_product_categories( $cart_item['product_id'], $categories ) ) {
            $cart_item['data']->set_price($cart_item['wccpf_cost']['user_val']);
        }
    }
}

代码位于事件子主题(或事件主题)的 function.php 文件中。它应该有效。


相关主题:

关于php - 避免 Woocommerce 中特定产品类别的购物车商品价格更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52991516/

相关文章:

php - 在 CodeIgniter 中处理大量的半相关表单

mysql - 用户上传的图像 – 空白帖子 – 连接由 peer/FastCGI : incomplete headers 重置

php - 如何在 WordPress 中以编程方式将媒体文件附加到帖子?

wordpress - Woocommerce 自定义产品别名

PHP如何在变量中拆分数组

javascript - PHP Ajax 适用于 get,但不适用于 post

php - 如何计算PHP中MySQL中每个重复值的总数?

php - 停止 WordPress 从 301 重定向/index.php 到/

Woocommerce [woocommerce_cart] 短代码不起作用

css - 在移动设备上查看时如何隐藏我的菜单?