-
AuthorPosts
-
Hello,
I have implemented Woo Wallet and create below code for following requirements:
1. Wallet amount will applicable on specified multiple categories only (Ex. Cat A, Cat B, Cat C)
2. Wallet amount will usable if specific category total reaches to required threshold (Ex: 4999)
3. Only 50% of total wallet balance is usable at single order/ checkout. (Ex: User wallet balance is 500, but only 250 is usable at a time)But this code work if a single product from any specified categories has been added to cart or having multiple quantities.
Requirement: I want that, if a user add other products too from any different categories but the specified categories having total of 4999 or more. So they are eligible to use 250 from wallet. Else, wallet balance is not usable.
Code Issue: This code doesn’t work if multiple products under mentioned category has been added; and if multiple products from different category has been added to cart.
// wallet condition
add_filter(‘woo_wallet_partial_payment_amount’, ‘woo_wallet_partial_payment_amount_callback’, 100);
function woo_wallet_partial_payment_amount_callback($amount) {
if (sizeof(WC()->cart->get_cart()) > 0) {
$totals = WC()->cart->get_totals();global $woocommerce;
$unset = false;
$category_ids = array(‘3192’, ‘3193’, ‘3194’);
foreach ( $woocommerce->cart->cart_contents as $key => $values ) {
$terms = get_the_terms( $values[‘product_id’], ‘product_cat’ );
foreach ( $terms as $term ) {
if ( in_array( $term->term_id, $category_ids ) ) {if($totals[‘subtotal’] > 4999){
$cart_total = $totals[‘subtotal’] + $totals[‘shipping_total’] + $totals[‘subtotal_tax’] + $totals[‘shipping_tax’];
$amount = ($cart_total * 50) / 100;
if ($amount > woo_wallet()->wallet->get_wallet_balance(get_current_user_id(), ‘edit’)) {
$amount = woo_wallet()->wallet->get_wallet_balance(get_current_user_id(), ‘edit’) < 250 ? woo_wallet()->wallet->get_wallet_balance(get_current_user_id(), ‘edit’) : 250;
}
}else{
$amount = 0;
}}else{
$amount = 0;
}
}
}}
return $amount;
}
I’m feeling glad if someone help me on this.
- This topic was modified 4 years, 4 months ago by rohitpawarindia.
Tried modifying the code, but still getting failed:
// wallet condition
add_filter(‘woo_wallet_partial_payment_amount’, ‘woo_wallet_partial_payment_amount_callback’, 100);
function woo_wallet_partial_payment_amount_callback($amount) {
if (sizeof(WC()->cart->get_cart()) > 0) {
$totals = WC()->cart->get_totals();function check_product_in_cart($cat_id) {
global $woocommerce;
// id of targeted category is 5 for example$product_count = 0;
// start of the loop that fetches the cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values[‘data’];
$terms = get_the_terms( $_product->id, ‘product_cat’ );// second level loop search, in case some items have several categories
foreach ($terms as $term) {
$_categoryid = $term->term_id;
if ( $_categoryid === $cat_id ) {//$product_count=+ 1 * $_product_qty;
$product = $values[‘data’];
$product_id = $values[‘product_id’];
$quantity = $values[‘quantity’];
$price = WC()->cart->get_product_price( $product );$product_count += WC()->cart->get_product_subtotal( $product, $values[‘quantity’] );
}
}
}return $product_count;
}// count
$total1 = check_product_in_cart(3192);
$total2 = check_product_in_cart(3193);
$total3 = check_product_in_cart(3194);// make total
$final_total = $total1 + $total2 + $total3;if ( $final_total > 4999 ) {
$cart_total = $totals[‘subtotal’] + $totals[‘shipping_total’] + $totals[‘subtotal_tax’] + $totals[‘shipping_tax’];
$amount = ($cart_total * 50) / 100;
if ($amount > woo_wallet()->wallet->get_wallet_balance(get_current_user_id(), ‘edit’)) {
$amount = woo_wallet()->wallet->get_wallet_balance(get_current_user_id(), ‘edit’) < 250 ? woo_wallet()->wallet->get_wallet_balance(get_current_user_id(), ‘edit’) : 250;
}}else{
$amount = 0;
}}
return $amount;
}
-
AuthorPosts
- You must be logged in to reply to this topic.