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

德米特里

在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 ) )

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

我会很高兴为您服务!

7uc1f3r

每当产品包含特定类别时,该产品的数量就会添加到$category_qty_total变量中。此总数随后从购物车中的总数中减去

/**
 * Calculate the number of lunchboxes and package, based on the number of products in cart.
 */ 
function add_delivery_charge_to_cart( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    /********** SETTINGS **********/

    $lunchbox_id  = 5737; // "LunchBox ID" to be added to cart
    $pakket_id = 5738; // "Pakket ID" to be added to cart
    $exclude_categories = array( 'drink', 'bread' ); // Exclude these categories

    $category_qty_total = 0; // Total of category quantity items, Don't edit!!

    /********** LOOP THROUGH CART ITEMS **********/

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // Get product id
        $product_id = $cart_item['data']->get_id();

        // Get product quantity
        $product_qty = $cart_item['quantity'];

        // Check if "LunchBox" product is already in cart
        if( $product_id == $lunchbox_id ) {
            $lunchbox_key = $cart_item_key;
            $lunchbox_qty = $product_qty;
        }

        // Check if "Pakket" product is already in cart
        if( $product_id == $pakket_id ) {
            $pakket_key = $cart_item_key;
            $pakket_qty = $product_qty;
        }

        // Check if product belongs to a certain category
        if( has_term( $exclude_categories, 'product_cat', $product_id ) ) {
            $category_qty_total += $product_qty;
        }
    }

    /********** CALCULATE THE TOTALS, SO "LUNCHBOX", "PAKKET" & CATEGORIES ARE NOT USED IN THE TOTALS **********/

    // Get total items in cart, counts number of products & quantity per product
    $total_items_in_cart = $cart->get_cart_contents_count();

    // Total items in cart - category quantity total
    $total_items_in_cart -= $category_qty_total;

    // Lunchbox total = total_items_in_cart & pakket total = total_items_in_cart 
    $lunchbox_total = $total_items_in_cart;
    $pakket_total = $total_items_in_cart;

    // Isset lunchbox qty -> lunchbox total - lunchbox qty & pakket total - lunchbox qty
    if ( isset($lunchbox_qty) ) {
        $lunchbox_total -= $lunchbox_qty;
        $pakket_total -= $lunchbox_qty;     
    }

    // Isset pakket qty -> lunchbox total - pakket qty & pakket total - pakket qty   
    if ( isset($pakket_qty) ) {
        $lunchbox_total -= $pakket_qty;
        $pakket_total = $pakket_total - $pakket_qty;
    }

    /********** APPLY NEW TOTALS TO LUNCHBOX & PAKKET **********/

    // If product "LunchBox" is in cart, we check the quantity to update it if needed
    if ( isset($lunchbox_key) && $lunchbox_qty != $total_items_in_cart ) {
        // 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 = 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) ) {
            // Set quantity, pakket
            $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 );

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类常见问题

如果产品在购物车中,请更改“添加到购物车”按钮文本,并在WooCommerce中满足条件时重定向到购物车页面

来自分类Dev

将产品添加到WooCommerce单一产品上的购物车后,触发JQuery点击

来自分类Dev

产品添加到购物车时如何获取类别名称

来自分类Dev

WooCommerce从购物车中删除所有产品,并将当前产品添加到购物车

来自分类Dev

添加到购物车按钮未在购物车中添加产品

来自分类Dev

将产品添加到购物车WooCommerce?

来自分类Dev

Woocommerce-防止将两个不同类别的商品添加到购物车

来自分类Dev

仅在woocommerce商店/类别页面上隐藏“添加到购物车”按钮

来自分类Dev

WooCommerce添加到购物车验证:阻止添加到购物车

来自分类Dev

Woocommerce结帐:将产品添加到购物车的复选框

来自分类Dev

Woocommerce使用单个“添加到购物车”按钮将产品分组

来自分类Dev

将产品添加到Rails应用程序中的购物车时,找不到带有'id'=的产品

来自分类Dev

WooCommerce:如何以编程方式将许多产品添加到购物车?

