Case 1: Repeat field or section based on product quantity of a specific category.
If you need to repeat a field or section based on the quantity of products in a specific category, you can use below code snippet.
add_filter('thwcfe_repeat_times', 'th27mb_override_repeat_count', 10, 3);
function th27mb_override_repeat_count($rt, $name, $type){
global $woocommerce;
$count = array();
$cart_object = WC()->cart->get_cart();
$category = 'accessories'; // category slug
if($cart_object){
foreach($cart_object as $cart_item ) {
if(has_term( $category, 'product_cat', $cart_item['product_id'])){
$qty = $cart_item['quantity'];
array_push($count,$qty);
}
}
}
if(empty($count)){
return $rt;
}
if($type == 'field'){
if($name === 'test_field'){ // update field name
$rt = array_sum($count);
}
}elseif($type == 'section'){
if($name === 'test_section'){ // update section name
$rt = array_sum($count);
}
}
return $rt;
}
Case 2: Repeat field or section based on the largest quantity of product in cart items.
If you need to repeat a field or section, you can add a repeat rule in field or section settings.
Recently there is a request to modify the repeat count based on the large quantity of products in cart items.
add_filter('thwcfe_repeat_times', 'th34ed_override_repeat_count', 10, 3);
function th34ed_override_repeat_count($rt, $name, $type){
global $woocommerce;
$cart_count = array();
$cart_object = WC()->cart->get_cart();
if($cart_object){
foreach($cart_object as $cart_item ) {
$qty = $cart_item['quantity'];
array_push($cart_count,$qty);
}
}
if(empty($cart_count)){
return $rt;
}
if($type == 'field'){
if($name === 'test_field'){ // update your field name
$rt = max($cart_count);
}
}elseif($type == 'section'){
if($name === 'test_section'){ // update your section name
$rt = max($cart_count);
}
}
return $rt;
}
Comments
0 comments
Please sign in to leave a comment.