Skip to content

Instantly share code, notes, and snippets.

@bummzack
Created February 18, 2017 21:14
Show Gist options
  • Save bummzack/a9bb2dd8949a38cf0299bbbb7b7ad666 to your computer and use it in GitHub Desktop.
Save bummzack/a9bb2dd8949a38cf0299bbbb7b7ad666 to your computer and use it in GitHub Desktop.
silvershop – User customisable OrderItem field
<?php
/**
* Extension to add custom field to AddProductForm and VariationForm
*/
class AddProductFormExtension extends Extension
{
public function updateAddProductForm()
{
// Add a new Textfield to the form-fields
$this->owner->Fields()->add(TextField::create('Inscription'));
// tell the form that there's extra savable data that should be put into the OrderItem
$this->owner->setSaveableFields([
'Inscription'
]);
}
}
Product_OrderItem:
required_fields:
- Inscription # This tells the cart, that this field is also needed to determine uniqueness of an OrderItem
extensions:
- InscriptionExtension # Add the extension
AddProductForm:
extensions:
- AddProductFormExtension
<?php
/**
* Extension meant to be applied to Product_OrderItem
*/
class InscriptionExtension extends DataExtension
{
// Add a custom field
private static $db = [
'Inscription' => 'Varchar(255)'
];
// Optional update title hook that will just add the Inscription text to the title.
public function updateTableTitle(&$title) {
if ($this->owner->Inscription) {
$title = "$title, with Inscription: " . $this->owner->Inscription;
}
}
}
@nimeso
Copy link

nimeso commented Dec 8, 2018

Life saver :) cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment