Woocommerce 3中按产品类别定义的自定义购物车商品计数

时间:2018-08-09 21:23:03

标签: php ajax wordpress woocommerce cart

我正在尝试按产品类别显示Woocommerce购物车计数。

  • 我有两类:“ 蛋白质”和“ 食品
  • 我想按项目类别显示计数。因此,如果有 2 蛋白 4 储藏室 >,我希望这两个项目类别计数彼此相邻显示,并用ajax更新。
  • 例如最终外观将显示为:购物车中的蛋白质2 购物车中的PANTRY 4 (取决于购物车中物品类型的数量)

我找到了该片段here,该片段显示了给定类别的购物车数量,但是我无法在网页上正确显示它。目前,它仅显示购物车中的物品总数:

function cat_cart_count( $cat_name ) {

global $woocommerce; $cat_count = 0; 

// For each product in the cart
foreach(WC()->cart->get_cart() as $cart_item_key => $values) { 
    $_product_id = $values['product_id']; // product ID
    $_product_qty = $values['quantity']; // product quantity

    // Getting categories of the product (could be more than one)
    $terms = get_the_terms( $_product_id, 'product_cat' );

    // Checking this product has a category
    if ( $terms && ! is_wp_error( $terms ) ) { 
        $term_name = array();

        // For each category of that product
        foreach($terms as $term) {

            // Adding the category name to an array
            $term_name[] = $term->name;

            // if the product has $cat_name category
            if ( in_array( $cat_name, $term_name ) ) {

                // add 1 x product quantity to the count
                $cat_count =+ 1 * $_product_qty;
            }
        }
    }
}
// Returning category count
if ( $cat_count != 0 ) {
    return $cat_count;
} else {
    return '';
}
}

我试图仅将计数器显示为HTML,以便我可以将其嵌入到页面的任何位置并从此处进行自定义:

<a class="cart-contents" href="<?php echo WC()->cart->get_cart_contents_count(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf (_n( '%d protein', '%d protein', cat_cart_count( "protein" ) ), cat_cart_count( "protein" ) ); ?> - <?php echo sprintf (_n( '%d pantry', '%d pantry', cat_cart_count( "pantry" ) ), cat_cart_count( "pantry" ) ); ?> - <?php echo WC()->cart->get_cart_contents_count(); ?></a>

谢谢!

1 个答案:

答案 0 :(得分:1)

  

主要问题来自默认使用"cart-contents"类的刷新的woocommerce ajax购物车片段以及具有刷新的默认购物车计数的现有内容。

所以我们需要:

  • 以不同的方式重命名该类,
  • 在特定的挂钩函数中添加内容(待添加到购物车中刷新)

我还重新访问,简化和压缩了您的功能cat_cart_count()

完整代码:

// Utility function that count specific product category cart items
function cat_cart_count( $term ) {
    $count = 0; // Initializing

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ) {
        if( has_term( $term, 'product_cat', $cart_item['product_id'] ) )
            $count += $cart_item['quantity'];
    }
    // Returning category count
    return $count == 0 ? false : $count;
}

// Utility function that displays the cart items counters
function display_cart_counters() {
    ob_start();

    $link  = wc_get_cart_url();
    $title = __( 'View your shopping cart', 'woocommerce' );
    $count = __( '0 item', 'woocommerce');

    // Displayed on first page load
    echo '<a class="cart-contents-cat" href="' . $link . '" title="' . $title . '">' . $count . '</a>';

    return ob_get_clean();
}

// Displaying refreshed content on add-to cart
add_filter( 'woocommerce_add_to_cart_fragments', 'counters_add_to_cart_fragment', 30, 1 );
function counters_add_to_cart_fragment( $fragments ) {
    ob_start();

    $term1   = 'protein';
    $count1  = cat_cart_count( $term1 );
    $output  = $count1 ? sprintf ( __( '%d %s', 'woocommerce'), $count1, $term1 ) . ' - ' : '';

    $term2   = 'pantry';
    $count2  = cat_cart_count( $term2 );
    $output .= $count2 ? sprintf ( __( '%d %s', 'woocommerce'), $count2, $term2 ) . ' - ' : '';

    $count   = WC()->cart->get_cart_contents_count();
    $output .= $count ? sprintf (_n( '(%d item)', '(%d items)', $count ), $count ) : __( '0 item', 'woocommerce');

    $link    = wc_get_cart_url();
    $title   = __( 'View your shopping cart', 'woocommerce' );

    // Replacement display on any cart item events
    ?>
    <a class="cart-contents-cat" href="<?php echo $link; ?>" title="<?php echo $title ?>"><?php echo $output; ?></a>
    <?php
    $fragments['a.cart-contents-cat'] = ob_get_clean();

    return $fragments;
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试并可以工作。


用法

1)要将计数器放在php文件的html代码中,您将使用:

<?php echo display_cart_counters(); ?>

2)要在任何php代码中使用计数器,您将调用函数**display_cart_counters()**