0

I am inserting custom options programmatically for multiple products from some template where I add options and assign them to products But when I edit those options and assigned it to more products then it double added for previously assigned products. How can I check if that custom option is already assigned or exists for any product so it will only edit for that product.

I can't add whole code here and yes, I am referring below link but my options are dynamic. https://webkul.com/blog/create-custom-option-programmatically-in-magento2/

4 Answers 4

0

Based in the code of webkul.com you have these options:

Load Product

$product = $_objectManager->get('\Magento\Catalog\Model\Product')->load($id);

Custom Options

$customOptions = $_objectManager->get('Magento\Catalog\Model\Product\Option')->getProductOptionCollection($product);

Configurable Options

$productTypeInstance = $_objectManager->get('Magento\ConfigurableProduct\Model\Product\Type\Configurable');
         $productAttributeOptions = $productTypeInstance->getConfigurableAttributesAsArray($product);

The last step is check based in Custom or Configurable Options if your option is created in product before and keep it.

0

private $productRepository;

public function __construct(
   -------------
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
   -------------
) {
  $this->productRepository = $productRepository;
}

public function hasOptionsProduct()
{
  $sku = '24-WG080';
 
  $product = $this->productRepository->get($sku);
  $hasOptions = $product->hasOptions();
 
}
0

To check if custom options exist for a product in Magento 2, you can follow these steps:

  1. Inject the \Magento\Catalog\Model\Product\OptionFactory class into your class constructor. Open the class file where you want to perform the check (e.g., a block class or a custom module class).
use Magento\Catalog\Model\Product\OptionFactory;

class CustomClass
{
    protected $optionFactory;

    public function __construct(
        OptionFactory $optionFactory
    ) {
        $this->optionFactory = $optionFactory;
    }

    // ...
}
  1. In your class, create a public function to check if custom options exist for a product. Add the following code to your class:
public function hasCustomOptions($productId)
{
    $productOptions = $this->optionFactory->create()->getProductOptionCollection($productId);
    return $productOptions->getSize() > 0;
}
  1. Now, you can use the hasCustomOptions function to check if custom options exist for a specific product ID. For example, in a template file, you can call this function by passing the product ID and perform the necessary actions based on the result.
<?php if ($block->hasCustomOptions($productId)) : ?>
    <!-- Custom options exist for the product -->
    <p>Custom options are available.</p>
<?php else : ?>
    <!-- No custom options for the product -->
    <p>No custom options available.</p>
<?php endif; ?>

By following these steps, you can check if custom options exist for a product in Magento 2. The OptionFactory class provides methods to fetch and manipulate custom options associated with a product.

0

To check if custom options exist for a product in Magento 2, you can use the following code:

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product\OptionFactory;

class CustomClass
{
    protected $productRepository;
    protected $optionFactory;

    public function __construct(
        ProductRepositoryInterface $productRepository,
        OptionFactory $optionFactory
    ) {
        $this->productRepository = $productRepository;
        $this->optionFactory = $optionFactory;
    }

    public function hasCustomOptions($productId)
    {
        try {
            $product = $this->productRepository->getById($productId);
            $customOptions = $this->optionFactory->create()->getProductOptionCollection($product);
            return count($customOptions) > 0;
        } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
            // Handle exception if the product does not exist
            return false;
        }
    }
}

Here's how you can use the above code:

  1. Inject the ProductRepositoryInterface and OptionFactory classes into your class constructor.

  2. In the hasCustomOptions function, first, retrieve the product using the ProductRepositoryInterface by passing the product ID.

  3. Use the OptionFactory to create an instance of the product options.

  4. Get the custom options collection by calling the getProductOptionCollection method on the options instance and passing the product as an argument.

  5. Check if the count of custom options is greater than zero. If it is, it means custom options exist for the product.

  6. If an exception of type NoSuchEntityException is thrown, it means the product does not exist. Handle the exception appropriately and return false.

You can call the hasCustomOptions function and pass the product ID to check if custom options exist for a specific product:

$productId = 123; // Replace with your product ID
$customClass = new CustomClass($productRepository, $optionFactory);
if ($customClass->hasCustomOptions($productId)) {
    // Custom options exist for the product
    // Perform your actions here
} else {
    // No custom options for the product
}

Make sure to replace $productRepository and $optionFactory with the actual instances of the respective classes in your code.

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.