Création du module CadeauBienvenue

This commit is contained in:
2021-01-26 21:15:26 +01:00
parent 776679f179
commit 1a4eb8f45e
26 changed files with 319 additions and 16 deletions

View File

@@ -0,0 +1,55 @@
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace CadeauBienvenue;
use Thelia\Log\Tlog;
use Thelia\Module\BaseModule;
use Propel\Runtime\Connection\ConnectionInterface;
use Propel\Runtime\Exception\PropelException;
use Thelia\Model\Message;
use Thelia\Model\MessageQuery;
class CadeauBienvenue extends BaseModule
{
/** @var string */
const DOMAIN_NAME = 'cadeaubienvenue';
/** @var string */
const VARIABLE_MONTANT_CODE_PROMO = 'cadeau_bienvenue.montant_coupon';
/** @var string */
const MESSAGE_MAIL_CLIENT = 'code_promo_nouveau_client';
public function postActivation(ConnectionInterface $con = null)
{
if (null === MessageQuery::create()->findOneByName(self::MESSAGE_MAIL_CLIENT)) {
$message = new Message();
try {
$message
->setName(self::MESSAGE_MAIL_CLIENT)
->setHtmlTemplateFileName('code-promo-bienvenue.html')
->setTextTemplateFileName('code-promo-bienvenue.txt')
->setHtmlLayoutFileName('')
->setTextLayoutFileName('')
->setSecured(0)
->setLocale('fr_FR')
->setTitle('Mail envoyé à tout nouveau client, avec un code promo')
->setSubject('Bienvenue chez AuxBieauxLegumes.fr - Code promo')
->save();
} catch (PropelException $e) {
Tlog::getInstance()->error($e->getMessage());
}
}
}
}

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>
<config xmlns="http://thelia.net/schema/dic/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://thelia.net/schema/dic/config http://thelia.net/schema/dic/config/thelia-1.0.xsd">
<services>
<service id="customer.creation.event" class="CadeauBienvenue\EventListeners\NewCustomerListener" scope="request">
<argument type="service" id="request"/>
<argument type="service" id="mailer"/>
<argument type="service" id="event_dispatcher"/>
<tag name="kernel.event_subscriber"/>
</service>
</services>
</config>

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="http://thelia.net/schema/dic/module"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://thelia.net/schema/dic/module http://thelia.net/schema/dic/module/module-2_2.xsd">
<fullnamespace>CadeauBienvenue\CadeauBienvenue</fullnamespace>
<descriptive locale="en_US">
<title>Generate a personal coupon for every new customer</title>
</descriptive>
<descriptive locale="fr_FR">
<title>Généré un code promotion personnel pour chaque nouveau client</title>
</descriptive>
<languages>
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.0</version>
<authors>
<author>
<name>Laurent LE CORRE</name>
<email>laurent@thecoredev.fr</email>
</author>
</authors>
<type>classic</type>
<thelia>2.4.3</thelia>
<stability>other</stability>
<mandatory>0</mandatory>
<hidden>0</hidden>
</module>

View File

