【问题标题】:Get cart items quantities from products with specific tag in WooCommerce从 WooCommerce 中具有特定标签的产品中获取购物车商品数量
【发布时间】:2021-08-29 11:25:56
【问题描述】:

想法:

客户将一个盒子放入购物车(四种不同的简单产品)。它们每盒可容纳 4、9、16 或 24 件。

然后客户开始选择要放入盒子的产品。这些产品应该来自混搭产品标签。如果不是,它们将被单独包装,但更重要的是,它们不会被视为混搭产品。

我不知道该怎么做:

该功能需要统计购物车中有多少个盒子,并自动计算可以添加多少件。

整体示例:

客户添加可容纳 4 件的盒子和可容纳 16 件的盒子。客户现在总共可以将 20 种混搭产品添加到购物车中。

如果客户未将 20 种混合搭配产品添加到购物车,则会显示一条消息。如果客户将超过 20 种混合搭配产品添加到他们的购物车中,则会显示不同的消息。

以下是一些消息示例:

“您已添加适合XX件的盒子。请在您的购物车中添加额外的XX件混搭产品。注意:非混搭产品将单独包装在玻璃纸袋中。”

“您已添加 XX 盒。您可以添加 XX 件。请在您的购物车中添加额外的 XX 混搭产品。注意:非混搭产品将单独包装在玻璃纸袋中。”

这是我需要帮助修改的代码:

add_action('woocommerce_before_shop_loop', 'number_of_pieces_in_boxes', 1, 1 );
add_action('woocommerce_before_add_to_cart_form', 'number_of_pieces_in_boxes', 1, 1 );
function number_of_pieces_in_boxes( $cart ) {

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

    global $product;

    $product_count = WC()->cart->cart_contents_count;

    $product_ids = array( 1718, 1719, 1720, 1721 );

    $fits_four = '4';
    $fits_nine = '9';
    $fits_sixteen = '16';
    $fits_twentyfour = '24';
    $piece_slug = 'mix-and-match';

    $in_cart = false;

        foreach( WC()->cart->get_cart() as $cart_item ) {

            $product_in_cart = $cart_item['product_id'];

            if ( in_array( $product_in_cart, $product_ids ) && has_term( 'mix-and-match', 'product_tag', $product_in_cart, $product_ids ) ) {

                $in_cart = true;

                break;
        }
    }

    if ( ! $in_cart ) {

        echo '<div class="product-count">You have '.$product_count.' pieces in your cart whereof none fits inside a box. We will pack and wrap them for you.</div>';

        } else {
    
            echo '<div class="product-count">You have '.$product_count.' pieces in your cart.</div>';
    }
}

我真的很感激我能在这方面得到任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce cart


    【解决方案1】:
    • 默认情况下,您使用的挂钩不包含 $cart 作为参数
    • 要从购物车中包含特定标签的产品中获取购物车商品数量,您可以使用WC_Cart::get_cart_item_quantities()
    • 将盒子内容+产品ID添加到$box_ids数组中,确保这些产品不包含相关标签
    • 带有特定标签的产品的购物车商品数量保存在柜台中
    • 每个盒子可以包含的内容 * 购物车中每个盒子的件数存储在另一个计数器中
    • 有了这个包含您需要的所有数据的基本答案,您可以选择任何一种方式,这取决于您具体想要什么

    所以你得到:

    function action_woocommerce_before_add_to_cart_form() {
        // Settings
        $term = 'mix-and-match';
        $taxonomy = 'product_tag';
        
        // Box IDs. Important, these should not contain the term!
        // Contents => Product ID
        $box_ids = array( 
            4  => 1718,
            9  => 1719,
            16 => 1720,
            20 => 1721
        );
        
        // Initialize
        $total_term = 0;
        $total_box = 0;
        
        // True
        if ( WC()->cart ) { 
            // Loop trough cart items quantities
            foreach( WC()->cart->get_cart_item_quantities() as $product_id => $cart_item_quantity ) {
                // Contains the definite term
                if ( has_term( $term, $taxonomy, $product_id ) ) {
                    // Add the cart item quantity from this certain product to the counter
                    $total_term += $cart_item_quantity;
                // The box ID is in the cart
                } elseif( in_array( $product_id, $box_ids ) ) {
                    // Add contens * the cart item quantity to the counter
                    $total_box += ( array_search ( $product_id, $box_ids ) * $cart_item_quantity );
                }
            }
        }
        
        echo '<p>Total term: ' . $total_term . '</p>';
        echo '<p>Total box = ' . $total_box . '</p>';
    }
    add_action( 'woocommerce_before_add_to_cart_form', 'action_woocommerce_before_add_to_cart_form', 10, 0 );
    

    【讨论】:

    • 谢谢。我会尽力添加所需的额外功能。这是一个很好的开始,感谢您抽出宝贵时间!
    猜你喜欢
    • 2021-05-16
    • 2018-07-31
    • 2019-09-18
    • 1970-01-01
    • 2021-11-27
    • 2020-11-15
    • 2017-12-10
    • 2019-02-06
    • 2019-01-27
    相关资源
    最近更新 更多