owebia / magento2-module-advanced-shipping

Other
90 stars 28 forks source link

Syntax error Issue writting a custom method #111

Closed Minirock closed 2 years ago

Minirock commented 2 years ago

Hi,

I have a tough rule to write and i'm kinda struggling. Some help would be appreciated.

I'm taking the following error :

Carrier[owsh1].getConfig - Error - Syntax error, unexpected '}' on line 43

So, the enable part seems to be written properly, but i'm failing on the price part witht he array map writting; he doesn't seems to like the closing } for a reason i can't tell.

The goal being is to sum price per item depending on their weigh with a max price of 100. Thanks for assist !

//pièces détachées uniquement, en France Métropolitaine  avec pièces non mécanisable (HORS MOTEUR ET BV)
addMethod('geodis_detached_fr_no_meca_no_motor', [
    'title' => "Geodis à domicile en France",
    'enabled' => (
        $request->dest_country_id == 'FR' &&
        !preg_match('/^20\\d{3}$/', $request->dest_postcode) &&
        (
            count(
                array_filter($request->all_items, function ($item) {
                    return $item->product->attribute_set->attribute_set_name == 'Default';
                })
            ) == count($request->all_items)
        ) &&
        (
            count(
                array_filter($request->all_items, function ($item) {
                    return !$item->product->tws_piece_non_mecanisable;
                })
            ) > 0
        ) &&
        (
            count(
                array_filter($request->all_items, function ($item) {
                    return
                        in_array(175, $item->product->category_ids) ||
                        in_array(200, $item->product->category_ids) ||
                        in_array(201, $item->product->category_ids);
                })
            ) == 0
        )
    ),
    'price' =>
        max(
            array_sum(
                array_map(
                    function ($item) {
                        $item->product->tws_piece_non_mecanisable ?
                            $item->product->weight < 5 ? 5.9 :
                                $item->product->weight >= 5 && $item->product->weight < 10 ? 7.9 :
                                    $item->product->weight >= 10 && $item->product->weight < 20 ? 10.9 :
                                        $item->product->weight >= 20 && $item->product->weight < 30 ? 14.9
                        : $item->product->weight < 20 ? 19.9 : 24.9
                    },
                    $request->all_items
                )
            ),
            100
        ),
]);
owebia commented 2 years ago

Hi,

There is no return in the last function and no ; at the end of the function.
That's why you have this error.

Best Regards, A.L.

Minirock commented 2 years ago

Thanks for the feedback !

Initially i was using returns in the function like

$item->product->weight < 5 ? return 5.9; :

But this syntax was causing an issue too. How could you integrate that return exactly ?

owebia commented 2 years ago

You should do something like this:

                array_map(
                    function ($item) {
                        return $item->product->tws_piece_non_mecanisable ?
                            ($item->product->weight < 5 ? 5.9 :
                                ($item->product->weight >= 5 && $item->product->weight < 10 ? 7.9 :
                                    ($item->product->weight >= 10 && $item->product->weight < 20 ? 10.9 :
                                        ($item->product->weight >= 20 && $item->product->weight < 30 ? 14.9
                        : ($item->product->weight < 20 ? 19.9 : 24.9)))));
                    },
                    $request->all_items
                )
Minirock commented 2 years ago

This is what i did initially but it throw

Carrier[owsh1].getConfig - Error - Syntax error, unexpected ';' on line 42

And if i remove it

Carrier[owsh1].getConfig - Error - Syntax error, unexpected '}' on line 43

owebia commented 2 years ago

Ok, I had not seen it before but the first ternary operator $item->product->tws_piece_non_mecanisable ? ... : ... is incomplete.

The third part is missing (: ...)

owebia commented 2 years ago

It's a personnal taste but I find it easier to read when written like this:

                    function ($item) {
                        return $item->product->tws_piece_non_mecanisable
                            ? (
                                $item->product->weight < 5
                                ? 5.9
                                : (
                                    $item->product->weight >= 5 && $item->product->weight < 10
                                    ? 7.9
                                    : (
                                        $item->product->weight >= 10 && $item->product->weight < 20
                                        ? 10.9
                                        : (
                                            $item->product->weight >= 20 && $item->product->weight < 30
                                            ? 14.9
                                            : ($item->product->weight < 20 ? 19.9 : 24.9)
                                        )
                                    )
                                )
                            )
                            : /* missing part */;
                    },
Minirock commented 2 years ago

Indeed, that's because i don't really have a use case for this one it can't happen.

And originally i was writting this piece with if elseif but looks like the if syntax wasn't very appreciated thats why i changed.

I guess i can try to put a number for the missing part, i will never get into it anyway.

Thanks for your support :)

I will try your way

Minirock commented 2 years ago

@owebia Thanks for your support, it's finally working like a charm !

Here is the full method and your writting style indeed makes it easier to debug

I only had to add a semi colon around global parenthesis for the return. Here is full code

//pièces détachées uniquement, en France Métropolitaine  avec pièces non mécanisable (HORS MOTEUR ET BV)
addMethod('geodis_detached_fr_no_meca_no_motor', [
    'title' => "Geodis à domicile en France",
    'enabled' => (
        $request->dest_country_id == 'FR' &&
        !preg_match('/^20\\d{3}$/', $request->dest_postcode) &&
        (
            count(
                array_filter($request->all_items, function ($item) {
                    return $item->product->attribute_set->attribute_set_name == 'Default';
                })
            ) == count($request->all_items)
        ) &&
        (
            count(
                array_filter($request->all_items, function ($item) {
                    return !$item->product->tws_piece_non_mecanisable;
                })
            ) > 0
        ) &&
        (
            count(
                array_filter($request->all_items, function ($item) {
                    return
                        in_array(175, $item->product->category_ids) ||
                        in_array(200, $item->product->category_ids) ||
                        in_array(201, $item->product->category_ids);
                })
            ) == 0
        )
    ),
    'price' =>
        max(
            array_sum(
                array_map(
                    function ($item) {
                        return ($item->product->tws_piece_non_mecanisable 
                          ?(
                                $item->product->weight < 5 
                                ? 5.9 
                                :(
                                    $item->product->weight >= 5 && $item->product->weight < 10 
                                        ? 7.9 
                                        :(
                                            $item->product->weight >= 10 && $item->product->weight < 20 
                                            ? 10.9 
                                            :( 
                                                $item->product->weight >= 20 && $item->product->weight < 30 
                                                ? 14.9 
                                                : 0
                                            )
                                        )
                                )
                            )
                            :(
                                $item->product->weight < 20 
                                    ? 19.9 
                                    : 24.9
                            ));
                    },
                    $request->all_items
                )
            ),
            100
        ),
]);