@@ -0,0 +1,93 @@
<?php
namespace CadeauBienvenue\EventListeners;
use CadeauBienvenue\CadeauBienvenue;
use Propel\Runtime\ActiveQuery\Criteria;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Thelia\Core\Event\Coupon\CouponCreateOrUpdateEvent;
use Thelia\Core\Event\Customer\CustomerCreateOrUpdateEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Mailer\MailerFactory;
use Thelia\Model\ConfigQuery;
class NewCustomerListener implements EventSubscriberInterface
{
protected $request;
/** @var MailerFactory */
protected $mailer;
/** @var EventDispatcherInterface */
protected $dispatcher;
public function __construct(Request $request, MailerFactory $mailer, EventDispatcherInterface $dispatcher)
{
$this->request = $request;
$this->mailer = $mailer;
$this->dispatcher = $dispatcher;
}
public static function getSubscribedEvents()
{
return array(
TheliaEvents::CUSTOMER_CREATEACCOUNT => array(
'createAccount', 128
),
);
}
/**
* @param CustomerCreateOrUpdateEvent $event
* @param $eventName
* @param EventDispatcherInterface $dispatcher
*/
public function createAccount(CustomerCreateOrUpdateEvent $event, $eventName, EventDispatcherInterface $dispatcher)
{
$customer = $event->getCustomer();
$couponCode = substr(str_shuffle(md5(time())), 0, 12);
$discountAmount = ConfigQuery::create()->filterByName(CadeauBienvenue::VARIABLE_MONTANT_CODE_PROMO)->findOne()->getValue();
$couponServiceId = 'thelia.coupon.type.remove_x_amount';
$effects = [ 'amount' => $discountAmount];
$dateExpiration = (new \DateTime())->add(new \DateInterval('P1Y')); // Expiration dans 1 an
$couponEvent = new CouponCreateOrUpdateEvent(
$couponCode, // Code
$couponServiceId, // $serviceId
'Offre de bienvenue', // $title
$effects, // $effects
'', // $shortDescription
sprintf('Offre de bienvenue pour %s %s',$customer->getFirstname(), $customer->getLastname()), // $description
true, // $isEnabled
$dateExpiration, // $expirationDate
false, // $isAvailableOnSpecialOffers
false, // $isCumulative
false, // $isRemovingPostage,
1, // $maxUsage,
$customer->getLocale(), // $locale,
[], // $freeShippingForCountries,
[], // $freeShippingForMethods,
1 // $perCustomerUsageCount,
);
$this->dispatcher->dispatch(TheliaEvents::COUPON_CREATE, $couponEvent);
// Envoyer le mail au client
$this->mailer->sendEmailToCustomer(
CadeauBienvenue::MESSAGE_MAIL_CLIENT,
$customer,
[
'customer_id' => $customer->getId(),
'montant' => $discountAmount,
'date_validite' => $dateExpiration,
'code_promo' => $couponCode
]
);
}
}

View File

@@ -0,0 +1,4 @@
<?php
return array(
// 'an english string' => 'The displayed english string',
);

View File

@@ -0,0 +1,4 @@
<?php
return array(
// 'an english string' => 'La traduction française de la chaine',
);

View File

@@ -0,0 +1,55 @@
# Cadeau Bienvenue
Add a short description here. You can also add a screenshot if needed.
## Installation
### Manually
* Copy the module into ```<thelia_root>/local/modules/``` directory and be sure that the name of the module is CadeauBienvenue.
* Activate it in your thelia administration panel
### Composer
Add it in your main thelia composer.json file
```
composer require your-vendor/cadeau-bienvenue-module:~1.0
```
## Usage
Explain here how to use your module, how to configure it, etc.
## Hook
If your module use one or more hook, fill this part. Explain which hooks are used.
## Loop
If your module declare one or more loop, describe them here like this :
[loop name]
### Input arguments
|Argument |Description |
|--- |--- |
|**arg1** | describe arg1 with an exemple. |
|**arg2** | describe arg2 with an exemple. |
### Output arguments
|Variable |Description |
|--- |--- |
|$VAR1 | describe $VAR1 variable |
|$VAR2 | describe $VAR2 variable |
### Exemple
Add a complete exemple of your loop
## Other ?
If you have other think to put, feel free to complete your readme as you want.

View File

@@ -0,0 +1,12 @@
{
"name": "your-vendor/cadeau-bienvenue-module",
"description": "CadeauBienvenue module for Thelia",
"license": "LGPL-3.0-or-later",
"type": "thelia-module",
"require": {
"thelia/installer": "~1.1"
},
"extra": {
"installer-name": "CadeauBienvenue"
}
}

View File

@@ -0,0 +1,23 @@
{extends file="email-layout.tpl"}
{* Do not provide a "Open in browser" link *}
{block name="browser"}{/block}
{* No pre-header *}
{block name="pre-header"}{/block}
{* Subject *}
{block name="email-subject"}Bienvenue chez AuxBieauxLegumes.fr - Code promo{/block}
{* Title *}
{block name="email-title"}Bienvenue chez AuxBieauxLegumes.fr - Code promo{/block}
{* Content *}
{block name="email-content"}
{loop type="customer" name="client" current="false" id=$customer_id}<p>Bonjour {$FIRSTNAME} {$LASTNAME},</p>{/loop}
<p>Pour fêter votre arrivée sur notre site {$nom_site}, nous vous offrons un bon de réduction d'une valeur de {$montant}€, utilisable dès votre prochain achat !
Ce bon est valable jusqu'au {format_date date={$date_validite} output="date"}.</p>
<p>Rendez-vous très bientôt sur <a href="{url path='/'}">{url path="/"}</a></p>
<p>Très cordialement,</p>
<p>{config key="store_name"}.</p>
{/block}

