Useful code snippets

Add amazon shipping costs to product prices

add_filter('cegg_view_data_prepare', 'add_amazon_shipping', 10, 4);

function add_amazon_shipping($data, $module_id, $post_id, $params)
{
    // Check if the module is related to Amazon
    if (strpos($module_id, 'Amazon') === false) {
        return $data;
    }

    // Loop through the items and add shipping cost
    foreach ($data as $key => $item) {
        $shipping = calculate_amazon_shipping($item); // Custom function to calculate shipping
        $data[$key]['price'] += $shipping; // Add shipping cost to the price
        $data[$key]['shipping_cost'] = $shipping; // Store shipping cost in data array
    }

    return $data;
}


function calculate_amazon_shipping($item)
{
    // Example price-based shipping rates (adjust as necessary)
    $price = isset($item['price']) ? $item['price'] : 0;
    
    if ($price >= 29) {
        // Free shipping for orders over €29
        return 0;
    } elseif ($price >= 10) {
        // Standard shipping rate for orders between €10 and €29
        return 2.99;
    } else {
        // Higher shipping rate for orders under €10
        return 4.99;
    }
}

Last updated