0

With this kind of setup:

function random()
{
    return array_reduce(range(0, 9), fn ($x) => $x . range('a', 'z')[rand(0, 25)], '');
}

$salt = random();

$result = Craft::$app->getFields()->saveField($field = new craft\fields\Number([
    'handle' => $salt,
    'name' => $salt,
]));
assert($result === true);

I can add this field to a User:

$result = Craft::$app->getFields()->saveLayout(new craft\models\FieldLayout([
    'fields' => [$field],
    'type' => craft\elements\User::class,
]));
assert($result === true);

$result = Craft::$app->getElements()->saveElement($user = new craft\elements\User([
    'email' => "[email protected]",
    'username' => $salt,
    $salt => 42,
]));
assert($result === true);
assert($user->{$salt} === 42);
assert(Craft::$app->getElements()->getElementById($user->id, craft\elements\User::class)->{$salt} === 42);

and to an Order:

$result = Craft::$app->getFields()->saveLayout(new craft\models\FieldLayout([
    'fields' => [$field],
    'type' => craft\commerce\elements\Order::class,
]));
assert($result === true);

$result = Craft::$app->getElements()->saveElement($order = new craft\commerce\elements\Order([
    $salt => 42,
]));
assert($result);
assert($order->{$salt} === 42);
assert(Craft::$app->getElements()->getElementById($order->id, craft\commerce\elements\Order::class)->{$salt} === 42);

but I cannot do the same with a Product:

$result = Craft::$app->getFields()->saveLayout(new craft\models\FieldLayout([
    'fields' => [$field],
    'type' => craft\commerce\elements\Product::class,
]));
assert($result === true);

$result = craft\commerce\Plugin::getInstance()->getProductTypes()->saveProductType($productType = new craft\commerce\models\ProductType([
    'handle' => $salt,
    'hasDimensions' => false,
    'hasVariants' => false,
    'name' => $salt,
    'siteSettings' => [
        Craft::$app->getSites()->getCurrentSite()->id => new craft\commerce\models\ProductTypeSite(),
    ],
]));
assert($result === true);

$result = Craft::$app->getElements()->saveElement($product = new craft\commerce\elements\Product([
    'slug' => $salt,
    'title' => $salt,
    'typeId' => $productType->id,
    'variants' => [
        [
            'hasUnlimitedStock' => false,
            'price' => 0,
            'maxQty' => 0,
            'minQty' => 0,
        ],
    ],
    $salt => 42,
]));
assert($result === true);
assert($product->{$salt} === 42);
assert(Craft::$app->getElements()->getElementById($product->id, craft\commerce\elements\Product::class)->{$salt} === 42);

Both assertions fail (though the Product is saved without errors), so I'm assuming I am missing some additional piece of ceremony in the creation. What could that be?

2
  • Whats the result of Plugin::getInstance()->getProductTypes()->saveProductType? Commented Aug 7, 2023 at 12:52
  • @LukeHolder a fair question. I've added assertions on the results of each call; they all return true. In preparing the test case, I also checked the errors (assert(!$x->hasErrors());) for each created item, they also passed.
    – msbit
    Commented Aug 7, 2023 at 14:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.