首頁>Magento>source

我在這裏看過很多關於此的帖子,但没有一个解決方案對我有用,所以我想我会問自己發佈自己的代碼。

我希望在客戶訪問购物車頁面時選擇預設送货方式.从阅讀其他帖子我知道它没有被設置,因為使用者尚未添加他们的傳遞地址。

有没有办法可以設置預設投放方式(統一费率),直到使用者選擇其他方法.這是.phtml檔案,顯示了可用的交付方法:

<?php $sub = Mage::helper('checkout/cart')->getQuote()->getSubtotal() ?>
<div class="shipping">
    <div class="shipping-form">
            <?php if (($_shippingRateGroups = $this->getEstimateRates())): ?>
        <form id="co-shipping-method-form" action="<?php echo $this->getUrl('checkout/cart/estimateUpdatePost') ?>">
             <?php $quote = Mage::getSingleton('checkout/session')->getQuote();
                                $cartItems = $quote->getAllVisibleItems();
                                foreach ($cartItems as $item){
                                $productId = $item->getProductId();
                                $product = Mage::getModel('catalog/product')->load($productId);
                                       if($product->getData('direct_delivery_product')==1){?>
                                 <div class="direct_message">
                                 <?php echo 'You have direct delivery products in your shopping cart. This means it may take longer than the standard 2-5 working days to deliver.';?></div>
                                    <?php 
                                     break;
                                 }}?>
    <div class="block block-login">
        <div class="block-slider">
            <ul class="slides">
                <li>
                    <div class="block-title">
                        <h2><span style="font-size:16px; letter-spacing: normal;"><?php echo $this->__('Shipping Options') ?></span></h2>
                    </div>
                    <div class="block-content">
                        <?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
                        <?php /* Extensions placeholder */ ?>
                        <?php echo $this->getChildHtml('customer.form.login.extra')?>
                        <?php $count = 0;?>
                        <form action="<?php echo $this->getPostActionUrl() ?>" method="post" id="login-form">
                            <ul>
                        <?php foreach ($_shippingRateGroups as $code => $_rates): ?>
                        <?php foreach ($_rates as $_rate): ?>
                            <li<?php if ($_rate->getErrorMessage()) echo ' class="error-msg"';?>>
                               <?php if ($_rate->getErrorMessage()): ?>
                                    <?php echo $this->escapeHtml($_rate->getErrorMessage()) ?>
                               <?php else:?>
                                    <input name="estimate_method" type="radio" value="<?php echo $this->escapeHtml($_rate->getCode()) ?>" id="s_method_<?php echo $_rate->getCode() ?>"<?php if($_rate->getCode()===$this->getAddressShippingMethod()) echo ' checked="checked"' ?> class="radio" />
                                  <label for="s_method_<?php echo $_rate->getCode() ?>">
                                   <?php if (($sub > 75 && $count < 1 ) ||(Mage::getSingleton('customer/session')->getCustomerGroupId()==5 && $count < 1 ))
                                        {echo "Free Shipping";}
                                        else{ echo $this->escapeHtml($_rate->getMethodTitle()); }?>
                                    <?php if ($sub == 0 && $count == 0)
                                    { echo "£4.99";}
                                    else{?>
                                    <?php $_excl = $this->getShippingPrice($_rate->getPrice(), $this->helper('tax')->displayShippingPriceIncludingTax()); ?>
                                    <?php $_incl = $this->getShippingPrice($_rate->getPrice(), true); ?>
                                    <?php echo $_excl; }?>
                                    </label>
                                <?php ++$count;?>   
                            <?php endif ?>
                        <?php endforeach; ?>
                          <?php endforeach; ?>
                        </ul>                         <div class="buttons-set">
                <button type="submit" title="<?php echo $this->__('Update Total') ?>" class="button" name="do" value="<?php echo $this->__('Update Total') ?>"><span><span><?php echo $this->__('Update Total') ?></span></span></button>
                        </div>
                            <a href="<?php echo $this->getForgotPasswordUrl() ?>" class="forgot-password" id="forgot-password"><?php echo $this->__('Click here if shipping internationally to get a quote') ?></a>
                        </form>
                    </div>
                </li>

                <li>
                    <div class="block-title">
                        <strong><span><?php echo $this->__('International Shipping') ?></span></strong>
                    </div>
                    <div class="block-content">
          <form action="<?php echo $this->getUrl('checkout/cart/estimatePost') ?>" method="post" id="shipping-zip-form">
            <p><?php echo $this->__('Enter your destination to get a shipping estimate.') ?></p>
            <ul class="form-list">
                <li>
                    <div class="input-box">
                        <?php echo Mage::getBlockSingleton('directory/data')->getCountryHtmlSelect($this->getEstimateCountryId()) ?>
                    </div>
                </li>
            </ul>
            <div class="buttons-set">
                <button type="button" title="<?php echo $this->__('Get a Quote') ?>" onclick="coShippingMethodForm.submit()" class="button"><span><span><?php echo $this->__('Get a Quote') ?></span></span></button>
                 <a href="<?php echo $this->getForgotPasswordUrl() ?>" class="forgot-password" id="back-login"><?php echo $this->__('Back to UK shipping prices'); ?></a>
            </div>
        </form>
                    </div>
                </li>
            </ul>
        </div>
    </div>
