owebia / magento2-module-advanced-shipping

Other
91 stars 29 forks source link

get custom magento 2 attribute value type decimal #124

Open alamkhatri opened 1 month ago

alamkhatri commented 1 month ago

Hi, How can i get custom attribute value. Best reagrds. A. L.

owebia commented 1 month ago

Hi, Did you try with your custom attribute as with the name attribute in the documentation?
https://owebia.com/doc/en/magento2-module-advanced-shipping#ref:var:items

alamkhatri commented 1 month ago

I tried this: addMethod('id_003', [ 'title' => "Frais de port en fonction du poids volumique", 'enabled' => true, // Toujours activé pour ce test 'price' => array_reduce($request->all_items, function ($carry, $item) { // On suppose que l'attribut 'poidvolumique' est une propriété du produit $poidsvolumique = isset($item->product->poidsvolumique) ? (float)$item->product->poidsvolumique : 0; // Logique de calcul des frais de port, par exemple 1.5 euros par unité de poids volumique $carry += $poidsvolumique * 1.5; return $carry; }, 0) ]); but instead to have 3(poidsvolumique=2) i have 6 as result. Best reagrds. A Lamkhatri

owebia commented 1 month ago

Are you using simple or bundle products?

Did you try to enable the debug option and then make a shipping estimation?
In the debug output (Magento backend, where the module configuration is), you can see the details of the calculation

alamkhatri commented 1 month ago

Hi, It is a configurable product. in the log file i have the right value: 'poidsvolumique' => '2.000000' But the result is 6 instead of 3:

addMethod('id_003', [
    'title' => "Frais de port en fonction du poids volumique",
    'enabled' => true,
    // Toujours activé pour ce test
    'price' => array_reduce($request->all_items, function ($carry, $item) {
        $poidsvolumique = isset($item->product->poidsvolumique) ? (float) $item->product->poidsvolumique : 0;
        $carry += $poidsvolumique * 1.5;
        return $carry;
    }, 0),
])
owebia commented 1 month ago

Configurable products are handle in a special way by Magento: two items are added in the cart; one for the parent and one for the child. Depending on what you want to achieve, you will need to get data from one or the other.

You can use $item->product_type == 'configurable' and $item->parent_item_id != 0 to include/exclude the parent or the child.

Best Regards, A.L.

alamkhatri commented 1 month ago

Thank you for your help. But i have also simple products. How can i manage this? Best regards. A Lamkhatri

owebia commented 1 month ago

You can check if the product is configurable or not with $item->product_type == 'configurable' and make a different process for configurable & simple products in the array_reduce callback function.

alamkhatri commented 1 month ago

i have a problem when i have the same item twice in kart this script doesn't give the right result he doesn't double the shipping fees( configurable product): addMethod('id_003', [ 'title' => "DHL Express", 'enabled' => $request->dest_country_id == 'FR' && substr($request->dest_postcode, 0, 2) !== '20', 'price' => array_reduce($request->all_items, function ($carry, $item) { if ($item->product_type == 'simple') { $poidsvolumique = isset($item->product->poidsvolumique) ? (float)$item->product->poidsvolumique : 0; $quantity = $item->qty; // Quantité du produit // Tranches de valeur du poids volumique if ($poidsvolumique < 2.1) { $carry += 21 $quantity; // Frais d'expédition pour un poids volumique inférieur à 2.1 kg } elseif ($poidsvolumique < 2.4) { $carry += 26 $quantity; // Frais d'expédition pour un poids volumique entre 2.1 et 2.4 kg } elseif ($poidsvolumique < 3) { $carry += 29 $quantity; // Frais d'expédition pour un poids volumique entre 2.4 et 3 kg } elseif ($poidsvolumique < 4) { $carry += 35 $quantity; // Frais d'expédition pour un poids volumique entre 3 et 4 kg } else { $carry += 40 * $quantity; // Frais d'expédition pour un poids volumique entre 4 et 5 kg } } return $carry; }, 0) ]);

owebia commented 1 month ago

As explained in my message, Magento put some data on the parent configurable item and other data on the child simple item.

Have you tried to replace

if ($item->product_type == 'simple') {

by something like

if ($item->product_type == 'configurable' || $item->product_type == 'simple' && $item->parent_item_id == 0) {
owebia commented 1 month ago

If it doesn't work, you will have to define a function like here: https://github.com/owebia/magento2-module-advanced-shipping/issues/86#issuecomment-706215587

alamkhatri commented 1 month ago

Thank you very much.