php - 当特定产品 ID 位于购物车时设置 Woocommerce 产品类别商品价格

标签 php wordpress woocommerce cart price

我正在努力实现以下目标:

  • 一位客户将产品 1(来自图书类别)添加到购物车,价格为 10 美元。
  • 然后他将产品 2 添加到购物车。

如果产品 2 在购物车中,如何使产品 1 = $0 且所有添加的图书类别中的产品 $0?

我的想法是,如果客户购买某种产品,那么该产品类别的所有产品都会免费。

到目前为止,我得到了这个,但它不起作用,并且正在将购物车中的所有产品制作为 $0:

add_action( 'woocommerce_before_calculate_totals', 'change_price_if_6245_in_cart' );
function change_price_if_6245_in_cart( $cart_object ) {
        if (woo_in_cartbooks (6245) ) {
    // set our flag to be false until we find a product in that category
    $cat_check = false;

    // check each cart item for our category
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

        $product = $cart_item['data'];

        // replace 'books' with your category's slug
        if ( has_term( 'books', 'product_cat', $product->id ) ) {
            $cat_check = true;
            // break because we only need one "true" to matter here
            break;
        }
    }

    // if a product in the cart is in our category, do something
    if ( $cat_check  ) {
        // we have the category, do what we want
        foreach ( $cart_object->get_cart() as $hash => $value ) {
            $value['data']->set_price( 0 );
        }
    }
}

最佳答案

这是使其工作的正确方法(代码已注释):

add_action( 'woocommerce_before_calculate_totals', 'free_product_category_for_a_specific_product', 20, 1 );
function free_product_category_for_a_specific_product( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // HERE Below your settings
    $product_category = "books"; // <= The product category (can be an ID, a slug or a name)
    $specific_product_id = 6245; // <= The specific product ID

    $found = false;

    // 1st cart items Loop: Check for specific product
    foreach ( $cart->get_cart() as $cart_item ) {
        // Check for 'books' with your category's slug
        if ( $cart_item['data']->get_id() == $specific_product_id ) {
            $found = true;
            break; // Found, we stop first loop
        }
    }
    // If specific product is not in cart we exit
    if( ! $found ) 
        return;

    // 2nd cart items Loop: check items remaining to "Books" product category when specific product is in cart
    foreach ( $cart->get_cart() as $cart_item ) {
        // If specific product has been found, we set price to 0 for items remaining to "Books" product category
        if ( has_term( $product_category, 'product_cat', $cart_item['product_id'] ) ) {
            $cart_item['data']->set_price( 0 );
        }
    }
}

代码位于事件子主题(或事件主题)的 function.php 文件中。经过测试并有效。

关于php - 当特定产品 ID 位于购物车时设置 Woocommerce 产品类别商品价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50235339/

相关文章:

php - 如何使用symfony2学说在表中添加列

php - HTTP 2 对 Web 开发人员意味着什么

java - wordpress,密码哈希,在java中

php - 在 woocommerce 结账时为我的自定义支付插件执行自定义 js 代码

php - 隐藏 WooCommerce 中特定用户角色的增值税

php - 仅显示 Woocommerce 子产品类别

php - CakePHP:如何在 View 中使用 Controller::referer()

php - 在浏览器上显示 mysql 存储的时间戳值

php - 在 WooCommerce 订单和电子邮件中添加并显示自定义购物车项目数据

wordpress - WooCommerce:更改按钮类而不覆盖模板文件