首页>Magento>source

我希望在magento收到订单后,增加向客户客户询问是否在成功页面上创建帐户的可能性.

Update:   我认为这个函数名称是Checkout后注册。

最新回答
  • 2019-12-5
    1 #

    根据截图判断,您使用的是自定义主题.这就是为什么你需要首先本地化这个页面(成功页面).确保它有调用 getChildHtml .如果没有这样的调用,那么你应该为我们要创建的块添加它.我把rwd主题作为基础。

    如果您没有用于自定义的模块,请创建它.就我而言,这是 Test_Module .网上有很多文章,您可以在其中找到如何创建模块。

    我们需要一个新的块 Test_Module_Block_Checkout_Success_Register   我们放置了可以在以后扩展的主要逻辑。

    <?php
    /**
     * Created by PhpStorm.
     * User: usr
     * Date: 6/2/16
     * Time: 10:12 AM
     */
    class Test_Module_Block_Checkout_Success_Register extends Mage_Core_Block_Template
    {
        /**
         * @var Mage_Sales_Model_Order|null
         */
        protected $order;
        public function __construct()
        {
            parent::__construct();
        }
        /**
         * @param Mage_Sales_Model_Order $order
         * @return Test_Module_Block_Checkout_Success_Register
         */
        public function setOrder(Mage_Sales_Model_Order $order)
        {
            $this->order = $order;
            return $this;
        }
        /**
         * @return Mage_Sales_Model_Order|null
         */
        public function getOrder()
        {
            return $this->order;
        }
        /**
         * Prepare customer data for the register form
         *
         * @return mixed|Varien_Object
         */
        public function updateFormData()
        {
            $data = $this->getData('form_data');
            if (is_null($data)) {
                /** @var array $formData */
                $formData = Mage::getSingleton('customer/session')->getCustomerFormData(true);
                $order = $this->getOrder();
                $customerData = [
                    'email' => $order->getCustomerEmail(),
                    'firstname' => $order->getCustomerFirstname(),
                    'lastname' => $order->getCustomerLastname(),
                    'city' => $order->getBillingAddress()->getCity(),
                    'country' => $order->getBillingAddress()->getCountry(),
                    'region_id' => $order->getBillingAddress()->getRegionId(),
                ];
                $data = new Varien_Object();
                if ($formData) {
                    $data->addData($formData);
                    $data->setCustomerData(1);
                }
                $data->addData($customerData);
                if (isset($data['region_id'])) {
                    $data['region_id'] = (int)$data['region_id'];
                }
                $this->setData('form_data', $data);
            }
            return $data;
        }
        /**
         * Prepare block data:
         * > Get order id
         * > load order
         * > set data to the block
         *
         * @return $this
         */
        protected function _beforeToHtml()
        {
            parent::_beforeToHtml();
            /** @var Mage_Checkout_Block_Onepage_Success|null $successBlock */
            $successBlock = $this->getLayout()->getBlock('checkout.success');
            if (!$successBlock) {
                return $this;
            }
            /** @var string $orderId order Increment Id here (not the real id!) */
            $orderId = $successBlock->getOrderId();
            /** @var Mage_Sales_Model_Order $order */
            $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
            $guest = $order->getCustomerIsGuest();
            if (!$guest) {
                return $this;
            }
            $this->setOrder($order);
            $this->updateFormData();
            return $this;
        }
    }
    ?>
    

    在这个区块中,我们使用方法 _beforeTOHtml   在哪里检查块图,并加载包含所有所需客户信息的订单.如果你是一个完美主义者,你可以将这个部分分开用于不同的方法,但总体方法是明确的.我们正在收到订单 icrement_id   来自父母区块。

    请注意! 如果您的主题是使用另一个订单接收系统,那么只需用您自己的主题替换它。

    然后,我们需要一份 customer/form/register.phtml   我们将编辑的模板以获得所需的结果.将文件复制到模块的templates文件夹中.在我的情况下,这是 app/design/frontend/base/default/template/test/customer/form/register.phtml .接下来,从模板中删除所有不必要的信息,但保留所有输入字段和小部件调用.这是我的模板:

    <?php echo $this->getChildHtml('form_fields_before') ?>
    <?php echo $this->getChildHtml('customer.form.register.extra') ?>
    <form action="<?php echo $this->getPostActionUrl() ?>" method="post" id="form-validate">
        <!--        Hide customer main data fields-->
        <div class="fieldset" hidden="hidden">
            <input type="hidden" name="success_url" value="<?php echo $this->getSuccessUrl() ?>"/>
            <input type="hidden" name="error_url" value="<?php echo $this->getErrorUrl() ?>"/>
            <ul class="form-list" hidden="hidden">
                <li class="fields">
                    <?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getFormData())->setForceUseCustomerAttributes(true)->toHtml() ?>
                </li>
                <li>
                    <label for="email_address" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label>
                    <div class="input-box">
                        <input type="text" name="email" id="email_address"
                               value="<?php echo $this->escapeHtml($this->getFormData()->getEmail()) ?>"
                               title="<?php echo $this->__('Email Address') ?>"
                               class="input-text validate-email required-entry"/>
                    </div>
                </li>
                <?php $_dob = $this->getLayout()->createBlock('customer/widget_dob') ?>
                <?php if ($_dob->isEnabled()): ?>
                    <li><?php echo $_dob->setDate($this->getFormData()->getDob())->toHtml() ?></li>
                <?php endif ?>
                <?php $_taxvat = $this->getLayout()->createBlock('customer/widget_taxvat') ?>
                <?php if ($_taxvat->isEnabled()): ?>
                    <li><?php echo $_taxvat->setTaxvat($this->getFormData()->getTaxvat())->toHtml() ?></li>
                <?php endif ?>
                <?php $_gender = $this->getLayout()->createBlock('customer/widget_gender') ?>
                <?php if ($_gender->isEnabled()): ?>
                    <li><?php echo $_gender->setGender($this->getFormData()->getGender())->toHtml() ?></li>
                <?php endif ?>
            </ul>
        </div>
        <?php if ($this->getShowAddressFields()): ?>
            <!--        Hide address fields-->
            <div class="fieldset" hidden="hidden">
                <input type="hidden" name="create_address" value="1"/>
                <h2 class="legend"><?php echo $this->__('Address Information') ?></h2>
                <ul class="form-list">
                    <li class="fields">
                        <div class="field">
                            <label for="company"><?php echo $this->__('Company') ?></label>
                            <div class="input-box">
                                <input type="text" name="company" id="company"
                                       value="<?php echo $this->escapeHtml($this->getFormData()->getCompany()) ?>"
                                       title="<?php echo $this->__('Company') ?>"
                                       class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('company') ?>"/>
                            </div>
                        </div>
                        <div class="field">
                            <label for="telephone" class="required"><em>*</em><?php echo $this->__('Telephone') ?></label>
                            <div class="input-box">
                                <input type="text" name="telephone" id="telephone"
                                       value="<?php echo $this->escapeHtml($this->getFormData()->getTelephone()) ?>"
                                       title="<?php echo $this->__('Telephone') ?>"
                                       class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('telephone') ?>"/>
                            </div>
                        </div>
                    </li>
                    <?php $_streetValidationClass = $this->helper('customer/address')->getAttributeValidationClass('street'); ?>
                    <li class="wide">
                        <label for="street_1" class="required"><em>*</em><?php echo $this->__('Street Address') ?></label>
                        <div class="input-box">
                            <input type="text" name="street[]"
                                   value="<?php echo $this->escapeHtml($this->getFormData()->getStreet(1)) ?>"
                                   title="<?php echo $this->__('Street Address') ?>" id="street_1"
                                   class="input-text <?php echo $_streetValidationClass ?>"/>
                        </div>
                    </li>
                    <?php $_streetValidationClass = trim(str_replace('required-entry', '', $_streetValidationClass)); ?>
                    <?php for ($_i = 2, $_n = $this->helper('customer/address')->getStreetLines(); $_i <= $_n; $_i++): ?>
                        <li class="wide">
                            <div class="input-box">
                                <input type="text" name="street[]"
                                       value="<?php echo $this->escapeHtml($this->getFormData()->getStreet($_i)) ?>"
                                       title="<?php echo $this->__('Street Address %s', $_i) ?>"
                                       id="street_<?php echo $_i ?>"
                                       class="input-text <?php echo $_streetValidationClass ?>"/>
                            </div>
                        </li>
                    <?php endfor; ?>
                    <li class="fields">
                        <div class="field">
                            <label for="city" class="required"><em>*</em><?php echo $this->__('City') ?></label>
                            <div class="input-box">
                                <input type="text" name="city"
                                       value="<?php echo $this->escapeHtml($this->getFormData()->getCity()) ?>"
                                       title="<?php echo $this->__('City') ?>"
                                       class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('city') ?>"
                                       id="city"/>
                            </div>
                        </div>
                        <div class="field">
                            <label for="region_id" class="required"><em>*</em><?php echo $this->__('State/Province') ?>
                            </label>
                            <div class="input-box">
                                <select id="region_id" name="region_id" title="<?php echo $this->__('State/Province') ?>"
                                        class="validate-select" style="display:none;">
                                    <option
                                        value=""><?php echo $this->__('Please select region, state or province') ?></option>
                                </select>
                                <script type="text/javascript">
                                    //<![CDATA[
                                    $('region_id').setAttribute('defaultValue', "<?php echo $this->getFormData()->getRegionId() ?>");
                                    //]]>
                                </script>
                                <input type="text" id="region" name="region"
                                       value="<?php echo $this->escapeHtml($this->getRegion()) ?>"
                                       title="<?php echo $this->__('State/Province') ?>"
                                       class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('region') ?>"
                                       style="display:none;"/>
                            </div>
                        </div>
                    </li>
                    <li class="fields">
                        <div class="field">
                            <label for="zip" class="required"><em>*</em><?php echo $this->__('Zip/Postal Code') ?></label>
                            <div class="input-box">
                                <input type="text" name="postcode"
                                       value="<?php echo $this->escapeHtml($this->getFormData()->getPostcode()) ?>"
                                       title="<?php echo $this->__('Zip/Postal Code') ?>" id="zip"
                                       class="input-text validate-zip-international <?php echo $this->helper('customer/address')->getAttributeValidationClass('postcode') ?>"/>
                            </div>
                        </div>
                        <div class="field">
                            <label for="country" class="required"><em>*</em><?php echo $this->__('Country') ?></label>
                            <div class="input-box">
                                <?php echo $this->getCountryHtmlSelect() ?>
                            </div>
                        </div>
                    </li>
                </ul>
                <input type="hidden" name="default_billing" value="1"/>
                <input type="hidden" name="default_shipping" value="1"/>
            </div>
        <?php endif; ?>
        <div class="fieldset">
            <h2 class="legend"><?php echo $this->__('Save Your Information For Next Time!') ?></h2>
            <ul class="form-list">
                <li class="fields">
                    <div class="field">
                        <label for="password" class="required"><em>*</em><?php echo $this->__('Password') ?></label>
                        <div class="input-box">
                            <input type="password" name="password" id="password" title="<?php echo $this->__('Password') ?>"
                                   class="input-text required-entry validate-password"/>
                        </div>
                    </div>
                    <div class="field">
                        <label for="confirmation" class="required"><em>*</em><?php echo $this->__('Confirm Password') ?>
                        </label>
                        <div class="input-box">
                            <input type="password" name="confirmation" title="<?php echo $this->__('Confirm Password') ?>"
                                   id="confirmation" class="input-text required-entry validate-cpassword"/>
                        </div>
                    </div>
                </li>
                <?php echo $this->getChildHtml('form.additional.info'); ?>
                <?php if ($this->isNewsletterEnabled()): ?>
                    <li class="control">
                        <div class="input-box">
                            <input type="checkbox" name="is_subscribed"
                                   title="<?php echo $this->__('Sign Up for Newsletter') ?>" value="1"
                                   id="is_subscribed"<?php if ($this->getFormData()->getIsSubscribed()): ?> checked="checked"<?php endif; ?>
                                   class="checkbox"/>
                        </div>
                        <label for="is_subscribed"><?php echo $this->__('Sign Up for Newsletter') ?></label>
                        <?php /* Extensions placeholder */ ?>
                        <?php echo $this->getChildHtml('customer.form.register.newsletter') ?>
                    </li>
                <?php endif ?>
            </ul>
        </div>
        <div class="buttons-set">
            <p class="required"><?php echo $this->__('* Required Fields') ?></p>
            <button type="submit" title="<?php echo $this->__('Register') ?>" class="button">
                <span><span><?php echo $this->__('Register') ?></span></span></button>
        </div>
    </form>
    <script type="text/javascript">
        //<![CDATA[
        var dataForm = new VarienForm('form-validate', true);
        <?php if($this->getShowAddressFields()): ?>
        new RegionUpdater('country', 'region', 'region_id', <?php echo $this->helper('directory')->getRegionJson() ?>, undefined, 'zip');
        <?php endif; ?>
        //]]>
    </script>
    

    对于不应显示的字段集,但默认情况下将从块中填充,我们应该设置属性 hidden="hidden" .如果您完全删除它们,则无法完成注册.只有具有密码的字段集和具有订阅的字段才会保持可见。

    因此,我们正在使用此表单的标准注册块,然后我们需要一个中间模板用于我们的块,其中此标准块与我们的模板 app/design/frontend/base/default/template/test/checkout/register.phtml   将被召唤。

    <?php
    if (!$this->getOrder()) {
        return;
    }
    $this->getChild('customer_form_register')->setData('form_data', $this->getData('form_data'));
    echo $this->getChildHtml('customer_form_register');
    ?>
    

    最后,我们需要一个布局,我们在其中标记 app/design/frontend/base/default/layout/test.xml的所有更改 :

    <?xml version="1.0"?>
    <layout version="0.1.0">
        <checkout_onepage_success>
            <reference name="content">
                <block type="test/checkout_success_register" name="checkout.success.register.form" template="test/checkout/register.phtml">
                    <block type="customer/form_register" name="customer_form_register" template="test/customer/form/register.phtml">
                        <block type="page/html_wrapper" name="customer.form.register.fields.before" as="form_fields_before" translate="label">
                            <label>Form Fields Before</label>
                        </block>
                    </block>
                </block>
            </reference>
        </checkout_onepage_success>
    </layout>
    

    rwd主题的结果应如下:

    所有隐藏字段都带有订单中的值:

    注册成功完成:

    请注意,这只是一个模型,您应该根据自己的主题进行自定义,并根据所需的结果进行更正。

    我希望这些信息有用.如果您有任何疑问 - 请随时问他们。

  • magento2:magento 2如何在customer_save_after_data_object观察者中创建商店和商店视图?
  • Paypal没有显示我们在Magento结帐的货币