来自分类Dev

在WooCommerce中将产品添加到购物车时自动添加包装

来自分类Dev

允许客户设置产品价格并通过WooCommerce中的某些验证将其添加到购物车

来自分类Dev

Laravel将具有相同产品的购物车按钮添加到+1数量的购物车中

来自分类Dev

Woocommerce:如果当前产品在购物车中,则将Body类添加到产品页面

来自分类Dev

如果购物车包含特定的Woocommerce产品类别,请阻止添加到购物车

来自分类Dev

Woocommerce结帐:将产品添加到购物车的复选框

来自分类Dev

添加产品时,WooCommerce将加售产品添加到购物车

来自分类Dev

WooCommerce从购物车中删除所有产品,并将当前产品添加到购物车

来自分类Dev

添加到购物车按钮未在购物车中添加产品

来自分类Dev

将产品添加到购物车WooCommerce?

来自分类Dev

Magento 1.9 - Ajax 从类别页面/产品列表添加到购物车

来自分类Dev

Woocommerce - 当用户是产品作者时删除添加到购物车按钮

来自分类Dev

当 Woocommerce 中的可变产品缺货时隐藏添加到购物车块

来自分类Dev

在 Woocommerce 单个产品的“添加到购物车”下方显示特定的运输类别

来自分类Dev

根据范围条件自动将产品添加到 WooCommerce 购物车

来自分类Dev

自定义特定 WooCommerce 产品类别上的“添加到购物车”按钮

Related 相关文章

  1. 1

    如果产品在购物车中,请更改“添加到购物车”按钮文本,并在WooCommerce中满足条件时重定向到购物车页面

  2. 2

    将产品添加到WooCommerce单一产品上的购物车后,触发JQuery点击

  3. 3

    产品添加到购物车时如何获取类别名称

  4. 4

    WooCommerce从购物车中删除所有产品,并将当前产品添加到购物车

  5. 5

    添加到购物车按钮未在购物车中添加产品

  6. 6

    将产品添加到购物车WooCommerce?

  7. 7

    Woocommerce-防止将两个不同类别的商品添加到购物车

  8. 8

    仅在woocommerce商店/类别页面上隐藏“添加到购物车”按钮

  9. 9

    WooCommerce添加到购物车验证:阻止添加到购物车

  10. 10

    Woocommerce结帐:将产品添加到购物车的复选框

  11. 11

    Woocommerce使用单个“添加到购物车”按钮将产品分组

  12. 12

    将产品添加到Rails应用程序中的购物车时,找不到带有'id'=的产品

  13. 13

    WooCommerce:如何以编程方式将许多产品添加到购物车?

  14. 14

    在WooCommerce中将产品添加到购物车时自动添加包装

  15. 15

    允许客户设置产品价格并通过WooCommerce中的某些验证将其添加到购物车

  16. 16

    Laravel将具有相同产品的购物车按钮添加到+1数量的购物车中

  17. 17

    Woocommerce:如果当前产品在购物车中,则将Body类添加到产品页面

  18. 18

    如果购物车包含特定的Woocommerce产品类别,请阻止添加到购物车

  19. 19

    Woocommerce结帐:将产品添加到购物车的复选框

  20. 20

    添加产品时,WooCommerce将加售产品添加到购物车

  21. 21

    WooCommerce从购物车中删除所有产品,并将当前产品添加到购物车

  22. 22

    添加到购物车按钮未在购物车中添加产品

  23. 23

    将产品添加到购物车WooCommerce?

  24. 24

    Magento 1.9 - Ajax 从类别页面/产品列表添加到购物车

  25. 25

    Woocommerce - 当用户是产品作者时删除添加到购物车按钮

  26. 26

    当 Woocommerce 中的可变产品缺货时隐藏添加到购物车块

  27. 27

    在 Woocommerce 单个产品的“添加到购物车”下方显示特定的运输类别

  28. 28

    根据范围条件自动将产品添加到 WooCommerce 购物车

  29. 29

    自定义特定 WooCommerce 产品类别上的“添加到购物车”按钮

热门标签

归档