Skip to content

Instantly share code, notes, and snippets.

@sjelfull
Created May 22, 2017 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjelfull/ab4ee2c059cf5f75016e25a59feedb55 to your computer and use it in GitHub Desktop.
Save sjelfull/ab4ee2c059cf5f75016e25a59feedb55 to your computer and use it in GitHub Desktop.
<?php
public function createProduct ($locale = null)
{
if ( !$this->productTypes ) {
$this->productTypes = craft()->commerce_productTypes->getAllProductTypes();
}
if ( !$locale ) {
$locale = craft()->i18n->getPrimarySiteLocaleId();
}
if ( $this->productTypes ) {
$productType = $this->faker->randomElement($this->productTypes);
$randomTitle = $this->faker->sentence();
$product = new Commerce_ProductModel();
$product->promotable = true;
$product->freeShipping = $this->faker->boolean($chanceOfGettingTrue = 30);
$product->typeId = $productType->id;
$product->locale = $locale;
$product->localeEnabled = true;
$product->getContent()->title = $randomTitle;
// Variants
// TODO: Add random amount of variants
$variant = new Commerce_VariantModel();
$variant->height = $this->faker->numberBetween(100, 3000);
$variant->width = $this->faker->numberBetween(100, 3000);
$variant->weight = $this->faker->numberBetween(100, 3000);
$variant->length = $this->faker->numberBetween(100, 3000);
$variant->sku = StringHelper::toKebabCase($randomTitle);
$variant->price = $this->faker->numberBetween(50, 99999);
$variant->sortOrder = 0;
$variant->unlimitedStock = $this->faker->boolean($chanceOfGettingTrue = 70);
//$variant->productId = $product->id;
$variant->isDefault = true;
$variant->stock = $this->faker->numberBetween(100, 30000);
$variant->getContent()->title = $randomTitle;
$product->variants = [ $variant ];
if ( craft()->commerce_products->saveProduct($product) ) {
echo "Saved product?";
}
else {
echo "Not saved product?";
}
//craft()->commerce_variants->saveVariant($variant);
var_dump($product->getErrors());
var_dump($product->getAttributes());
var_dump($product->getContent()->getAttributes());
//$variant->setProduct($product);
// TODO: Fill custom fields
/*
'typeId'
'taxCategoryId'
'shippingCategoryId'
'promotable'
'freeShipping'
'postDate'
'expiryDate'
'defaultVariantId'
'defaultSku'
'defaultPrice'
'defaultHeight'
'defaultLength'
'defaultWidth'
'defaultWeight'
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment