PHP code example of setono / sylius-variant-link-plugin
1. Go to this page and download the library: Download setono/sylius-variant-link-plugin library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
setono / sylius-variant-link-plugin example snippets
# config/bundles.php
return [
// ...
Setono\SyliusVariantLinkPlugin\SetonoSyliusVariantLinkPlugin::class => ['all' => true],
// ...
];
namespace App\Resolver;
use Setono\SyliusVariantLinkPlugin\Resolver\ProductVariantFromIdentifierResolverInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
final class ProductVariantFromSizeResolver implements ProductVariantFromIdentifierResolverInterface
{
public function resolve(ProductInterface $product, string $identifier) : ?ProductVariantInterface
{
foreach ($product->getVariants() as $variant) {
foreach ($variant->getOptionValues() as $optionValue) {
if(strtolower($optionValue->getValue()) === $identifier) {
return $variant;
}
}
}
return null;
}
}
namespace App\UrlGenerator;
use Setono\SyliusVariantLinkPlugin\UrlGenerator\ProductVariantUrlGeneratorInterface;
use Sylius\Component\Product\Model\ProductVariantInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
final class ProductVariantFromSizeUrlGenerator implements ProductVariantUrlGeneratorInterface
{
/** @var UrlGeneratorInterface */
private $urlGenerator;
public function __construct(UrlGeneratorInterface $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}
public function generate(
ProductVariantInterface $productVariant,
array $parameters = [],
int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH
) : string{
$parameters = array_merge($parameters, [
'slug' => $productVariant->getProduct()->getSlug(),
'variant_identifier' => $productVariant->getOptionValues()->first()->getCode(),
]);
return $this->urlGenerator->generate(
'setono_sylius_variant_link_shop_product_variant_show', $parameters, $referenceType
);
}
}
twig
{% set variant = product|sylius_resolve_variant %}
{{ variant.code }}