View File

@@ -0,0 +1,13 @@
{loop type="customer" name="client" current="false" id=$customer_id}Bonjour {$FIRSTNAME} {$LASTNAME},{/loop}
Pour fêter votre arrivée sur notre site {$nom_site}, nous vous offrons un bon de réduction d'une valeur de {$montant}€, utilisable dès votre prochain achat !
Votre avantage parrainage est une remise de {$label_promo} sur votre prochaine commande.
Profitez-en vite en indiquant lors de votre prochaine commande le code promotion {$code_promo}.
Ce bon est valable jusqu'au {format_date date=$date_validite format="d/m/Y"}.
Rendez-vous très bientôt sur {url path="/"}.
Très cordialement,
{config key="store_name"}.

View File

@@ -1,4 +1,4 @@
{default_translation_domain domain='email.default'} {default_translation_domain domain='email.custom'}
{intl l="Hello,"} {intl l="Hello,"}
{intl l="Your account at %store_name has been changed by one of our managers." store_name={config key="store_name"}}. {intl l="Your account at %store_name has been changed by one of our managers." store_name={config key="store_name"}}.

View File

@@ -1,4 +1,4 @@
{default_translation_domain domain='email.default'} {default_translation_domain domain='email.custom'}
{intl l="Hello,"} {intl l="Hello,"}
{intl l="An account at %store_name has been created by one of our managers." store_name={config key="store_name"}}. {intl l="An account at %store_name has been created by one of our managers." store_name={config key="store_name"}}.

View File

@@ -1,4 +1,4 @@
{default_translation_domain domain='email.default'} {default_translation_domain domain='email.custom'}
{intl l="Hello,"} {intl l="Hello,"}
{intl l="You have requested a new password for your administrator account at %store_name" store_name={config key="store_name"}}. {intl l="You have requested a new password for your administrator account at %store_name" store_name={config key="store_name"}}.

View File

@@ -1,4 +1,4 @@
{default_translation_domain domain='email.default'} {default_translation_domain domain='email.custom'}
{loop type="customer" name="confirmation" current=false id=$customer_id} {loop type="customer" name="confirmation" current=false id=$customer_id}
{intl l="Welcome to %store," store={config key="store_name"}} {intl l="Welcome to %store," store={config key="store_name"}}
<br> <br>

View File

@@ -20,7 +20,7 @@ DO NOT DELETE THIS FILE, some plugins may use it.
*} *}
{* Set the default translation domain, that will be used by {intl} when the 'd' parameter is not set *} {* Set the default translation domain, that will be used by {intl} when the 'd' parameter is not set *}
{default_translation_domain domain='email.default'} {default_translation_domain domain='email.custom'}
{default_locale locale={$locale}} {default_locale locale={$locale}}
{block name='message-body'}{$message_body nofilter}{/block} {block name='message-body'}{$message_body nofilter}{/block}

View File

@@ -20,7 +20,7 @@ DO NOT DELETE THIS FILE, some plugins may use it.
*} *}
{* Set the default translation domain, that will be used by {intl} when the 'd' parameter is not set *} {* Set the default translation domain, that will be used by {intl} when the 'd' parameter is not set *}
{default_translation_domain domain='email.default'} {default_translation_domain domain='email.custom'}
{default_locale locale={$locale}} {default_locale locale={$locale}}
{block name='message-body'}{$message_body nofilter}{/block} {block name='message-body'}{$message_body nofilter}{/block}

View File

@@ -1,4 +1,4 @@
{default_translation_domain domain='email.default'} {default_translation_domain domain='email.custom'}
{default_locale locale={$locale}} {default_locale locale={$locale}}
{declare_assets directory='assets'} {declare_assets directory='assets'}
{assign var="url_site" value="{config key="url_site"}"} {assign var="url_site" value="{config key="url_site"}"}

