0

So I have a bunch of products with custom options and I would like to stop Magento from appending those other SKUS for these orders.

This is how it shows: SKU1-CUSTOMOPTION1SKU-CUSTOMOPTION2SKU

How can I get it to only show the main products SKU? SKU1

  public function getSku($product)
    {
        $sku = $product->getData('sku');
        if ($product->getCustomOption('option_ids')) {
            $sku = $this->getOptionSku($product, $sku);
        }
        return $sku;
    }

    /**
     * Default action to get sku of product with option
     *
     * @param \Magento\Catalog\Model\Product $product Product with Custom Options
     * @param string $sku Product SKU without option
     * @return string
     */
    public function getOptionSku($product, $sku = '')
    {
        $skuDelimiter = '-';
        if (empty($sku)) {
            $sku = $product->getData('sku');
        }
        $optionIds = $product->getCustomOption('option_ids');
        if ($optionIds) {
            foreach (explode(',', $optionIds->getValue()) as $optionId) {
                $option = $product->getOptionById($optionId);
                if ($option) {
                    $confItemOption = $product->getCustomOption(self::OPTION_PREFIX . $optionId);

                    $group = $option->groupFactory($option->getType())
                        ->setOption($option)
                        ->setListener(new \Magento\Framework\DataObject());

                    $optionSku = $group->getOptionSku($confItemOption->getValue(), $skuDelimiter);
                    if ($optionSku) {
                        $sku .= $skuDelimiter . $optionSku;
                    }

                    if ($group->getListener()->getHasError()) {
                        $product->setHasError(true)->setMessage($group->getListener()->getMessage());
                    }
                }
            }
        }
        return $sku;
    }
1
  • Hello @DevAnd, below solution worked for you ? Or do you need further help ?
    – Pawan
    Commented Sep 19, 2018 at 17:14

2 Answers 2

0

If a product having custom option, its SKU is created on the fly. If you look at file.

MagentoRoot\vendor\magento\module-catalog\Model\Product\Type\AbstractType.php

You will find a function getOptionSku, which is responsible for creating SKU.

If you want to remove custom option SKU to main product's SKU, You can change to

$sku

From

$sku .= $skuDelimiter . $optionSku;

Where

$sku = SKU of product
$skuDelimiter = -
$optionSku = SKU of custom option of product

You can change as per your requirement.

Note: Never edit core files, you need a proper Model overriding. For test purpose you can check by putting die below this sku generation code and add a product from front.

3
  • I uploaded the code you mentioned can you see what I need to change? A little confused. Also how can I do this without changing magento core files?
    – DevAnd
    Commented Jun 18, 2018 at 14:51
  • search $sku .= $skuDelimiter . $optionSku; and replace it with $sku.
    – Pawan
    Commented Jun 18, 2018 at 16:21
  • Hello @DevAnd, Did my solution worked for you ?
    – Pawan
    Commented Jun 23, 2018 at 9:29
0

We've found only simple products append the optional accessory SKU. Have you found a way to have a configurable product append the optional accessory SKU?

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.