Skip to content

Instantly share code, notes, and snippets.

@seanr
Created December 16, 2020 20:23
Show Gist options
  • Save seanr/eb038934c3c1b8a133e38103f31d9723 to your computer and use it in GitHub Desktop.
Save seanr/eb038934c3c1b8a133e38103f31d9723 to your computer and use it in GitHub Desktop.
Attribute Generator
<?php
use Drush\Drush;
use Drupal\commerce_product\Entity\ProductAttributeValue;
use Symfony\Component\Yaml\Yaml;
$yml = file_get_contents('/app/config/templates/commerce-attributes/attributes.yml');
$attributes = Yaml::parse($yml);
// For each attribute
foreach ($attributes as $attribute) {
$machineName = strtolower($attribute['machine_name']);
$machineName = preg_replace('/[^a-z0-9_]+/', '_', $machineName);
$machineName = preg_replace('/_+/', '_', $machineName);
$data = ProductAttributeValue::create([
'attribute' => $machineName,
'name' => 'test',
]);
$data->save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment