owebia / magento2-module-advanced-shipping

Other
91 stars 29 forks source link

Product Attribute Condition is not working #109

Closed wajahatbashir closed 2 years ago

wajahatbashir commented 2 years ago

Hi,

I want to show multiple shipping method but on base of product attribute condition. But this is not working for me.

Preorder is product attribute code. and it's dropdown.

I am trying to add this condition ( If Preorder is No and Product is Out of Stock then this custom shipping method should be display.) But this condition is not working. no shipping method is loading.

if (($item->product->getAttributeText('preorder') == 'No') && ($item->product->stock_item->is_in_stock == 'No')) {
    addMethod('id_0026', [
        'title' => "In-Stock",
        'price' => 130,
    ]);
}

Can you please correct this condition ? I am using Magento 2.4.0.

owebia commented 2 years ago

Hi,

It seems that your preorder attribute is of type Yes/No.

Have you tried to use it like a boolean?

if ($item->product->preorder && ...

You can also activate the debug option to understand how the configuration is evaluated.

A.L.

wajahatbashir commented 2 years ago

Hi,

Yes, preorder attribute is a type Yes/No, Please check this screenshot link.

https://imgur.com/0sif8Of

Can you please correct this condition ?

if ($item->product->preorder == 0) OR if ($item->product->preorder == 'No')

I have tried both of this but not working.

and which condition i'll use for Product is In-stock or Out of stock ?

I have activate the debug option, that's coming empty.

Regards,

wajahatbashir commented 2 years ago

In debugging getting this error : Unknown variable $item Debug:

if ($item->product->preorder == 'No') {
    addMethod('id_0026', ['title' => "In-Stock", 'price' => 130]);
} else {
    addMethod('id_0026', ['title' => "Free Shipping", 'price' => 0]);
}

preorder
product
$item
Unknown variable $item
wajahatbashir commented 2 years ago

Hi Team,

I have write this code But this function is not working twice : $request->package_qty

Code screenshot link: https://imgur.com/rXUwEuw

This Code is working fine:

if ($quote->base_subtotal <= 364 ) {
    addMethod('id_0025', [
        'title' => "Flat Rate In-Stock",
        'enabled' => count(
                array_filter($request->all_items, function ($item) {
                return $item->product->preorder == 0;
            })
        ) == $request->package_qty,
        'price' => 130,
    ]);
}

Now in below code $request->package_qty is not working, when i remove this line then code is working fine.

if ($quote->base_subtotal > 364 ) {
    addMethod('id_0026', [
        'title' => "Free In-Stock",
        'enabled' => count(
                array_filter($request->all_items, function ($item) {
                return $item->product->preorder == 0;
            })
        ) == $request->package_qty,
        'price' => 0
    ]);
}
owebia commented 2 years ago

In debugging getting this error : Unknown variable $item Debug:

if ($item->product->preorder == 'No') {
    addMethod('id_0026', ['title' => "In-Stock", 'price' => 130]);
} else {
    addMethod('id_0026', ['title' => "Free Shipping", 'price' => 0]);
}

preorder
product
$item
Unknown variable $item

$item variable cannot be used outside of a loop as it comes from $request->all_items that is an collection of items

owebia commented 2 years ago

Now in below code $request->package_qty is not working, when i remove this line then code is working fine.

Why do you say it is not working? Which error do you have?

wajahatbashir commented 2 years ago

Now in below code $request->package_qty is not working, when i remove this line then code is working fine.

Why do you say it is not working? Which error do you have?

Debugging Code Screenshot: https://imgur.com/z6cCYQU

==> there is not showing any bug or error in debugger, But shipping method is not showing. But when i just remove this line of code $request->package_qty

Then shipping method is showing.

Actual code with issue:

if ($quote->base_subtotal > 364 ) {
    addMethod('id_0026', [
        'title' => "Free In-Stock",
        'enabled' => count(
                array_filter($request->all_items, function ($item) {
                return $item->product->preorder == 0;
            })
        ) == $request->package_qty,    // Error in this line
        'price' => 0
    ]);
}
owebia commented 2 years ago

The debug tells you why the enabled condition is evaluated to false.

Have you checked this.

You do not use $item->qty in your condition. Maybe that's the reason why the count does not match $request->package_qty.

A.L.

On Thu, Jul 21, 2022, 13:08 Wajahat Bashir @.***> wrote:

Now in below code $request->package_qty is not working, when i remove this line then code is working fine.

Why do you say it is not working? Which error do you have?

Debugging Code Screenshot: https://imgur.com/z6cCYQU

==> there is not showing any bug or error in debugger, But shipping method is not showing. But when i just remove this line of code $request->package_qty

Then shipping method is showing.

Actual code with issue:

if ($quote->base_subtotal > 364 ) { addMethod('id_0026', [ 'title' => "Free In-Stock", 'enabled' => count( array_filter($request->all_items, function ($item) { return $item->product->preorder == 0; }) ) == $request->package_qty, 'price' => 0 ]); }

— Reply to this email directly, view it on GitHub https://github.com/owebia/magento2-module-advanced-shipping/issues/109#issuecomment-1191352090, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAETURLRYV2YSMT3VDGXD3TVVEVRRANCNFSM54DHQYBA . You are receiving this because you commented.Message ID: @.*** com>

wajahatbashir commented 2 years ago

I could no find why count is showing false in this code.

2 things i have noticed

  1. when I'll use <= symbol in condition then it's working fine But not working with >= symbol
  2. or when I'll remove this line of code == $request->package_qty, then it's working fine with >= symbol
if ($quote->base_subtotal > 364 ) {
    addMethod('id_0026', [
        'title' => "Free In-Stock",
        'enabled' => count(
                array_filter($request->all_items, function ($item) {
                return $item->product->preorder == 0;
            })
        ) == $request->package_qty,
        'price' => 0,
    ]);
}

Kindly help to fix this issue, what's going wrong with this code ?

owebia commented 2 years ago

Hi,

I suppose that you are using a single item in the cart with a quantity greater than 1 to test the condition > 364.

But your code does not take into account the quantity of items.

You should use something like this instead:

                    array_sum(
                        array_map(
                            function ($item) {
                                return $item->product->preorder == 0 ? $item->qty : 0;
                            },
                            $request->all_items
                        )
                    )

A.L.

wajahatbashir commented 2 years ago

Thanks a lot, this code is working perfectly. :)