计算购物车内容与WooCommerce购物车中的术语计数,并使用AJAX(Fragments)显示差异



我有四个礼品盒。客户只能将标有fits-in-giftbox的产品放入其中。这些盒子可容纳4件、9件、16件或24件。所有其他产品将单独包装。

我需要计算每个盒子被添加到购物车的次数,并计算它们可以容纳的总件数。

然后,我需要计算客户在购物车中添加了多少带有fits-in-giftbox标签的产品,并将该数字与所有礼品盒可以容纳的数量相匹配。

示例:客户将一个箱子-4(可容纳4个产品(和一个箱子-24(可容纳24个产品(放入购物车中。客户现在总共可以在礼品盒中添加28种标有合身标签的产品。

客户添加了18个标有fits-in-giftbox的产品。现在还剩10件。

预期输出:";您已在购物车中添加了三(3(个礼品盒。请添加额外的XX个产品来填充您的盒子">

或者:";您添加了两(2(个礼品盒,可容纳8件。您已将12种产品添加到购物车中。超过四(4(种产品将单独包装,除非您在购物车中添加另一个礼品盒">

或者:";您已在购物车中添加了一个礼品盒,但没有礼品盒产品。除非你这样做,否则这些产品将单独包装">

我希望这是有道理的。感觉我在这里兜圈子。。

这是我需要帮助的代码:

function product_tag_count( $term ) {
$tag_count = 0;
foreach( WC()->cart->get_cart() as $cart_item ) {
if ( has_term( $term, 'product_tag', $cart_item['product_id'] ) )
$tag_count += $cart_item['quantity'];
}
return $tag_count == 0 ? false : $tag_count;
}
function display_cart_counters() {
ob_start();
$link  = wc_get_checkout_url();
$title = __( 'Checkout and Pay', 'woocommerce' );
$tag_count = __( '0 products', 'woocommerce');
echo '<a class="product-cart-count" href="' . $link . '" title="' . $title . '">' . $tag_count . '</a>';
return ob_get_clean();
}
add_filter( 'woocommerce_add_to_cart_fragments', 'counters_add_to_cart_fragment', 30, 1 );
function counters_add_to_cart_fragment( $fragments ) {

ob_start();
// count the number of giftboxes
$giftbox4 = 'giftbox-4';
$fits_in_giftbox4 = '4';
$count_giftbox4 = product_tag_count( $giftbox4 );
$giftbox9 = 'giftbox-9';
$fits_in_giftbox9 = '9';
$count_giftbox9 = product_tag_count( $giftbox9 );
$giftbox16 = 'giftbox-16';
$fits_in_giftbox16 = '16';
$count_giftbox16 = product_tag_count( $giftbox16 );
$giftbox24 = 'giftbox-24';
$fits_in_giftbox24 = '24';
$count_giftbox24 = product_tag_count( $giftbox24 );

// get the total number of giftboxes
$count_giftboxes_total = ( $count_giftbox4 + $count_giftbox9 + $count_giftbox16 + $count_giftbox24 );
// count how many products that fits into giftboxes
$fits_in_giftbox = 'goes-into-box';
$count_fits_in_giftbox = product_tag_count( $fits_in_giftbox );
// count the cart as a whole
$cart_count = WC()->cart->get_cart_contents_count();
// calculate the difference between cart as a whole and giftbox products
$difference_between_cart_and_giftbox_products = ( $cart_count - $count_fits_in_giftbox );
// calculate boxes VS giftbox products
$fits_into_boxes_vs_giftbox_products = ( $count_giftboxes_total - $count_fits_in_giftbox );

// this is what I need help with.. how do I get the total pieces of all boxes added to cart?

// first check, is there space left or not?
if ( $count_giftboxes_total < $count_fits_in_giftbox ) {
$output = sprintf (_n( 'You have %d product to much. This will be packed separately unless you add another giftbox.', 'You have %d products to many. These will be packed separately unless you add another giftbox.' ), 'woocommerce');
} elseif () {
$output = sprintf (_n( 'You have %d product to much. This will be packed separately unless you add another giftbox.', 'You have %d products to many. These will be packed separately unless you add another giftbox.' ), 'woocommerce');
} elseif () {
$output = sprintf (_n( 'You have %d product to much. This will be packed separately unless you add another giftbox.', 'You have %d products to many. These will be packed separately unless you add another giftbox.' ), 'woocommerce');
} else {
$output = sprintf (_n( 'You have %d product to much. This will be packed separately unless you add another giftbox.', 'You have %d products to many. These will be packed separately unless you add another giftbox.' ), 'woocommerce');
}

$checkout_link = wc_get_checkout_url();
$link_title = __( 'Go to Checkout', 'woocommerce' );
?>
<div class="product-wrapper">
<?php echo $output; ?>
<br>
<a class="product-cart-count" href="<?php echo $link; ?>"
title="<?php echo $title ?>">Go to Checkout &amp; Pay</a>
</div>

<?php 

$fragments['a.product-cart-count'] = ob_get_clean();
return $fragments;
}
add_action( 'woocommerce_before_shop_loop', 'count_products_and_boxes');
add_action( 'woocommerce_before_add_to_cart_form', 'count_products_and_boxes');
function count_products_and_boxes() {
echo display_cart_counters();
}

这将根据购物车中礼品盒的数量和它们可以包含的产品数量来调整消息。消息已更改:

  • 当购物车包含产品,但没有礼品盒时
  • 当购物车包含礼品盒,但没有产品时
  • 当购物车中的产品超过礼品盒所能容纳的数量时
  • 当购物车中的产品少于礼品盒所能容纳的数量时

请注意,我使用了4个礼品盒的产品ID,否则会使其变得不必要的复杂。

function display_cart_counters() {
// Settings
$term = 'fits-in-giftbox';
$taxonomy = 'product_tag';

// Giftbox IDs. Important, these should not contain the term 'fits-in-giftbox' !!
// Contents => Product ID
$giftbox_ids = array( 
4  => 218,
9  => 220,
16 => 813,
24 => 815 
);

// Initialize
$count_fits_in_giftbox = 0;
$count_giftboxes = 0;
$count_giftboxes_total = 0;
$count_not_term = 0;
$message = '';

// 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
$count_fits_in_giftbox += $cart_item_quantity;
// The box ID is in the cart
} elseif( in_array( $product_id, $giftbox_ids ) ) {
// Add 1 * cart item quantity to counter
$count_giftboxes += 1 * $cart_item_quantity;

// Add contens * the cart item quantity to the counter
$count_giftboxes_total  += ( array_search ( $product_id, $giftbox_ids ) * $cart_item_quantity );
// Does not contain the particular term and is not a gift box
} else {
// Add the cart item quantity from this certain product to the counter
$count_not_term += $cart_item_quantity;
}
}
}

// Singular or plural
$s_o_p_giftbox = _n( '%d giftbox', '%d giftboxes', $count_giftboxes, 'woocommerce' );
$s_o_p_product = _n( '%d product', '%d products', $count_fits_in_giftbox, 'woocommerce' );

// Additional message for untagged products
if ( $count_not_term >= 1 ) { 
$untagged_product_message = ' You also added %s untagged products, these will be packaged separately.';
} else {
$untagged_product_message = '';
$count_not_term = '';
}

// When the cart contains products, but no giftbox(es)
if ( $count_giftboxes == 0 && $count_fits_in_giftbox >= 1 ) {
$message = sprintf( 
__( 'You have added ' . $s_o_p_giftbox . ' to your cart but ' . $s_o_p_product . '. Unless you do, these products will be packed separately.' . $untagged_product_message . '', 'woocommerce' ), 
$count_giftboxes, 
$count_fits_in_giftbox,
$count_not_term
);      
// When the cart contains giftbox(es), but no products
} elseif ( $count_giftboxes >= 1 && $count_fits_in_giftbox == 0 ) {
$message = sprintf( 
__( 'You have added ' . $s_o_p_giftbox . ' to your cart but no giftbox products. Unless you do, these products will be packed separately.' . $untagged_product_message . '', 'woocommerce' ), 
$count_giftboxes,
$count_not_term
);
// When there are more products in cart than giftboxes can contain
} elseif( $count_giftboxes_total < $count_fits_in_giftbox ) {
$message = sprintf( 
__( 'You have added ' . $s_o_p_giftbox . ' which can hold %d pieces. You have added ' . $s_o_p_product . ' to your cart. The exceeding %d products will be packed separately unless you add another giftbox to your cart.' . $untagged_product_message . '', 'woocommerce' ),
$count_giftboxes, 
$count_giftboxes_total, 
$count_fits_in_giftbox, 
$count_fits_in_giftbox - $count_giftboxes_total,
$count_not_term
);
// When there are less products in cart than giftboxes can contain
} elseif( $count_giftboxes_total > $count_fits_in_giftbox ) {
$message = sprintf( 
__( 'You have added ' . $s_o_p_giftbox . ' which can hold %d pieces. You have added ' . $s_o_p_product . ' to your cart. Please add an additional %d products to fill your boxes.' . $untagged_product_message . '', 'woocommerce' ), 
$count_giftboxes, 
$count_giftboxes_total, 
$count_fits_in_giftbox, 
$count_giftboxes_total - $count_fits_in_giftbox,
$count_not_term
);
}

return $message;
}
// Refreshing on cart ajax events
function filter_woocommerce_add_to_cart_fragments( $fragments ) {    
$fragments['div.display-cart-counters'] = '<div class="display-cart-counters">' . display_cart_counters() . '</div>';

return $fragments;        
}
add_filter( 'woocommerce_add_to_cart_fragments', 'filter_woocommerce_add_to_cart_fragments', 10, 1 );
// Display message via the desired hooks
function display_message() {
echo '<div class="display-cart-counters">' . display_cart_counters() . '</div>';
}
add_action( 'woocommerce_before_shop_loop', 'display_message', 10, 0 );
add_action( 'woocommerce_before_add_to_cart_form', 'display_message', 10, 0 );

相关内容

  • 没有找到相关文章

最新更新