Untitled

 avatar
unknown
plain_text
a year ago
1.4 kB
3
Indexable
function wildrobot_enklere_valg_freight_price_adjustment(array $rates, array $package): array
{
    /* @var $product WC_Product_Simple */
    foreach ($rates as $key => $shipping_rate) {
        $cost = (float) $shipping_rate->get_cost();
        if ($cost == 0) {
            continue;
        }
        $added_cost = 0;
        foreach ($package['contents'] as $product) {
            // Varmefordelingsplate 20 mm = 2787
            if ($product['product_id'] === 2787) {
                $added_cost += 4 * $product['quantity'];
            }
            // Varmefordelingsplate 17 mm = 2785
            if ($product['product_id'] === 2785) {
                $added_cost += 3 * $product['quantity'];
            }
            // Varmefordelingsplate 16 mm = 2779
            if ($product['product_id'] === 2779) {
                $added_cost += 3 * $product['quantity'];
            }
        }
        // Set the new cost
        $new_cost = $cost + $added_cost;
        $shipping_rate->set_cost($new_cost);

        // Recalculate taxes
        $taxes = $shipping_rate->get_taxes();
        $new_taxes = array();
        foreach ($taxes as $tax_id => $tax) {
            $new_taxes[$tax_id] = $tax * ($new_cost / $cost);
        }
        $shipping_rate->set_taxes($new_taxes);
    }
    return $rates;
}

add_filter('woocommerce_package_rates', 'wildrobot_enklere_valg_freight_price_adjustment', 10, 2);
Editor is loading...
Leave a Comment