Untitled
unknown
plain_text
5 months ago
4.8 kB
4
Indexable
add_filter('b2bking_applicable_rules_products', 'b2bking_discount_rules_sale_price', 10, 5); function b2bking_discount_rules_sale_price($results, $rule_type, $product_id, $user_id, $categories_array){ $rules = $results[0]; if ($rule_type == 'discount_everywhere'){ $user_id = get_current_user_id(); $currentusergroupidnr = apply_filters('b2bking_b2b_group_for_pricing', b2bking()->get_user_group($user_id), $user_id, $product_id); $price_tiers = get_post_meta($product_id, 'b2bking_product_pricetiers_group_'.$currentusergroupidnr, true ); if (empty($price_tiers)){ $price_tiers = get_post_meta($product_id, 'b2bking_product_pricetiers_group_b2c', true ); } $product = wc_get_product($product_id); $price_tiers = b2bking()->convert_price_tiers($price_tiers, $product); if (!empty($price_tiers)){ // get cart qty of the product $quantity = 0; if (is_object( WC()->cart )){ // particularly in the case of product addons / options, there is the following issue: // for customized products with various options, their IDs are the same, just meta data are different // by searching the first ID in cart and breaking the foreach, all we're doing is forcing all products to use the qty of the first instance. // if there are multiple instances of the item, let's add up all quantities $instances = 0; // for variable products, sum up variations $possible_parent_id = wp_get_post_parent_id($product_id); $sum_up_variations = 'no'; if ($possible_parent_id !== 0){ $sum_up_variations = get_post_meta( $possible_parent_id, 'b2bking_tiered_sum_up_variations', true ); } foreach( WC()->cart->get_cart() as $cart_item ){ if ( $product_id === $cart_item['product_id'] || $product_id === $cart_item['variation_id']){ $quantity = apply_filters('b2bking_cart_item_quantity_tiers', $cart_item['quantity'], $cart_item['variation_id'], $cart_item['product_id']); $instances++; } if ($possible_parent_id !== 0 && $sum_up_variations === 'yes'){ // for variable products, sum up variations if enabled if ( $possible_parent_id === $cart_item['product_id']){ $quantity = apply_filters('b2bking_cart_item_quantity_tiers', $cart_item['quantity'], $cart_item['variation_id'], $cart_item['product_id']); $instances++; } } } if ($instances > 1){ $quantity = 0; // let's add up all the quantities together foreach( WC()->cart->get_cart() as $cart_item ){ if ( $product_id === $cart_item['product_id'] || $product_id === $cart_item['variation_id']){ $quantity += apply_filters('b2bking_cart_item_quantity_tiers', $cart_item['quantity'], $product_id); } else { if ($sum_up_variations === 'yes'){ if ( $possible_parent_id === $cart_item['product_id']){ $quantity += apply_filters('b2bking_cart_item_quantity_tiers', $cart_item['quantity'], $cart_item['variation_id']); } } } } } } // get first tier quantity to check if it has been reached $price_tiers = explode(';', $price_tiers); $quantities_array = array(); $prices_array = array(); // first eliminate all quantities larger than the quantity in cart foreach($price_tiers as $tier){ $tier_values = explode(':', $tier); if (count($tier_values) > 1){ if ($tier_values[0] <= $quantity && !empty($tier_values[0])){ array_push($quantities_array, $tier_values[0]); $prices_array[$tier_values[0]] = b2bking()->tofloat($tier_values[1]); } } } // if any number remains = tiered pricing applies if(count($quantities_array) !== 0){ $rules = array(); } } } return array($rules, $results[1]); } add_filter('b2bking_tiered_discount_everywhere_final', function($price_display, $price, $price_new){ return wc_price($price); }, 10, 3);
Editor is loading...
Leave a Comment