WooCommerce - 禁用BACS一种送货方式

时间:2014-04-18 13:17:33

标签: php wordpress woocommerce payment-gateway shipping

我想为特定的送货方式禁用电汇付款方式(bacs网关)。我希望客户在选择货到付款方式时支付运费,但如果选择电汇方式,我希望他们也免费送货。

如果他们选择免费送货,我可以禁用COD,但如果他们选择不免费送货,则无法禁用电汇。

如何针对该特定运输方式禁用BACS?

1 个答案:

答案 0 :(得分:0)

我已经安装了WooCommerce Custom Payment Gateways插件。这激活了一些更多的网关选项,所以我正在使用它并编辑其中一个类。

以下是我修改过的woocommerce-custom-payment-gatewayways / class-wc-custom_payment_gateway_5.php

<?php
/**
 * WC wcCpg5 Gateway Class.
 * Built the wcCpg5 method.
 */
class WC_Custom_Payment_Gateway_5 extends WC_Gateway_BACS {

/**
 * Constructor for the gateway.
 *
 * @return void
 */
public function __construct() {
    global $woocommerce;

    $this->id             = 'wcCpg5';
    $this->icon           = apply_filters( 'woocommerce_wcCpg5_icon', '' );
    $this->has_fields     = false;
    $this->method_title     = __( 'Bacs', 'woocommerce' );

// Load the settings.
$this->init_form_fields();
$this->init_settings();

// Define user set variables
    $this->title        = $this->get_option( 'title' );
    $this->description  = $this->get_option( 'description' );
    $this->instructions = $this->get_option( 'instructions', $this->description );
    $this->enable_for_methods = $this->get_option( 'enable_for_methods', array() );
  // BACS account fields shown on the thanks page and in emails
    $this->account_details = get_option( 'woocommerce_bacs_accounts',
       array(
            array(
               'account_name'   => $this->get_option( 'account_name' ),
               'account_number' => $this->get_option( 'account_number' ),
               'sort_code'      => $this->get_option( 'sort_code' ),
               'bank_name'      => $this->get_option( 'bank_name' ),
               'iban'           => $this->get_option( 'iban' ),
               'bic'            => $this->get_option( 'bic' )
           )
        )
    );

    // Actions
    add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
    add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'save_account_details' ) );
    add_action( 'woocommerce_thankyou_bacs', array( $this, 'thankyou_page' ) );

    // Customer Emails
    add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );

    // Actions.
    if ( version_compare( WOOCOMMERCE_VERSION, '2.0.0', '>=' ) )
        add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( &$this, 'process_admin_options' ) );
    else
        add_action( 'woocommerce_update_options_payment_gateways', array( &$this, 'process_admin_options' ) );
}


/* Admin Panel Options.*/
function admin_options() {
    ?>
    <h3><?php _e('Custom Payment Gateways 5','wcwcCpg5'); ?></h3>
    <table class="form-table">
        <?php $this->generate_settings_html(); ?>
    </table> <?php
}

/* Initialise Gateway Settings Form Fields. */
public function init_form_fields() {
$shipping_methods = array();

    if ( is_admin() )
        foreach ( WC()->shipping->load_shipping_methods() as $method ) {
            $shipping_methods[ $method->id ] = $method->get_title();
        }
    $this->form_fields = array(
        'enabled' => array(
            'title'   => __( 'Enable/Disable', 'woocommerce' ),
            'type'    => 'checkbox',
            'label'   => __( 'Enable Bank Transfer', 'woocommerce' ),
            'default' => 'yes'
         ),
        'title' => array(
            'title'       => __( 'Title', 'woocommerce' ),
            'type'        => 'text',
            'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
            'default'     => __( 'Direct Bank Transfer', 'woocommerce' ),
            'desc_tip'    => true,
        ),
        'description' => array(
            'title'       => __( 'Description', 'woocommerce' ),
            'type'        => 'textarea',
            'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce' ),
            'default'     => __( 'Make your payment directly into our bank account. Please use your Order ID as the payment reference. Your order won\'t be shipped until the funds have cleared in our account.', 'woocommerce' ),
            'desc_tip'    => true,
        ),
        'instructions' => array(
            'title'       => __( 'Instructions', 'woocommerce' ),
            'type'        => 'textarea',
            'description' => __( 'Instructions that will be added to the thank you page and emails.', 'woocommerce' ),
            'default'     => '',
            'desc_tip'    => true,
        ),
        'account_details' => array(
            'type'        => 'account_details'
            ),
        'enable_for_methods' => array(
            'title'             => __( 'Enable for shipping methods', 'woocommerce' ),
            'type'              => 'multiselect',
            'class'             => 'chosen_select',
            'css'               => 'width: 450px;',
            'default'           => '',
            'description'       => __( 'If COD is only available for certain methods, set it up here. Leave blank to enable for all methods.', 'woocommerce' ),
            'options'           => $shipping_methods,
            'desc_tip'          => true,
            'custom_attributes' => array(
                'data-placeholder' => __( 'Select shipping methods', 'woocommerce' )
            )
        )
    );
}
    /**
 * Check If The Gateway Is Available For Use
 *
 * @return bool
 */
public function is_available() {
    $order = null;

    if ( ! $this->enable_for_virtual ) {
        if ( WC()->cart && ! WC()->cart->needs_shipping() ) {
            return false;
        }

        if ( is_page( wc_get_page_id( 'checkout' ) ) && 0 < get_query_var( 'order-pay' ) ) {
            $order_id = absint( get_query_var( 'order-pay' ) );
            $order    = wc_get_order( $order_id );

            // Test if order needs shipping.
            $needs_shipping = false;

            if ( 0 < sizeof( $order->get_items() ) ) {
                foreach ( $order->get_items() as $item ) {
                    $_product = $order->get_product_from_item( $item );

                    if ( $_product->needs_shipping() ) {
                        $needs_shipping = true;
                        break;
                    }
                }
            }

            $needs_shipping = apply_filters( 'woocommerce_cart_needs_shipping', $needs_shipping );

            if ( $needs_shipping ) {
                return false;
            }
        }
    }

    if ( ! empty( $this->enable_for_methods ) ) {

        // Only apply if all packages are being shipped via local pickup
        $chosen_shipping_methods_session = WC()->session->get( 'chosen_shipping_methods' );

        if ( isset( $chosen_shipping_methods_session ) ) {
            $chosen_shipping_methods = array_unique( $chosen_shipping_methods_session );
        } else {
            $chosen_shipping_methods = array();
        }

        $check_method = false;

        if ( is_object( $order ) ) {
            if ( $order->shipping_method ) {
                $check_method = $order->shipping_method;
            }

        } elseif ( empty( $chosen_shipping_methods ) || sizeof( $chosen_shipping_methods ) > 1 ) {
            $check_method = false;
        } elseif ( sizeof( $chosen_shipping_methods ) == 1 ) {
            $check_method = $chosen_shipping_methods[0];
        }

        if ( ! $check_method ) {
            return false;
        }

        $found = false;

        foreach ( $this->enable_for_methods as $method_id ) {
            if ( strpos( $check_method, $method_id ) === 0 ) {
                $found = true;
                break;
            }
        }

        if ( ! $found ) {
            return false;
        }
    }

    return parent::is_available();
}


}

使用此功能,您可以选择为送货方式启用bacs