Initial Commit
This commit is contained in:
commit
2003f39bad
|
|
@ -0,0 +1,210 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Plugin Name: Carrier Account Shipping
|
||||
* Description: Carrier Account Shipping Method for WooCommerce
|
||||
* Version: 1.0.0
|
||||
* Author: Eric Fawcett
|
||||
* Author URI: http://ericfawcett.com
|
||||
*/
|
||||
|
||||
if ( ! defined( 'WPINC' ) )
|
||||
{
|
||||
|
||||
die;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Check if WooCommerce is active
|
||||
*/
|
||||
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) )
|
||||
{
|
||||
function carrieracct_shipping_method()
|
||||
{
|
||||
if ( ! class_exists( 'CarrierAcct_Shipping_Method' ) )
|
||||
{
|
||||
class CarrierAcct_Shipping_Method extends WC_Shipping_Method {
|
||||
/**
|
||||
* Constructor for your shipping class
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->id = 'carrieracct';
|
||||
$this->method_title = __( 'CarrierAcct Shipping', 'carrieracct' );
|
||||
$this->method_description = __( 'Custom Shipping Method for CarrierAcct', 'carrieracct' );
|
||||
|
||||
$this->init();
|
||||
|
||||
$this->enabled = isset( $this->settings['enabled'] ) ? $this->settings['enabled'] : 'no';
|
||||
$this->title = isset( $this->settings['title'] ) ? $this->settings['title'] : __( 'CarrierAcct Shipping', 'carrieracct' );
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Init your settings
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function init()
|
||||
{
|
||||
// Load the settings API
|
||||
$this->init_form_fields();
|
||||
$this->init_settings();
|
||||
|
||||
// Save settings in admin if you have any defined
|
||||
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Define settings field for this shipping
|
||||
* @return void
|
||||
*/
|
||||
function init_form_fields()
|
||||
{
|
||||
|
||||
$this->form_fields = array(
|
||||
|
||||
'enabled' => array(
|
||||
'title' => __( 'Enable', 'carrieracct' ),
|
||||
'type' => 'checkbox',
|
||||
'description' => __( 'Enable this shipping.', 'carrieracct' ),
|
||||
'default' => 'yes'
|
||||
),
|
||||
|
||||
'title' => array(
|
||||
'title' => __( 'Title', 'carrieracct' ),
|
||||
'type' => 'text',
|
||||
'description' => __( 'Title to be display on site', 'carrieracct' ),
|
||||
'default' => __( 'CarrierAcct Shipping', 'carrieracct' )
|
||||
),
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This function is used to calculate the shipping cost. Within this function we can check for weights, dimensions and other parameters.
|
||||
*
|
||||
* @access public
|
||||
* @param mixed $package
|
||||
* @return void
|
||||
*/
|
||||
public function calculate_shipping( $package = array( ))
|
||||
{
|
||||
|
||||
$rate = array(
|
||||
'id' => $this->id,
|
||||
'label' => $this->title,
|
||||
'cost' => '0',
|
||||
'calc_tax' => 'per_order',
|
||||
'package' => $package,
|
||||
);
|
||||
|
||||
// Register the rate
|
||||
$this->add_rate( $rate );
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
add_action( 'woocommerce_shipping_init', 'carrieracct_shipping_method' );
|
||||
|
||||
function add_carrieracct_shipping_method( $methods )
|
||||
{
|
||||
$methods['carrieracct'] = 'CarrierAcct_Shipping_Method';
|
||||
return $methods;
|
||||
}
|
||||
|
||||
|
||||
add_filter( 'woocommerce_shipping_methods', 'add_carrieracct_shipping_method' );
|
||||
|
||||
// Add custom fields to a specific selected shipping method
|
||||
add_action( 'woocommerce_after_shipping_rate', 'carrier_custom_fields', 20, 2 );
|
||||
function carrier_custom_fields( $method, $index )
|
||||
{
|
||||
if
|
||||
( ! is_checkout()) return; // Only on checkout page
|
||||
|
||||
$customer_carrier_method = 'carrieracct';
|
||||
|
||||
if
|
||||
( $method->id != $customer_carrier_method ) return; // Only display for "local_pickup"
|
||||
|
||||
$chosen_method_id = WC()->session->chosen_shipping_methods[ $index ];
|
||||
|
||||
// If the chosen shipping method is 'legacy_local_pickup' we display
|
||||
if
|
||||
($chosen_method_id == $customer_carrier_method ):
|
||||
|
||||
echo '<div class="custom-carrier" style="width:400px;">';
|
||||
|
||||
woocommerce_form_field( 'carrier_name' , array(
|
||||
'type' => 'select',
|
||||
'class' => array('form-row-first carrier-name'),
|
||||
'required' => true,
|
||||
'options' => array(
|
||||
'' => __('Select a carrier', carrieracct),
|
||||
'UPS' => __('UPS', carrieracct),
|
||||
'FedEx' => __('FedEx', carrieracct),
|
||||
'DHL' => __('DHL', carrieracct),
|
||||
)
|
||||
), WC()->checkout->get_value( 'carrier_name' ));
|
||||
|
||||
woocommerce_form_field( 'carrier_number' , array(
|
||||
'type' => 'text',
|
||||
'class' => array('form-row-last carrier-number'),
|
||||
'required' => true,
|
||||
'placeholder' => 'Carrier Number',
|
||||
), WC()->checkout->get_value( 'carrier_number' ));
|
||||
|
||||
echo '</div>';
|
||||
endif;
|
||||
}
|
||||
|
||||
|
||||
// Check custom fields validation
|
||||
add_action('woocommerce_checkout_process', 'carrier_checkout_process');
|
||||
function carrier_checkout_process()
|
||||
{
|
||||
if
|
||||
( isset( $_POST['carrier_name'] ) && empty( $_POST['carrier_name'] ) )
|
||||
wc_add_notice( ( "Missing shipping carrier name." ), "error" );
|
||||
|
||||
if( isset( $_POST['carrier_number'] ) && empty( $_POST['carrier_number'] ) )
|
||||
wc_add_notice( ( "Missing shipping carrier account number." ), "error" );
|
||||
}
|
||||
|
||||
|
||||
// Save custom fields to order meta data
|
||||
add_action( 'woocommerce_checkout_update_order_meta', 'carrier_update_order_meta', 30, 1 );
|
||||
function carrier_update_order_meta( $order_id ) {
|
||||
if( !empty( $_POST['carrier_name'] ))
|
||||
update_post_meta( $order_id, '_carrier_name', sanitize_text_field( $_POST['carrier_name'] ) );
|
||||
|
||||
if( !empty( $_POST['carrier_number'] ))
|
||||
update_post_meta( $order_id, '_carrier_number', sanitize_text_field( $_POST['carrier_number'] ) );
|
||||
}
|
||||
|
||||
function carrier_display_in_admin( $order ){ ?>
|
||||
<h3><?php _e( 'Carrier Account' ); ?></h3>
|
||||
<?php
|
||||
echo '<div class=address><p>' . get_post_meta( $order->id, '_carrier_name', true ) . '</br>';
|
||||
echo '' . get_post_meta( $order->id, '_carrier_number', true ) . '</p></div>'; ?>
|
||||
<?php }
|
||||
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'carrier_display_in_admin' );
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue