class ProductEventSubscriber in Mailchimp E-Commerce 8
Event Subscriber for Commerce Products.
Hierarchy
- class \Drupal\mailchimp_ecommerce_commerce\EventSubscriber\ProductEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of ProductEventSubscriber
1 string reference to 'ProductEventSubscriber'
- mailchimp_ecommerce_commerce.services.yml in modules/
mailchimp_ecommerce_commerce/ mailchimp_ecommerce_commerce.services.yml - modules/mailchimp_ecommerce_commerce/mailchimp_ecommerce_commerce.services.yml
1 service uses ProductEventSubscriber
File
- modules/
mailchimp_ecommerce_commerce/ src/ EventSubscriber/ ProductEventSubscriber.php, line 15
Namespace
Drupal\mailchimp_ecommerce_commerce\EventSubscriberView source
class ProductEventSubscriber implements EventSubscriberInterface {
/**
* The Product Handler.
*
* @var \Drupal\mailchimp_ecommerce\ProductHandler
*/
private $product_handler;
/**
* ProductEventSubscriber constructor.
*
* @param \Drupal\mailchimp_ecommerce\ProductHandler $product_handler
* The Product Handler.
*/
public function __construct(ProductHandler $product_handler) {
$this->product_handler = $product_handler;
}
/**
* Respond to event fired after saving a new product.
*/
public function productInsert(ProductEvent $event) {
/** @var Product $product */
$product = $event
->getProduct();
$product_id = $product
->get('product_id')->value;
$title = !empty($product
->get('title')->value) ? $product
->get('title')->value : '';
$description = !empty($product
->get('body')->value) ? $product
->get('body')->value : '';
// TODO Fix Type
$type = !empty($product
->get('type')->value) ? $product
->get('type')->value : '';
$variants = $this->product_handler
->buildProductVariants($product);
$url = $this->product_handler
->buildProductUrl($product);
$image_url = $this->product_handler
->getProductImageUrl($product);
$this->product_handler
->addProduct($product_id, $title, $url, $image_url, $description, $type, $variants);
}
/**
* Respond to event fired after updating an existing product.
*/
public function productUpdate(ProductEvent $event) {
$product = $event
->getProduct();
$title = !empty($product
->get('title')->value) ? $product
->get('title')->value : '';
$description = !empty($product
->get('body')->value) ? $product
->get('body')->value : '';
// TODO Fix Type
$type = !empty($product
->get('type')->value) ? $product
->get('type')->value : '';
$variants = $this->product_handler
->buildProductVariants($product);
$url = $this->product_handler
->buildProductUrl($product);
$image_url = $this->product_handler
->getProductImageUrl($product);
// Update the existing product and variant.
$this->product_handler
->updateProduct($product, $title, $url, $image_url, $description, $type, $variants);
}
/**
* Respond to event fired after deleting a product.
*/
public function productDelete(ProductEvent $event) {
// TODO: Process deleted product.
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[ProductEvents::PRODUCT_INSERT][] = [
'productInsert',
];
$events[ProductEvents::PRODUCT_UPDATE][] = [
'productUpdate',
];
$events[ProductEvents::PRODUCT_DELETE][] = [
'productDelete',
];
return $events;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ProductEventSubscriber:: |
private | property | The Product Handler. | |
ProductEventSubscriber:: |
public static | function | ||
ProductEventSubscriber:: |
public | function | Respond to event fired after deleting a product. | |
ProductEventSubscriber:: |
public | function | Respond to event fired after saving a new product. | |
ProductEventSubscriber:: |
public | function | Respond to event fired after updating an existing product. | |
ProductEventSubscriber:: |
public | function | ProductEventSubscriber constructor. |