Okay
  Public Ticket #3679522
Are there any way to identify location chosen when in cart, to add a virtual transportation cost product?
Closed

Comments

  • Gustav started the conversation

    First of all, thank you for the great plugin! We've just started our business and it's been a great start, our customers have specifically mentioned how great it is being able to book cleaning services this easily! 
    Now, I'm not a coder but I've leveraged AI to create some other scripts that has worked in cart. For example I have a script that adds a material cost, depending on the subtotal of the product made by Amelia.
    (I'm selling cleaning services)
    Now, we've expanded and when customers books in Amelia, they can choose between many nearby cities. Some cities are 30-50 minutes away, and I would like to add a transportation cost (a virtual product) depending on what city they choose.

    I've tried the WP Hooks like amelia_after_location_added but it doesn't seem to work. I assume it's because when in cart page, there hasn't been a booking added yet? An example of code I've tried: 

    function add_transportation_fee_for_falun($location) {
    // Check if the location is "Falun"
    if (stripos($location, 'Falun') !== false) {
    // Product ID for the transportation fee $product_id = 5424; $price = 80;

    // Get the product $product = wc_get_product($product_id);

    // Ensure the product exists and is valid
    if ($product) {
    // Add the product to the cart with the specified price
    $cart_item_data = array(
    'price' => $price,
    'custom_price' => $price,
    );
    WC()->cart->add_to_cart($product_id, 1, 0, array(), $cart_item_data);
    }
    }
    }

    // Hook into Amelia's action after location is added
    add_action('amelia_after_location_added', 'add_transportation_fee_for_falun', 10, 1);

    I've also tried it simplified to just echo "city found", but it never fires. I'm not really asking you to figure out the code for me but perhaps point me in the right direction on how I can go about identifying the city in the cart page, so that I can add a transportation cost? 

  •  700
    Stefan replied

    Hi Gustav,

    Thanks for reaching out to us. There might be a possibility to configure this, but I would need to consult the developers since it requires a bit of thinking "outside of the box" in terms of technical details. I will certainly let you know as soon as I have more information.

    Thank you for your patience and understanding. 

    Kind Regards, 

    Stefan Petrov
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  •  700
    Stefan replied

    Hi Gustav,

    You can try something like this:

    Use the amelia_before_wc_cart_filter filter, which is triggered just before the WooCommerce cart. You can find the selected location ID here.

    Then, based on the product ID for that location, you can add it to the cart.

    function checkLocation ($location) {    
        switch ($location) {        
        // add other locations here and their product ID        
            case "Falun":            
                return 5424;
            default:
                return null;
        }
    }
    function example($appointmentData)
    {    
        $price = 80;
        $container = require AMELIA_PATH . '/src/Infrastructure/ContainerConfig/container.php';
        $locationRepository = $container->get('domain.locations.repository');
        // get location object    
        $locationObj = $locationRepository->getById($appointmentData['locationId']);
        // get location name (eg Falun)    
        $location = $locationObj->getName()->getValue();
        // get product ID if you predefined these products in WooCommerce (we understood it like that eg 5424 for Falun)
        $product_id = checkLocation($location);
        // continue with your logic  
       if ($product_id) {
        $product = wc_get_product($product_id);
        // Ensure the product exists and is valid    
        if ($product) {
            // Add the product to the cart with the specified price
            $cart_item_data = array( 
               'price' => $price,
                'custom_price' => $price,
            );        
            WC()->cart->add_to_cart($product_id, 1, 0, array(), $cart_item_data);
        }
    }     
    return $appointmentData;
    }
    add_filter('amelia_before_wc_cart_filter', 'example', 10, 1);

    Hope this helps.



    Kind Regards, 

    Stefan Petrov
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables

  • Gustav replied

    Thank you so much!! It works now and it adds transportation costs depending on what city customers choose. Adding the price with the code didn't work, so I did this instead. Created virtual transportation-cost products for each city, with a fixed price on each. (originally I had 1 "transportation cost" product with a price of 0, and hoped the code would set it's price). So code looks like this

    // Function to check the location and return the product ID
    function checkLocation($location) {    
        switch ($location) {
            case "Falun":            
                return 5424;
            case "Avesta":
                return 5438;
            // Add other locations and their respective product IDs here
            // case "CityName": return ProductID;
            default:
                return null;
        }
    }
    // Function to add the city-specific transportation product to the cart
    function example($appointmentData) {
        // Load the Amelia container to access the location repository
        $container = require AMELIA_PATH . '/src/Infrastructure/ContainerConfig/container.php';
        $locationRepository = $container->get('domain.locations.repository');
        
        // Get the location object
        $locationObj = $locationRepository->getById($appointmentData['locationId']);
        
        // Get the location name (e.g., Falun)
        $location = $locationObj->getName()->getValue();
        
        // Get product ID for the chosen location
        $product_id = checkLocation($location);
        
        // If a valid product ID is found for the location, add the product to the cart
        if ($product_id) {
            $product = wc_get_product($product_id);
            
            // Ensure the product exists and is valid
            if ($product) {
                // Add the product to the cart with no additional custom data
                WC()->cart->add_to_cart($product_id, 1);
            }
        }
        
        return $appointmentData;
    }
  •  700
    Stefan replied

    Hi Gustav,

    Great to hear that! Thanks for the feedback. I will leave this ticket on Public so other users can see the workaround.  Anyway, let us know if you have any other questions or issues and we will kindly assist. 

    Kind Regards, 

    Stefan Petrov
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables