禁用付款选项 - 仅限特定产品 - magento的货到付款

时间:2012-12-23 13:30:37

标签: magento

我正在使用Magento 1.4.1.1,我想禁用某些产品的付款选项。我想只显示某些产品的货到付款方式,需要隐藏其他产品。

有什么机构可以帮我吗?我怎么能管理这个?管理员有没有选择,或者我需要自定义代码。如果是这样,你能不能给我一个代码和文件的确切路径?

提前谢谢。

2 个答案:

答案 0 :(得分:9)

过滤付款方式的一种不引人注目的方式是实施名为payment_method_is_active的事件的观察员

步骤
1 GT;在config.xml中注册事件:'payment_method_is_active'。 在app / code / local / MagePsycho / Paymentfilter / etc / config.xml中添加以下xml代码:

...
<global>
    ...
    <events>
        <payment_method_is_active>
            <observers>
                <paymentfilter_payment_method_is_active>
                    <type>singleton</type>
                    <class>paymentfilter/observer</class>
                    <method>paymentMethodIsActive</method>
                </paymentfilter_payment_method_is_active>
            </observers>
        </payment_method_is_active>
    </events>
    ...
</global>
...

2 - ;实现观察者模型 创建观察者文件:app / code / local / MagePsycho / Paymentfilter / Model / Observer.php并粘贴以下代码:

<?php
/**
 * @category   MagePsycho
 * @package    MagePsycho_Paymentfilter
 * @author     magepsycho@gmail.com
 * @website    http://www.magepsycho.com
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
    */
class MagePsycho_Paymentfilter_Model_Observer {

    public function paymentMethodIsActive(Varien_Event_Observer $observer) {
        $event           = $observer->getEvent();
        $method          = $event->getMethodInstance();
        $result          = $event->getResult();
        $currencyCode    = Mage::app()->getStore()->getCurrentCurrencyCode();


            if($someTrueConditionGoesHere){
                $result->isAvailable = true;
            }else{
                $result->isAvailable = false;
            }

    }

}

答案 1 :(得分:3)

首先创建一个产品属性(名称:'magepal_payment_filter_by_product',输入:yes / no)以识别这些产品。

