Untitled

 avatar
webwizards
plain_text
a month ago
4.2 kB
3
Indexable
add_filter( 'woocommerce_bundle_price_data', 'b2bking_custom_bundle_form_data_with_sale_prices', 99, 2 );
function b2bking_custom_bundle_form_data_with_sale_prices( $data, $bundle ) {

    if ( ! is_product() ) return $data;
    if ( ! is_array( $data ) ) return $data;

    // Override the bundle parent's base price with its sale price
    $base_sale = $bundle->get_sale_price();
    if ( $base_sale !== '' && $base_sale !== null && (float) $base_sale > 0 ) {
        $data['base_price'] = (float) $base_sale;
    }

    // Override each bundled item's price with the sale price
    if ( ! empty( $data['product_ids'] ) && is_array( $data['product_ids'] ) ) {

        $min_sum = 0.0;
        $max_sum = 0.0;

        foreach ( $data['product_ids'] as $bundled_item_id => $product_id ) {

            $product = wc_get_product( $product_id );
            if ( ! $product ) continue;

            $sale = $product->get_sale_price();

            if ( $sale !== '' && $sale !== null && (float) $sale > 0 ) {
                $data['prices'][ $bundled_item_id ] = (float) $sale;
            }

            $price = isset( $data['prices'][ $bundled_item_id ] ) ? (float) $data['prices'][ $bundled_item_id ] : 0.0;
            $min_q = isset( $data['quantities_min'][ $bundled_item_id ] ) ? (int) $data['quantities_min'][ $bundled_item_id ] : 1;
            $max_q = isset( $data['quantities_max'][ $bundled_item_id ] ) ? (int) $data['quantities_max'][ $bundled_item_id ] : $min_q;

            $min_sum += $price * $min_q;
            $max_sum += $price * $max_q;
        }

        if ( isset( $data['raw_bundle_price_min'] ) ) {
            $data['raw_bundle_price_min'] = (float) $data['base_price'] + $min_sum;
        }
        if ( isset( $data['raw_bundle_price_max'] ) && $data['raw_bundle_price_max'] !== '' ) {
            $data['raw_bundle_price_max'] = (float) $data['base_price'] + $max_sum;
        }
    }

    return $data;
}


add_filter( 'pre_option_woocommerce_tax_round_at_subtotal', function( $val ) {
    return 'yes'; 
} );

add_action( 'woocommerce_cart_calculate_fees', 'b2bking_custom_bundle_discount_fee', 99, 1 );
function b2bking_custom_bundle_discount_fee( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    if ( ! is_object( $cart ) ) return;
    if ( ! function_exists( 'b2bking' ) ) return;

    // Only apply at checkout, not on cart
    $is_checkout_context = is_checkout()
        || ( isset( $_REQUEST['wc-ajax'] ) && $_REQUEST['wc-ajax'] === 'update_order_review' );

    if ( ! $is_checkout_context ) return;

    $decimals      = wc_get_price_decimals();
    $discount_incl = 0.0;
    $touched       = false;

    foreach ( $cart->get_cart() as $cart_item ) {

        $product = $cart_item['data'];
        if ( ! $product ) continue;

        $is_bundle_parent = isset( $cart_item['bundled_items'] );
        $is_bundle_child  = isset( $cart_item['bundled_by'] );
        if ( ! $is_bundle_parent && ! $is_bundle_child ) continue;

        $product_id = isset( $cart_item['variation_id'] ) && (int) $cart_item['variation_id'] !== 0
            ? (int) $cart_item['variation_id']
            : (int) $cart_item['product_id'];

        $rules = b2bking()->get_applicable_rules( 'discount_everywhere', $product_id );
        if ( $rules === 'norules' ) continue;

        $touched = true;
        $qty = (int) $cart_item['quantity'];

        $fresh = wc_get_product( $product_id );
        if ( ! $fresh ) continue;

        $regular_incl = (float) wc_get_price_to_display( $fresh, array( 'price' => $fresh->get_regular_price() ) );

        $sale_price = $fresh->get_sale_price();
        $sale_price = ( $sale_price === '' || $sale_price === null ) ? $fresh->get_regular_price() : $sale_price;
        $sale_incl  = (float) wc_get_price_to_display( $fresh, array( 'price' => $sale_price ) );

        if ( $regular_incl > $sale_incl ) {
            $discount_incl += round( $regular_incl - $sale_incl, $decimals ) * $qty;
        }
    }

    if ( ! $touched ) return;

    $discount_incl = round( $discount_incl, $decimals );

    if ( $discount_incl > 0 ) {
        $cart->add_fee( __( 'Pakkerabatt', 'b2bking' ), -1 * $discount_incl, false );
    }
}
Editor is loading...
Leave a Comment