View File

@@ -1,4 +1,4 @@
{default_translation_domain domain='email.default'} {default_translation_domain domain='email.custom'}
{if $firstname || $lastname} {if $firstname || $lastname}
{intl l="Dear %firstname %lastname," firstname=$firstname lastname=$lastname} {intl l="Dear %firstname %lastname," firstname=$firstname lastname=$lastname}
{else} {else}

View File

@@ -1,4 +1,4 @@
{default_translation_domain domain='email.default'} {default_translation_domain domain='email.custom'}
{loop name="order.invoice" type="order" id=$order_id customer="*"} {loop name="order.invoice" type="order" id=$order_id customer="*"}
{intl l="Hello,"} {intl l="Hello,"}

View File

@@ -1,4 +1,4 @@
{default_translation_domain domain='email.default'} {default_translation_domain domain='email.custom'}
{loop name="order.invoice" type="order" id=$order_id customer="*"} {loop name="order.invoice" type="order" id=$order_id customer="*"}
{intl l="Hello"} {intl l="Hello"}

View File

@@ -1,4 +1,4 @@
{default_translation_domain domain='email.default'} {default_translation_domain domain='email.custom'}
{intl l="Hello,"} {intl l="Hello,"}
{intl l="You have requested a new password for your account at %store_name" store_name={config key="store_name"}}. {intl l="You have requested a new password for your account at %store_name" store_name={config key="store_name"}}.

View File

@@ -1,5 +1,5 @@
{* Set the default translation domain, that will be used by {intl} when the 'd' parameter is not set *} {* Set the default translation domain, that will be used by {intl} when the 'd' parameter is not set *}
{default_translation_domain domain='fo.default'} {default_translation_domain domain='fo.custom'}
{form name="thelia.order.delivery"} {form name="thelia.order.delivery"}

View File

@@ -1,7 +1,7 @@
{* This page should not replace the current previous URL *} {* This page should not replace the current previous URL *}
{set_previous_url ignore_current="1"} {set_previous_url ignore_current="1"}
{default_translation_domain domain='fo.default'} {default_translation_domain domain='fo.custom'}
{loop type="product" name="add_product_to_cart" id={product attr="id"}} {loop type="product" name="add_product_to_cart" id={product attr="id"}}
<div class="clearfix"> <div class="clearfix">
<table> <table>

View File

@@ -20,7 +20,7 @@ GNU General Public License : http://www.gnu.org/licenses/
{* Declare assets directory, relative to template base directory *} {* Declare assets directory, relative to template base directory *}
{declare_assets directory='assets/dist'} {declare_assets directory='assets/dist'}
{* Set the default translation domain, that will be used by {intl} when the 'd' parameter is not set *} {* Set the default translation domain, that will be used by {intl} when the 'd' parameter is not set *}
{default_translation_domain domain='fo.default'} {default_translation_domain domain='fo.custom'}
{* -- Define some stuff for Smarty ------------------------------------------ *} {* -- Define some stuff for Smarty ------------------------------------------ *}
{config_load file='variables.conf'} {config_load file='variables.conf'}

View File

@@ -10,7 +10,7 @@
/*************************************************************************************} /*************************************************************************************}
{* Set the default translation domain, that will be used by {intl} when the 'd' parameter is not set *} {* Set the default translation domain, that will be used by {intl} when the 'd' parameter is not set *}
{default_translation_domain domain='pdf.default'} {default_translation_domain domain='pdf.custom'}
{literal} {literal}
<style> <style>
h1, h2, h3, h4 { h1, h2, h3, h4 {

View File

@@ -10,7 +10,7 @@
/*************************************************************************************} /*************************************************************************************}
{* Set the default translation domain, that will be used by {intl} when the 'd' parameter is not set *} {* Set the default translation domain, that will be used by {intl} when the 'd' parameter is not set *}
{default_translation_domain domain='pdf.default'} {default_translation_domain domain='pdf.custom'}
{literal} {literal}
<style> <style>
h1, h2, h3, h4 { h1, h2, h3, h4 {