_orderSummary = $orderSummary;
}
/**
* @return array
*/
public function getOrderSummary()
{
return $this->_orderSummary;
}
/**
* Return checkout config
*
* @return Zend_Config
*/
protected function _getCheckoutConfig()
{
if (null === $this->_checkoutConfig) {
$this->_checkoutConfig = Qs_Application::getConfig('ECommerce_Checkout');
}
return $this->_checkoutConfig;
}
protected function _initElements()
{
$this->_initSummaryInfo();
$this->_initBaseFields();
$this->_initShippingFields();
$this->_initPaymentFields();
if (!$this->_getCart()->hasShippableItems()) {
$this->addAttribs(['class' => $this->getAttrib('class') . ' no-shipping']);
}
return $this;
}
/**
* "Address" fields for "simple" mode
* @param string $type
* @param bool $isRequired
* @return $this
*/
protected function _initAddressFields($type, $isRequired = true)
{
$form = new Qs_Form_SubForm(array('legend' => ucfirst($type) . ' Address'));
if (Entity::ADDRESS_SHIPPING == $type && !$this->_getCart()->hasShippableItems()) {
$form->addElement(
'static',
'noShippingAddress',
['value' => $this->getDoc()->getSmarty()->fetch('ECommerce/Checkout/noShippingMessage.tpl')]
);
$this->addSubForm($form, $type);
return $this;
}
if ($type != Entity::ADDRESS_BILLING) {
$form->addElement(
'checkbox',
'asBilling',
array('label' => 'My shipping address is the same as my billing address', 'decoration' => 'simple')
);
}
$form->addElement('text', 'firstName', array('label' => 'First Name'));
$form->addElement('text', 'lastName', array('label' => 'Last Name'));
$form->addElement('text', 'address', array('label' => 'Address'));
$form->addElement('text', 'city', array('label' => 'City', 'class' => 'city'));
$form->addElement(
'select',
'state',
array(
'label' => 'State',
'class' => 'state',
'multiOptions' => array('' => 'Select One') + $this->_getStates4Select()
)
);
$form->addElement('zip', 'zip', array('label' => 'Zip Code', 'class' => 'zip'));
if ($isRequired) {
/** @var $element \Zend_Form_Element */
$element = null;
if ($type == Entity::ADDRESS_BILLING) {
foreach ($form as $element) {
$element->setRequired();
}
} else {
$asBilling = $this->_getData($type . '[asBilling]');
foreach ($form as $element) {
if ('asBilling' == $element->getName()) {
continue;
}
if ($asBilling && 'n' == $asBilling) {
$element->setRequired();
} else {
$element->getDecorator('Label')->setOption('class', 'required');
}
}
}
}
$this->addSubForm($form, $type);
return $this;
}
/**
* Init base checkout fields
*
* @return App_ECommerce_Checkout_Form_New
*/
protected function _initBaseFields()
{
$this->_initAddressFields(Entity::ADDRESS_BILLING);
$this->_initAddressFields(Entity::ADDRESS_SHIPPING);
// additional billing email field
$billingForm = $this->getSubForm('billing');
$billingForm->addElement(
'email',
'email',
array('label' => 'Email Address', 'required' => true, 'attribs' => array('autocomplete' => 'off'))
);
$billingForm->addElement(
'phone',
'phone',
array('label' => 'Phone Number', 'attribs' => array('autocomplete' => 'off'))
);
return $this;
}
/**
* Init fields for shipping info
*
* @return App_ECommerce_Checkout_Form_New
*/
protected function _initShippingFields()
{
if ($this->_getCheckoutConfig()->get('shipping')) {
$subForm = $this->_getCheckoutSubform('shipping');
if ($subForm) {
$this->addSubForm($subForm, App_ECommerce_Checkout_View::SHIPPING_SECTION_DATA);
}
}
return $this;
}
/**
* Init fields for payment info
*
* @return App_ECommerce_Checkout_Form_New
*/
protected function _initPaymentFields()
{
if ($this->_getCheckoutConfig()->get('payment')) {
$subForm = $this->_getCheckoutSubform('payment');
if ($subForm) {
$this->addSubForm($subForm, App_ECommerce_Checkout_View::PAYMENT_SECTION_DATA);
$this->_initDiscountFields();
}
}
return $this;
}
/**
* Init fields for discount
*
* @return App_ECommerce_Checkout_Form_New
*/
protected function _initDiscountFields()
{
$groupElements = array();
$cart = $this->_getCart();
$subForm = $this->getSubForm(App_ECommerce_Checkout_View::PAYMENT_SECTION_DATA);
if (Qs_SiteMap::getAliasByItem('ECommerce_Promo_Admin_')) {
$hasPromo = (bool) $cart->getData('promoCode');
$subForm->addElement(
'checkbox',
'usePromoCode',
array('label' => 'I want to use a Promo Code', 'decoration' => 'simple')
);
$groupElements[] = 'usePromoCode';
if ($hasPromo) {
/** @var Qs_Form_Decorator_DtDdWrapper $dtDdWrapper */
$dtDdWrapper = $subForm->getElement('usePromoCode')->getDecorator('DtDdWrapper');
$dtDdWrapper->setDdAttrib('style', 'display: none;');
}
$subForm->addElement('text', 'usePromoCodeValue');
$groupElements[] = 'usePromoCodeValue';
$subForm->getElement('usePromoCodeValue')->getDecorator('HtmlTag')->setOption('style', 'display: none;');
$applyButton = $this->getView()->formButton(
'usePromoCodeApply',
'Apply',
array(
'onclick' => 'App_ECommerce_Checkout_OtherServices.applyPromoCode(this)',
'class' => 'btn'
)
);
$subForm->getElement('usePromoCodeValue')
->getDecorator('ViewHelper')
->setAdditionalHtmlAfterElement($applyButton);
$subForm->addElement('static', 'usePromoCodeStatic', array('value' => ''));
$groupElements[] = 'usePromoCodeStatic';
if ($hasPromo) {
$staticValue = $cart->renderPromoCodeDescription();
$staticValue .= 'remove';
$subForm->getElement('usePromoCodeStatic')->setValue($staticValue);
} else {
/** @var Qs_Form_Decorator_HtmlTag $dtDdWrapper */
$dtDdWrapper = $subForm->getElement('usePromoCodeStatic')->getDecorator('HtmlTag');
$dtDdWrapper->setOption('style', 'display: none;');
}
}
/** @var App_Doc_Site $doc */
$doc = Zend_Registry::get('doc');
if (Qs_SiteMap::getAliasByItem('ECommerce_GiftCard_Admin_') && $doc->getAuth()->isLoggedIn()) {
$hasGift = (bool) $cart->getData('giftCardCode');
$subForm->addElement(
'checkbox',
'useGiftCard',
array('label' => 'I want to use a Gift Card', 'decoration' => 'simple')
);
$groupElements[] = 'useGiftCard';
if ($hasGift) {
/** @var Qs_Form_Decorator_DtDdWrapper $dtDdWrapper */
$dtDdWrapper = $subForm->getElement('useGiftCard')->getDecorator('DtDdWrapper');
$dtDdWrapper->setDdAttrib('style', 'display: none;');
}
$subForm->addElement('text', 'useGiftCardValue');
$groupElements[] = 'useGiftCardValue';
$subForm->getElement('useGiftCardValue')->getDecorator('HtmlTag')->setOption('style', 'display: none;');
$redeemButton = $this->getView()->formButton(
'useGiftCardApply',
'Redeem Card',
array(
'onclick' => 'App_ECommerce_Checkout_OtherServices.applyGiftCard(this)',
'class' => 'btn'
)
);
$subForm->getElement('useGiftCardValue')
->getDecorator('ViewHelper')
->setAdditionalHtmlAfterElement($redeemButton);
$subForm->addElement('static', 'useGiftCardStatic', array('value' => ''));
$groupElements[] = 'useGiftCardStatic';
if ($hasGift) {
$staticValue = $cart->renderGiftCardDescription();
$staticValue .= 'remove';
$subForm->getElement('useGiftCardStatic')->setValue($staticValue);
} else {
/** @var Qs_Form_Decorator_HtmlTag $dtDdWrapper */
$dtDdWrapper = $subForm->getElement('useGiftCardStatic')->getDecorator('HtmlTag');
$dtDdWrapper->setOption('style', 'display: none;');
}
}
if (!empty($groupElements)) {
$subForm->addDisplayGroup($groupElements, 'additionalGroup');
}
return $this;
}
public function setDefaults(array $defaults = array())
{
parent::setDefaults($defaults);
if (null !== ($serviceShippingSubForm = $this->getSubForm(App_ECommerce_Checkout_View::SHIPPING_SECTION_DATA))) {
/** @var $serviceShippingSubForm App_ECommerce_Checkout_Form_Service_Shipping */
$serviceShippingSubForm->reinitData($this->_getData());
}
if (null !== ($servicePaymentSubForm = $this->getSubForm(App_ECommerce_Checkout_View::PAYMENT_SECTION_DATA))) {
/** @var $servicePaymentSubForm App_ECommerce_Checkout_Form_Service_Payment */
$servicePaymentSubForm->reinitData($this->_getData());
}
return $this;
}
protected function _initButtons()
{
parent::_initButtons();
$this->getElement('btnSubmit')->setLabel('Continue Checkout');
$this->removeElement('btnCancel');
return $this;
}
/**
* Return subform for checkout
*
* @param string $type
* @return Qs_Form_SubForm
*/
protected function _getCheckoutSubform($type)
{
$subFormClass = 'App_ECommerce_Checkout_Form_Service_' . ucfirst($type);
$form = false;
$defaults = [];
if (!empty($this->_defaults['id'])) {
$defaults['orderId'] = $this->_defaults['id'];
}
if (class_exists($subFormClass)) {
$form = new $subFormClass(['orderSummary' => $this->getOrderSummary(), 'defaults' => $defaults]);
}
return $form;
}
protected function _initSummaryInfo()
{
$item = new Qs_Doc_Item($this->getOrderSummary());
Qs_Smarty_Text::setTemplate(file_get_contents($item->tpl));
$summary = Qs_Smarty_Text::render(array('item' => $item));
$this->addElement('static', 'orderTotal', array('value' => $summary));
$this->getElement('orderTotal')->getDecorator('Label')->setTagOption('class', 'hidden');
$this->addDisplayGroup(array('orderTotal'), 'orderSummary', array('legend' => 'Order Summary'));
return $this;
}
public function render(Zend_View_Interface $view = null)
{
/** @var $doc App_Doc_Admin */
$doc = Zend_Registry::get('doc');
$doc->addScript('js/app/ECommerce/checkout.js');
$options = [
'orderTotal' => Qs_Array::get($this->getOrderSummary(), 'total'),
'noPaymentMessage' => $this->getDoc()->getSmarty()->fetch('ECommerce/Checkout/noPaymentMessage.tpl')
];
$options = $this->getScriptOptions() + $options;
if ($doc->isGtmEnabled()) {
$options['gtmProducts'] = Qs_Gtm::eeGetProducts($this->_getCart()->getList(), Qs_Gtm::DATASET_CHECKOUT);
}
$params = [$this->getId(), $options];
$doc->addInitFunction('App_ECommerce_Checkout.init', $params);
$params = array(App_ECommerce_Checkout_View::PAYMENT_SECTION_DATA . '-additionalGroup-element');
$doc->addInitFunction('App_ECommerce_Checkout_OtherServices.init', $params);
if ($doc->isGtmEnabled()) {
if ($this->_ajaxValidation) {
$this->setScriptOption('onSuccessCallback', 'App_ECommerce_Checkout.gtmCheckout');
} else {
$doc->addInitFunction('App_ECommerce_Checkout.initGtmOnSubmit');
}
}
return parent::render($view);
}
}