0

Onclick Category Link , We have to display all Products ( Only if the Product attribute [ Ready to Ship ] value is "YES". )

enter image description here

We don't want to display Products in that url when the Product attribute value is No.

enter image description here

We are using PWA Magento 2.4.5 Venia Theme.

attribute code :

ready_to_ship

I am using below code, what mistake i am doing ?

vendor/magento/module-catalog/view/frontend/templates/category - products.phtml

<?php if (!$block->isContentMode() || $block->isMixedMode()) :?>
    <?= $block->getProductListHtml() ?>
<?php endif; ?>

<?php

$objectManager = \Magento\Framework\App\objectManager::getInstance(); 

            $product = $objectManager->create('Magento\Catalog\Model\Product')->load($singleproductdata['entity_id']);
            $attributes = $product->getAttributes('ready_to_ship');
           foreach ($attributes as $attribute) { 

                    echo $attribute->getAttributeCode();   
                    echo $attribute->getFrontend()->getValue($product);   

            }
        
?>
1
  • You need to update your product query where you fetch all the products. Can you share the method you use to retrieve products on the category page?
    – Msquare
    Nov 20, 2023 at 8:26

2 Answers 2

3
+50

Yes, I noticed your comments. You are using a product query to retrieve products, so you can simply add your attribute filter to your existing query.

Product Query: https://developer.adobe.com/commerce/webapi/graphql/schema/products/queries/products/

{
  products(filter: { ready_to_ship: { eq : "1" }, sku: { in : ["24-MB01", "24-MB02"] } }) {
    items {
      name
      sku
      url_key
    }
  }
}

To add a filter to your existing product query, add or update your existing 'schema.graphqls' file within your custom module.

app/code/VendorName/ModuleName/etc/schema.graphqls

input ProductAttributeFilterInput {
  ready_to_ship: FilterEqualTypeInput @doc(description: "Product Data filter with Custom Attribute Value")
}

Before running the updated query above, please execute the Magento upgrade command.

enter image description here enter image description here

For More Click Here

3

when u are using PWA Magento 2.4.5 Venia Theme then you can't use classic Magento templates from modules.

Magento theme is based on PHP and HTML and is part of classic Magento approach. PWA Venia is separated product/technology which has another source. It is written in React.JS and it use only GraphQL API of Magento2.

So Venia PWA is headless. Magento gives API and Venia uses it.

The answer

You should limit your products in GraphQL Endpoint or you should filter result in React.JS on Venia source code :)

3
  • Previous developers already filtered products in layered navigation based on "ready_to_ship" attribute value, you can see that after click on "Filter" button in this link , do you think they are not using magento template code to filter products based on attribute value ? Nov 19, 2023 at 6:28
  • If u using Venia then for sure no :) Venia is not Magento itself. It is PWA. It is another technology Nov 20, 2023 at 7:45
  • Can you please guide me where i have to look at react side to filter the products based on attribute value ? if you tell me file name, it will be really helpfull. Nov 22, 2023 at 14:11

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.