File Manager
<?php
/**
* Declares a shipping method for carrier bpost shm
*/
class bpost_shm extends WC_Shipping_Flat_Rate {
/**
* Constructor.
*
* @param int $instance_id Instance ID.
*/
public $has_pickup;
public $options;
public $service_level;
public $pickupbehaviour;
public $supports;
public $method_description;
public $method_title;
public function __construct( $instance_id = 0){
global $Bpost;
$this->instance_id = absint( $instance_id );
$this->id = 'shipping_Bpost_68';
$this->title = preg_replace('/\b(shm|sml)\b/i', '', 'bpost shm') . 'Flat Rate'; //name for end user
$this->method_title = preg_replace('/\b(shm|sml)\b/i', '', 'bpost shm') . 'Flat Rate'; //name for admin
$this->method_description = preg_replace('/\b(shm|sml)\b/i', '', 'bpost shm') ; // description for admin
$this->has_pickup = true;
$this->supports = array(
'shipping-zones',
'instance-settings',
'instance-settings-modal',
);
// Default to returning the string passed by param
$translate = function ($str){ return $str;};
// how it's possible that someone declares this method before there's an instance of the plugin is a mistery to solve later
// possible they copied the code and did their own rendition of the thing?
if(!$Bpost && class_exists('BpostWoo')) {
$Bpost = BpostWoo::instance();
}
# If we have an instance of Bpost, then use that translate function
if($Bpost) {
$translate = function ($str) { global $Bpost; return $Bpost->translate($str); };
}
$this->init();
$this->options = get_option($this->id . '_' . $this->instance_id . '_settings');
/*Max amount field*/
$this->instance_form_fields['max_amount'] = array(
'title' => 'Maximum order amount',
'type' => 'text',
'class' => 'Bpost-shipping-level',
'default' => 0
);
/*Service Level*/
$this->instance_form_fields['service_level'] = array(
'title' => $translate('service_level'),
'type' => 'select',
'class' => 'Bpost-service-level',
'default' => 0, 'options' => array( "" => "-" , '301' => 'Bpack 24/7 & Bpack@bpost' , '302' => 'Bpack 24h Pro' , '303' => 'bpack World Business' , '304' => 'bpack 24h business' , '306' => 'bpack World Express Pro' , '307' => 'Bpack 24/7 Int & Bpack@Bpost Int') );
$this->service_level = $this->get_option( 'service_level' , '');
/*Extraoptions */
$this->instance_form_fields['pickupbehaviour'] = array(
'title' => $translate('pickupbehaviour'),
'type' => 'select',
'class' => 'Bpost-pickupbehaviour',
'default' => 0, 'options' => array('0'=> "Optionnel", '1' => "Obligatoire", '2' =>"Désactivé") );
$this->pickupbehaviour = $this->get_option( 'pickupbehaviour' , '');
/** checkboxfields **/
/** Exclude classes **/
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
public function validate_excludeclasses_field( $key , $value ) {
if ( $key === 'excludeclasses' ) {
return empty($value) ? '' : implode( ',' , $value );
}
return $value;
}
public function get_admin_options_html()
{
if ( $this->instance_id ) {
$settings_html = $this->generate_settings_html( $this->get_instance_form_fields(), false );
} else {
$settings_html = $this->generate_settings_html( $this->get_form_fields(), false );
}
$excludeclassesoptions = array();
if ( is_plugin_active('woocommerce-advanced-shipping/woocommerce-advanced-shipping.php') ) {
$shipping = new \WC_Shipping();
$opt_exclude_classes = explode( ',', $this->get_instance_option('excludeclasses') );
foreach ( $shipping->get_shipping_classes() as $shipping_class ) {
array_push( $excludeclassesoptions, array(
'id' => $shipping_class->term_id,
'name' => $shipping_class->name,
'selected' => in_array( $shipping_class->term_id , $opt_exclude_classes ) ? 'checked' : ''
));
}
}
return '<table class="form-table">' . $settings_html . '</table><script>
var optionsid = "#woocommerce_' . $this->id . '_extraoptions";
jQuery("#woocommerce_'. $this->id . '_max_amount").on("input", function () {
const inputField = jQuery(this);
const value = inputField.val();
// Remove any existing error messages
jQuery(`#error-message`).remove();
jQuery(`#range-error-message`).remove();
// Check if the input contains only numbers
if (/^\d*$/.test(value)) {
inputField.val(value); // Allow only numeric input
} else {
// Remove non-numeric characters
const cleanedValue = value.replace(/\D/g, ``);
inputField.val(cleanedValue);
if (jQuery(`#error-message`).length === 0) {
inputField.after(`<div id="error-message" style="color: red;">Only numbers are allowed</div>`);
}
}
});
function checkSelectedValue() {
var selectedValue = jQuery("#woocommerce_shipping_Bpost_68_service_level").val();
var pickupBehaviourField = jQuery(`select[name="woocommerce_shipping_Bpost_68_pickupbehaviour"]`);
if (!pickupBehaviourField.length) {
pickupBehaviourField = jQuery(`select[name="woocommerce_shipping_Bpost_71_pickupbehaviour"]`);
}
var pickupBehaviourLabel = jQuery(`label[for="woocommerce_shipping_Bpost_68_pickupbehaviour"]`);
if (!pickupBehaviourLabel.length) {
pickupBehaviourLabel = jQuery(`label[for="woocommerce_shipping_Bpost_71_pickupbehaviour"]`);
}
// if (selectedValue == "301") {
// pickupBehaviourField.show();
// pickupBehaviourLabel.show();
// } else {
pickupBehaviourField.hide();
pickupBehaviourLabel.hide();
// }
}
// Attach the change event to the dropdown
jQuery("#woocommerce_shipping_Bpost_68_service_level").change(function() {
checkSelectedValue();
});
checkSelectedValue();
// setTimeout( function(){
// console.log("Helloo from '.$this->id.'");
//
// jQuery(".Bpost-extra-option-values").parent().parent().parent().hide();
// Bpost_extraoption_values();
//
// jQuery(optionsid).change(Bpost_extraoption_values);
//
// setExcludeClasses();
// }, 0);
function Bpost_extraoption_values(){
var selectedoption = jQuery(optionsid).val();
jQuery(".Bpost-extra-option-values").parent().parent().parent().hide();
jQuery("#woocommerce_' . $this->id . '_extraoptions" + selectedoption).parent().parent().parent().show();
}
function setExcludeClasses() {
var excludeoptions =' . wp_json_encode($excludeclassesoptions) . ';
var select = jQuery("#woocommerce_shipping_Bpost_68_excludeclasses");
var content = select.parent();
select.remove();
for ( var x=0; x< excludeoptions.length; ++x ) {
content.append(\'<span class="Bpost-ib Bpost-exclude-class"> <input type="checkbox" name="woocommerce_shipping_Bpost_68_excludeclasses[]" value="\' + excludeoptions[x].id + \'" \' + excludeoptions[x].selected + \' /> \' + excludeoptions[x].name + \'</span>\');
}
}
</script>';
}
}
File Manager Version 1.0, Coded By Lucas
Email: hehe@yahoo.com