</div>
 <?php endif; ?>
    <script type="text/javascript">
        //<![CDATA[
        var dataForm = new VarienForm('login-form', true);
        var dataForgetForm = new VarienForm('form-validate', true);
        //]]>
    </script>
        <script type="text/javascript">
        //<![CDATA[
            var coShippingMethodForm = new VarienForm('shipping-zip-form');
            var countriesWithOptionalZip = <?php echo $this->helper('directory')->getCountriesWithOptionalZip(true) ?>;
            coShippingMethodForm.submit = function () {
                var country = $F('country');
                var optionalZip = true;

                return VarienForm.prototype.submit.bind(coShippingMethodForm)();
            }
        //]]>
</script>
    </div>

所以我想要的是預設情况下在我的购物車中選擇統一费率,這樣使用者就不必選擇價格然後點击更新,如果他们想要一个不同的送货方式,他们只需要這樣做.

谢谢你能提供帮助。

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

    第43行

    <input name="estimate_method" type="radio" value="<?php echo $this->escapeHtml($_rate->getCode()) ?>" id="s_method_<?php echo $_rate->getCode() ?>"<?php if($_rate->getCode()===$this->getAddressShippingMethod()) echo ' checked="checked"' ?> class="radio" />
    

    正在檢查複選框( echo checked = "checked" )如果條件

    $_rate->getCode()===$this->getAddressShippingMethod()   是真的.

    我建議在條件中添加一些內容,例如 -

    ([[existing condition]] || $rate->getCode()=="flatrate"

    有了這个,你可以在這个條件成立時檢查它,即使原来的條件不是這樣.

    希望這能指出你正確的方向。

  • 2019-12-5
    2 #

    以前的答案似乎是正確的, 另外,我只是快速瀏覽一下這段代碼,如果你想成為一名優秀的開發人員,首先, NEVER   這樣做:

    {echo "Free Shipping";}
    

    echo'您的购物車中有直接送货产品.這意味着交付時間可能超過標準的2-5个工作日。';

    請,让所有的字元串都可以翻译(你可以用: $this->__('Free Shipping'); 並尝試遵循Magento編碼標準。

  • 2019-12-5
    3 #

    checkout>cart>shipping.phtml的最後放置指令碼   檔案

    <script type="text/javascript">
      jQuery("#Id_Of_Your_Shipping_Method").attr("checked", true);
    </script>
    

  • Magento2如何获得客戶的自定義屬性及其value?
  • checkout:在第63行的lib / Varien / Crypt / Mcryptphp中呼叫未定義的函式mcrypt_module_open()