Skip to content

Instantly share code, notes, and snippets.

@QuingKhaos
Created November 12, 2014 20:03
Show Gist options
  • Save QuingKhaos/37a610a8bc603d617eab to your computer and use it in GitHub Desktop.
Save QuingKhaos/37a610a8bc603d617eab to your computer and use it in GitHub Desktop.
<?php
namespace AppBundle\Form\Type;
use Sylius\Bundle\CartBundle\Form\Type\CartItemType as BaseCartItemType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
/**
* We extend the item form type a bit, to add a variant select field
* when we're adding product to cart, but not when we edit quantity in cart.
* We'll use simple option for that, passing the product instance required by
* variant choice type.
*/
class CartItemType extends BaseCartItemType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
if (isset($options['product']) && $options['product']->hasVariants()) {
$builder->add('variant', 'app_product_variant_match', array(
'variable' => $options['product']
));
}
}
/**
* We need to override this method to allow setting 'product'
* option, by default it will be null so we don't get the variant choice
* when creating full cart form.
*
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
parent::setDefaultOptions($resolver);
$resolver
->setOptional(array(
'product'
))
->setAllowedTypes(array(
'product' => array('Sylius\Component\Core\Model\ProductInterface')
))
;
}
}
# app/config/config.yml
sylius_cart:
classes:
item:
form: AppBundle\Form\Type\CartItemType
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="form.type.app_product_variant_match" class="AppBundle\Form\Type\VariantMatchType">
<argument type="string">product</argument>
<tag name="form.type" alias="app_product_variant_match"/>
</service>
</services>
</container>
<?php
namespace AppBundle\Form\Type;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Sluggable\Util\Urlizer;
use Sylius\Bundle\VariationBundle\Form\DataTransformer\VariantToCombinationTransformer;
use Sylius\Bundle\VariationBundle\Form\Type\VariantMatchType as BaseVariantMatchType;
use Sylius\Component\Core\Model\ProductVariant;
use Sylius\Component\Product\Model\Option;
use Symfony\Component\Form\FormBuilderInterface;
/**
* Variant match form type.
*/
class VariantMatchType extends BaseVariantMatchType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
/** @var Option $option */
foreach ($options['variable']->getOptions() as $i => $option) {
$oldValues = $option->getValues();
$option->setValues(new ArrayCollection());
/** @var ProductVariant $variant */
foreach ($options['variable']->getVariants() as $variant) {
foreach ($oldValues as $value) {
if($variant->hasOption($value)) {
$option->addValue($value);
}
}
}
$builder->add(
Urlizer::urlize($option->getName()),
sprintf('sylius_%s_option_value_choice', $this->variableName),
array(
'label' => $option->getPresentation(),
'option' => $option,
'property_path' => '[' . $i . ']'
)
);
}
$builder->addModelTransformer(new VariantToCombinationTransformer($options['variable']));
}
/**
* {@inheritdoc}
*/
public function getName()
{
return sprintf('app_%s_variant_match', $this->variableName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment