0

is there a way to set up variable on top of list of methods?

I setup a function (return bool) looking for a custom attribute > OK I display or hide each method on it > OK

exemple on one method :

//check if dangerous products
$allItems = $request->all_items;
$hasDangerousProduct = function ($allItems) use ($request) {
        $count = count(array_filter($allItems, function ($item) {
                                        return $item->product->dangerous_product == true;
                                    }
                )
            );
    if($count > 0){
        return true;
    }
    return false;
};

// France Relais Colis
addMethod('id_1', [
    'title'         => "Relais Colis",
    'enabled'       => in_array($request->dest_country_id, ['FR']) && $hasDangerousProduct($allItems) == false,
    'price'         => array_reduce([ [0,0.5,5.8], [0.51,1,6], [1.01,2,6.66], [2.01,3,6.95], [3.01,4,7.5], [4.01,5,8.10], [5.01,6,8.5], [6.01,7,8.72], [7.01,8,9.23], [8.01,9,9.9], [9.01,10,11], [10.01,11,12], [11.01,12,13], [12.01,13,13.5], [13.01,14,14], [14.01,15,15], [15.01,16,16], [16.01,17,17], [17.01,18,18] ], function ($carry, $item) use ($request) {
            if (isset($carry)) return $carry;
            if (isset($item[0]) && ($request->package_weight >= $item[0] && $request->package_weight <= $item[1])) {
               $carry = $item[2];
            }
            return $carry;
        }),
    'description'   => ""

])
->set('delivery_delay', 2);

I need to display a message if at least one method has been hidden depending on my function returning bool. Or If you prefer if this function return false, I have to display a message.

My idea is to get this result with ko and make my render template dynamic with it, but how to get a global information above shipping methids defined by Owebia ?

Thanks a lot !

1 Answer 1

0

Super naive option: at the end of your code

if ($hasDangerousProduct()) {addError('Some options are not available as your order contains restricted products');}

but of course you have no control over the look of the message and I haven't checked how translations might work.

Another "simple" way I might do it is to pass that information back up in all the methods - like you have ->set('delivery_delay', 2) you could also set hasDangerousProduct

This is stuck in the shippingmethod information rather than at a wider scope but it should be possible to use it to trigger showing a message in the knockout code?

1
  • Ho, txs for your reply!! This is exactly what I've done : option 2 > set hasDangerousProduct to all methods then loop in ko to set up local template boolean to manage my display... Not very sexy, but efficient ^^
    – Cedric410
    Commented Jun 1, 2021 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.