例如Magento v1.7的基础你可以

  1. 自动启用付款模块,以编程方式查看https://stackoverflow.com/a/14023210/1191288

  2. 启用所有适用的付款模块并过滤显示

  3. 的模块

    在/app/code/local/MagePal/PaymentFilterByProduct/etc/config.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <MagePal_PaymentFilterByProduct>
                <version>1.0.1</version>
            </MagePal_PaymentFilterByProduct>
        </modules>
        <global>
            <helpers>
                <paymentfilterbyproduct>
                    <class>MagePal_PaymentFilterByProduct_Helper</class>
                </paymentfilterbyproduct>
                <payment>
                    <rewrite>
                        <data>MagePal_PaymentFilterByProduct_Helper_Payment_Data</data>
                    </rewrite>
                </payment>            
            </helpers>      
        </global>
    </config>
    

    在/app/code/local/MagePal/PaymentFilterByProduct/Helper/Payment/Data.php

    <?php
    class MagePal_PaymentFilterByProduct_Helper_Payment_Data extends Mage_Payment_Helper_Data
    {
    
        public function getStoreMethods($store = null, $quote = null)
        {   
            $methods = parent::getStoreMethods($store, $quote);
    
            if(!Mage::getStoreConfig('paymentfilterbyproduct/general_option/paymentfilterbyproduct_enable', Mage::app()->getStore()) || !$quote){
                return $methods;
            }
    
            //get a list of product in cart
            $cart = Mage::getSingleton('checkout/session');
    
            $specialProductInCart = array();
    
            foreach ($cart->getQuote()->getAllItems() as $item) {
                $specialProductInCart[] = $item->getMagepalPaymentFilterByProduct();                 
            }
    
            // if special product in cart 
            // need to if check $item->getMagepalPaymentFilterByProduct() return 'yes/no' or '0/1)
            if(in_array('yes', $specialProductInCart)){
               $filter_csv = Mage::getStoreConfig('paymentfilterbyproduct/filter_option/paymentfilter_special_products', Mage::app()->getStore()); 
            }
            else{
                $filter_csv = Mage::getStoreConfig('paymentfilterbyproduct/filter_option/paymentfilter_all_product', Mage::app()->getStore());
            }
    
            $filter_array = explode(",", $filter_csv);
    
            foreach ($methods as $k => $method){
                if(!in_array($method->getCode(), $filter_array))
                    unset($methods[$k]);       
            }//methods
    
            return $methods;
        }
    }
    

    在/app/code/local/MagePal/PaymentFilterByProduct/etc/system.xml

    <?xml version="1.0"?>
    <config>
        <tabs>
            <magepal translate="label" module="paymentfilterbyproduct">
                <label>MagePal</label>
                <sort_order>900</sort_order>
            </magepal>
        </tabs>
        <sections>
            <paymentfilterbyproduct translate="label" module="paymentfilterbyproduct">
                <label>Payment Method Filter by Product</label>
                <tab>magepal</tab>
                <sort_order>1000</sort_order>
                <show_in_default>1</show_in_default>
                <show_in_website>1</show_in_website>
                <show_in_store>1</show_in_store>
                <groups>
                    <general_option translate="label">
                        <label>General Options</label>
                        <frontend_type>text</frontend_type>
                        <sort_order>1</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                        <fields>
                            <paymentfilter_enable translate="label">
                                <label>Enable Payment Filter</label>
                                <frontend_type>select</frontend_type>
                                <source_model>adminhtml/system_config_source_yesno</source_model>
                                <sort_order>50</sort_order>
                                <show_in_default>1</show_in_default>
                                <show_in_website>1</show_in_website>
                                <show_in_store>1</show_in_store>
                            </paymentfilter_enable>
                        </fields>
                    </general_option>
                    <filter_option translate="label">
                        <label>Payment Method Filter Configuration</label>
                        <frontend_type>text</frontend_type>
                        <sort_order>2</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                        <comment>Please enable all applicable payment methods in system payment config</comment>
                        <fields>
                            <paymentfilter_all_products translate="label">
                                <label>Select Default Payment option for All Products</label>
                                <frontend_type>multiselect</frontend_type>
                                <source_model>MagePal_PaymentFilterByProduct_ActivePaymentMethod</source_model>
                                <sort_order>30</sort_order>
                                <show_in_default>1</show_in_default>
                                <show_in_website>1</show_in_website>
                                <show_in_store>1</show_in_store>
                            </paymentfilter_admin>
                            <paymentfilter_special_products translate="label">
                                <label>Select Payments for Cart with Special Products</label>
                                <frontend_type>multiselect</frontend_type>
                                <source_model>MagePal_PaymentFilterByProduct_ActivePaymentMethod</source_model>
                                <sort_order>40</sort_order>
                                <show_in_default>1</show_in_default>
                                <show_in_website>1</show_in_website>
                                <show_in_store>1</show_in_store>
                            </paymentfilter_store>
                        </fields>
                    </filter_option>
                </groups>
            </paymentfilterbyproduct>
        </sections>
    </config>
    

    在/app/code/local/MagePal/PaymentFilterByProduct/Helper/Data.php

    <?php 
    class MagePal_PaymentFilterByProduct_Helper_Data extends Mage_Core_Block_Template
    {
    }
    

    在/app/code/local/MagePal/PaymentFilterByProduct/ActivePaymentMethod.php

    <?php
    class MagePal_PaymentFilterByProduct_ActivePaymentMethod
    {
        //get all active (enable) payment method
        public function toOptionArray()
        {
           $payments = Mage::getSingleton('payment/config')->getActiveMethods();
    
           foreach ($payments as $paymentCode=>$paymentModel) {
               if($paymentModel->canUseCheckout() == 1){
                    $paymentTitle = Mage::getStoreConfig('payment/'.$paymentCode.'/title');
                    $methods[$paymentCode] = array(
                        'label'   => $paymentTitle,
                        'value' => $paymentCode,
                    );
               }
            }
    
            return $methods;
        }
    }
    

    在/app/etc/modules/MagePal_PaymentFilterByProduct.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <MagePal_PaymentFilterByProduct>
                <active>true</active>
                <codePool>local</codePool>
            </MagePal_PaymentFilterByProduct>
        </modules>
    </config>