-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[3.x]: Unable to programmatically create a custom field and assign to Product. #3246
Comments
Could you please let us if this persists in Commerce 4 please? We will then take a look. Thanks! |
Hey @lukeholder, I'm not going to be able to run this on 4.x for a while, but I've modified my test code to the following: function createFieldLayout(string $type, Field ...$fields): FieldLayout
{
Craft::$app->getFields()->saveLayout($layout = new FieldLayout([
'fields' => $fields,
'tabs' => [
[
'fields' => $fields,
'name' => $type,
],
],
'type' => $type,
]));
return $layout;
} function createProductType(string $handle, string $name = null): ProductType
{
CommercePlugin::getInstance()->getProductTypes()->saveProductType($productType = new ProductType([
'fieldLayoutId' => Craft::$app->getFields()->getLayoutByType(Product::class)->id,
'handle' => $handle,
'hasDimensions' => false,
'hasVariants' => true,
'name' => $name ?? $handle,
'siteSettings' => [
Craft::$app->getSites()->getPrimarySite()->id => new ProductTypeSite(),
],
'variantFieldLayoutId' => Craft::$app->getFields()->getLayoutByType(\craft\commerce\elements\Variant::class)->id,
]));
return $productType;
} and function createProduct(ProductType $type, string $slug, array $attributes = [], array $variantAttributes = []): Product
{
Craft::$app->getElements()->saveElement($product = new Product(array_merge([
'slug' => $slug,
'title' => $slug,
'typeId' => $type->id,
'variants' => [
array_merge([
'price' => 1,
'stock' => 1,
'title' => $slug,
], $variantAttributes),
],
], $attributes)));
return $product;
} Running something like: createFieldLayout(
\craft\commerce\elements\Product::class,
// some fields
);
createFieldLayout(
\craft\commerce\elements\Variant::class,
// some fields
); then: createProduct(
createProductType("YQu4TZ4M9n6JzqkAGJMLq"),
"8FX3-dEE2TwnZSfNLgPGd",
); gets me a properly set up Product with the appropriate fields. I'm guessing the pertinent change was providing the:
explicitly when creating the ProductType? |
If the issue persists, please create a new ticket with a failing example in Commerce 4 or 5 which are under active support. Thanks! |
What happened?
Description
I am unable to programmatically create a custom field, and subsequently assign it to a
Product
. It works forUser
, and forOrder
, but not forProduct
. I suspect it's related toProduct
delegating toProductType
for its field layout under certain circumstances, but I can't work it out. I'd be happy to be told how I'm holding this wrong 🤣Cross post
Steps to reproduce
From a fresh install of Craft + Commerce, under
php craft shell
,require('foo.php');
wherefoo.php
is:Expected behavior
All assertions to pass.
Actual behavior
Craft CMS version
3.8.16
Craft Commerce version
3.4.22.1
PHP version
7.4.33
Operating system and version
macOS 13.4.1
Database type and version
mysql 8.0.34
Image driver and version
No response
Installed plugins and versions
The text was updated successfully, but these errors were encountered: