first commit
This commit is contained in:
147
includes/Shipping/FlatRate.php
Normal file
147
includes/Shipping/FlatRate.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
namespace Formipay\Shipping;
|
||||
use Formipay\Traits\SingletonTrait;
|
||||
use Formipay\Shipping\Shipping;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) exit;
|
||||
|
||||
class FlatRate extends Shipping {
|
||||
|
||||
use SingletonTrait;
|
||||
|
||||
private $shipping_method = 'flat_rate';
|
||||
|
||||
protected function __construct() {
|
||||
|
||||
parent::__construct();
|
||||
|
||||
add_filter( 'formipay/product-config/tab:shipping/method', [$this, 'add_shipping_method'], 15 );
|
||||
add_filter( 'formipay/product-config/tab:shipping', [$this, 'add_shipping_settings'], 15 );
|
||||
|
||||
// Add to order details
|
||||
add_filter( 'formipay/order/order-details', [$this, 'add_shipping_to_order_details'], 99, 3 );
|
||||
|
||||
}
|
||||
|
||||
public function add_shipping_method($shipping_methods){
|
||||
|
||||
$shipping_methods['flat_rate'] = [
|
||||
'method' => __( 'Flat Rate', 'formipay' ),
|
||||
];
|
||||
|
||||
return $shipping_methods;
|
||||
|
||||
}
|
||||
|
||||
public function add_shipping_settings($fields) {
|
||||
|
||||
$flat_rate_fields = array(
|
||||
$this->shipping_method.'_group' => array(
|
||||
'type' => 'group_title',
|
||||
'label' => __( 'Flat Rate Setup', 'formipay' ),
|
||||
'dependency' => array(
|
||||
array(
|
||||
'key' => 'product_type',
|
||||
'value' => 'physical',
|
||||
'section' => 'general'
|
||||
),
|
||||
array(
|
||||
'key' => 'shipping_method',
|
||||
'value' => 'flat_rate'
|
||||
)
|
||||
),
|
||||
'dependencies' => '&&',
|
||||
'group' => 'started'
|
||||
),
|
||||
$this->shipping_method.'_type' => array(
|
||||
'type' => 'select',
|
||||
'label' => __( 'Type', 'formipay' ),
|
||||
'options' => array(
|
||||
'fixed' => __( 'Fixed', 'formipay' ),
|
||||
'percentage' => __( 'Percentage', 'formipay' )
|
||||
),
|
||||
'value' => 'fixed',
|
||||
'dependency' => array(
|
||||
array(
|
||||
'key' => 'product_type',
|
||||
'value' => 'physical',
|
||||
'section' => 'general'
|
||||
),
|
||||
array(
|
||||
'key' => 'shipping_method',
|
||||
'value' => 'flat_rate'
|
||||
)
|
||||
),
|
||||
'dependencies' => '&&',
|
||||
),
|
||||
$this->shipping_method.'_amount' => array(
|
||||
'type' => 'number',
|
||||
'label' => __( 'Amount', 'formipay' ),
|
||||
'value' => '10',
|
||||
'dependency' => array(
|
||||
array(
|
||||
'key' => 'product_type',
|
||||
'value' => 'physical',
|
||||
'section' => 'general'
|
||||
),
|
||||
array(
|
||||
'key' => 'shipping_method',
|
||||
'value' => 'flat_rate'
|
||||
)
|
||||
),
|
||||
'dependencies' => '&&',
|
||||
),
|
||||
$this->shipping_method.'_label' => array(
|
||||
'type' => 'text',
|
||||
'label' => __( 'Label', 'formipay' ),
|
||||
'description' => __( 'This will be shown in Order Review and Order Details', 'formipay' ),
|
||||
'value' => __( 'Shipping Fee', 'formipay' ),
|
||||
'dependency' => array(
|
||||
array(
|
||||
'key' => 'product_type',
|
||||
'value' => 'physical',
|
||||
'section' => 'general'
|
||||
),
|
||||
array(
|
||||
'key' => 'shipping_method',
|
||||
'value' => 'flat_rate'
|
||||
)
|
||||
),
|
||||
'dependencies' => '&&',
|
||||
'group' => 'ended'
|
||||
),
|
||||
);
|
||||
|
||||
foreach($flat_rate_fields as $key => $value){
|
||||
$fields[$key] = $value;
|
||||
}
|
||||
|
||||
return $fields;
|
||||
|
||||
}
|
||||
|
||||
public function add_shipping_to_order_details( $details, $form_id, $order_data ) {
|
||||
|
||||
if( formipay_get_post_meta($form_id, 'product_type') == 'physical' && formipay_get_post_meta($form_id, 'shipping_method')){
|
||||
|
||||
$amount = floatval( formipay_price_format( formipay_get_post_meta( $form_id, 'flat_rate_amount' ) ) );
|
||||
|
||||
if( formipay_get_post_meta($form_id, 'flat_rate_type') == 'percentage' ) {
|
||||
$price = floatval( formipay_get_post_meta($form_id, 'product_price') );
|
||||
$calculate = $price * $amount / 100;
|
||||
$amount = floatval($calculate);
|
||||
}
|
||||
|
||||
$details[] = [
|
||||
'item' => formipay_get_post_meta($form_id, 'flat_rate_label'),
|
||||
'amount' => $amount,
|
||||
'subtotal' => $amount
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
return $details;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
160
includes/Shipping/Shipping.php
Normal file
160
includes/Shipping/Shipping.php
Normal file
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
namespace Formipay\Shipping;
|
||||
use Formipay\Traits\SingletonTrait;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) exit;
|
||||
|
||||
abstract class Shipping {
|
||||
|
||||
use SingletonTrait;
|
||||
|
||||
abstract public function add_shipping_method($shipping_methods);
|
||||
abstract public function add_shipping_settings($fields);
|
||||
abstract public function add_shipping_to_order_details($details, $form_id, $order_data);
|
||||
|
||||
protected function __construct() {
|
||||
|
||||
add_filter( 'formipay/global-settings', [$this, 'add_setting_shipping_menu'], 15 );
|
||||
add_filter( 'formipay/product-config', [$this, 'add_form_shipping_menu'], 75 );
|
||||
|
||||
}
|
||||
|
||||
public function add_setting_shipping_menu($fields){
|
||||
|
||||
$shipping_settings = [];
|
||||
|
||||
$shipping_settings = apply_filters( 'formipay/global-settings/tab:shipping', $shipping_settings );
|
||||
|
||||
if(!empty($shipping_settings)){
|
||||
$fields['shipping'] = array(
|
||||
'name' => __('Shipping', 'formipay'),
|
||||
'fields' => $shipping_settings
|
||||
);
|
||||
}
|
||||
|
||||
return $fields;
|
||||
|
||||
}
|
||||
|
||||
public function add_form_shipping_menu($fields) {
|
||||
|
||||
$shipping_methods = apply_filters( 'formipay/product-settings/tab:shipping/method', [
|
||||
'free_shipping' => [
|
||||
'method' => __( 'Free Shipping', 'formipay' )
|
||||
]
|
||||
] );
|
||||
|
||||
$shipping_options = [];
|
||||
$shipping_fields = [];
|
||||
|
||||
foreach($shipping_methods as $id => $shipping){
|
||||
// $id = $shipping['id'];
|
||||
$label = $shipping['method'];
|
||||
if(isset($shipping['courier'])){
|
||||
$label .= ' - '.$shipping['courier'];
|
||||
if(isset($shipping['service'])){
|
||||
$label .= ' - '.$shipping['service'];
|
||||
}
|
||||
}
|
||||
$shipping_options[$id] = $label;
|
||||
}
|
||||
|
||||
$shipping_fields = [
|
||||
'shipping_notice' => array(
|
||||
'type' => 'notification_message',
|
||||
'image' => FORMIPAY_URL . 'admin/assets/img/logistics.png',
|
||||
'description' => __( '
|
||||
<h1>No Shipping Method Available</h1>
|
||||
<p>Shipping methods only for physical product type. If you insist to use shipping method, change your product type first</p>
|
||||
', 'formipay' ),
|
||||
'dependency' => array(
|
||||
'key' => 'product_type',
|
||||
'value' => 'digital',
|
||||
'section' => 'general'
|
||||
),
|
||||
),
|
||||
'shipping_method' => array(
|
||||
'type' => 'radio',
|
||||
'label' => esc_html__('Shipping Methods', 'formipay'),
|
||||
'options' => $shipping_options,
|
||||
'dependency' => array(
|
||||
'key' => 'product_type',
|
||||
'value' => 'physical',
|
||||
'section' => 'general'
|
||||
),
|
||||
)
|
||||
];
|
||||
|
||||
$free_shipping_fields = array(
|
||||
'free_shipping_group' => array(
|
||||
'type' => 'group_title',
|
||||
'label' => __( 'Free Shipping Setup', 'formipay' ),
|
||||
'description' => __( 'Will not add any shipping fee to the order', 'formipay' ),
|
||||
'dependency' => array(
|
||||
array(
|
||||
'key' => 'product_type',
|
||||
'value' => 'physical',
|
||||
'section' => 'general'
|
||||
),
|
||||
array(
|
||||
'key' => 'shipping_method',
|
||||
'value' => 'free_shipping'
|
||||
)
|
||||
),
|
||||
'dependencies' => '&&',
|
||||
'group' => 'started'
|
||||
),
|
||||
'free_shipping_label' => array(
|
||||
'type' => 'text',
|
||||
'label' => __( 'Label', 'formipay' ),
|
||||
'value' => __( 'Free Shipping', 'formipay' ),
|
||||
'dependency' => array(
|
||||
array(
|
||||
'key' => 'product_type',
|
||||
'value' => 'physical',
|
||||
'section' => 'general'
|
||||
),
|
||||
array(
|
||||
'key' => 'shipping_method',
|
||||
'value' => 'free_shipping'
|
||||
)
|
||||
),
|
||||
'dependencies' => '&&',
|
||||
),
|
||||
'free_shipping_add_to_order_review' => array(
|
||||
'type' => 'checkbox',
|
||||
'label' => __( 'Show in Order Review', 'formipay' ),
|
||||
'dependency' => array(
|
||||
array(
|
||||
'key' => 'product_type',
|
||||
'value' => 'physical',
|
||||
'section' => 'general'
|
||||
),
|
||||
array(
|
||||
'key' => 'shipping_method',
|
||||
'value' => 'free_shipping'
|
||||
)
|
||||
),
|
||||
'dependencies' => '&&',
|
||||
'group' => 'ended'
|
||||
),
|
||||
);
|
||||
|
||||
foreach($free_shipping_fields as $key => $value) {
|
||||
$shipping_fields[$key] = $value;
|
||||
}
|
||||
|
||||
$shipping_fields = apply_filters( 'formipay/product-settings/tab:shipping', $shipping_fields );
|
||||
|
||||
if(!empty($shipping_fields)){
|
||||
$fields['formipay_product_settings']['shipping'] = array(
|
||||
'name' => __( 'Shipping', 'formipay' ),
|
||||
'fields' => $shipping_fields
|
||||
);
|
||||
}
|
||||
|
||||
return $fields;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user