为了账号安全,请及时绑定邮箱和手机立即绑定

自动将产品添加到购物车 WooCommerce 时排除某些类别

自动将产品添加到购物车 WooCommerce 时排除某些类别

PHP
PIPIONE 2022-11-12 09:36:42
在 WooCommerce 中,我使用代码在将任何菜肴添加到购物车时自动添加包装。function add_delivery_charge_to_cart( $cart ) {    if ( is_admin() && ! defined( 'DOING_AJAX' ) )        return;    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )        return;    $lunchbox_id  = 5737; // "LunchBox" to be added to cart    $pakket_id = 5738; // "Pakket" to be added to cart    // Loop through cart items    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {        // Check if "LunchBox" product is already in cart        if( $cart_item['data']->get_id() == $lunchbox_id ) {            $lunchbox_key = $cart_item_key;            $lunchbox_qty = $cart_item['quantity'];        }        // Check if "Pakket" product is already in cart        if( $cart_item['data']->get_id() == $pakket_id ) {            $pakket_key = $cart_item_key;            $pakket_qty = $cart_item['quantity'];        }           }add_action( 'woocommerce_before_calculate_totals', 'add_delivery_charge_to_cart', 10, 1 );默认情况下,包装会自动添加到所有产品中。但对我来说这是错误的。我需要排除某些类别,例如“饮料”和“面包”,这样包装就不会与这些类别中的每个产品一起添加。我知道可以添加以下代码:// Excluded product categories in this array (Can be IDs, slugs or names)$excl_cats = array( 'drink', 'bread' ); 和条件:if( ! has_term( $excl_cats, 'product_cat', $product_id ) )只有我不知道如何在上面的代码中正确地做到这一点。我很乐意为您提供帮助!
查看完整描述

1 回答

?
RISEBY

TA贡献1856条经验 获得超5个赞

在 WooCommerce 中,我使用代码在将任何菜肴添加到购物车时自动添加包装。


function add_delivery_charge_to_cart( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )

        return;


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

        return;


    $lunchbox_id  = 5737; // "LunchBox" to be added to cart

    $pakket_id = 5738; // "Pakket" to be added to cart


    // Loop through cart items

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {

        // Check if "LunchBox" product is already in cart

        if( $cart_item['data']->get_id() == $lunchbox_id ) {

            $lunchbox_key = $cart_item_key;

            $lunchbox_qty = $cart_item['quantity'];

        }


        // Check if "Pakket" product is already in cart

        if( $cart_item['data']->get_id() == $pakket_id ) {

            $pakket_key = $cart_item_key;

            $pakket_qty = $cart_item['quantity'];

        }       

    }


    // Get total items in cart, counts number of products and quantity per product

    $total_items_in_cart = WC()->cart->get_cart_contents_count();


    // If product "LunchBox" is in the cart, we check the quantity to update it if needed

    if ( isset($lunchbox_key) && $lunchbox_qty != $total_items_in_cart ) {

        // Lunchbox total = total_items_in_cart 

        $lunchbox_total = $total_items_in_cart;


        // Isset lunchbox qty, lunchbox total - lunchbox qty

        if ( isset($lunchbox_qty) ) {

            $lunchbox_total = $lunchbox_total - $lunchbox_qty;

        }


        // Isset pakket qty, lunchbox total - pakket qty        

        if ( isset($pakket_qty) ) {

            $lunchbox_total = $lunchbox_total - $pakket_qty;

        } 


        // Set quantity, lunchbox

        $cart->set_quantity( $lunchbox_key, $lunchbox_total );


    } elseif ( !isset($lunchbox_key) && $total_items_in_cart > 0 ) {

        // Product "LunchBox" is not in cart, we add it

        $cart->add_to_cart( $lunchbox_id, $total_items_in_cart );

    }


    // Total items in cart greater than or equal to 3

    if ( $total_items_in_cart >= 3 ) {

        // Pakket total = total_items_in_cart 

        $pakket_total = $total_items_in_cart;


        // Isset lunchbox qty, pakket total - lunchbox qty

        if ( isset($lunchbox_qty) ) {

            $pakket_total = $pakket_total - $lunchbox_qty;

        }


        // Isset pakket qty, pakket total - pakket qty      

        if ( isset($pakket_qty) ) {

            $pakket_total = $pakket_total - $pakket_qty;

        }       



        // Pakket total = pakket_total / 3 = floor(result)

        // Floor = round fractions down, rounding result down

        $pakket_total = floor( $pakket_total / 3 );


        // If product "Pakket" is in cart

        if ( isset($pakket_key) ) {         

            $cart->set_quantity( $pakket_key, $pakket_total );

        } elseif ( !isset($pakket_key) ) {

            // Product "Pakket" is not in cart, we add it

            $cart->add_to_cart( $pakket_id, $pakket_total );

        }

    }

}

add_action( 'woocommerce_before_calculate_totals', 'add_delivery_charge_to_cart', 10, 1 );

默认情况下,包装会自动添加到所有产品中。但对我来说这是错误的。


我需要排除某些类别,例如“饮料”和“面包”,这样包装就不会与这些类别中的每个产品一起添加。


我知道可以添加以下代码:


// Excluded product categories in this array (Can be IDs, slugs or names)

$excl_cats = array( 'drink', 'bread' ); 

和条件:


if( ! has_term( $excl_cats, 'product_cat', $product_id ) )

只有我不知道如何在上面的代码中正确地做到这一点。


我很乐意为您提供帮助!


查看完整回答
反对 回复 2022-11-12
  • 1 回答
  • 0 关注
  • 83 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信