id = 'advanced_shipping_rules_for_woocommerce'; $this->instance_id = absint( $instance_id ); // Support more istances $this->method_title = __( 'Advanced Shipping Rules For WooCommerce', 'advanced-shipping-rules-for-woocommerce' ); $this->method_description = __( 'Calculates shipping costs based on weight or item count.', 'advanced-shipping-rules-for-woocommerce' ); $this->enabled = 'yes'; $this->supports = array( 'shipping-zones', 'instance-settings', // Allows multiple configurable instances ); $this->init(); // Loads the custom title set by the user $this->title = sanitize_text_field( $this->get_option( 'title', __( 'Advanced Shipping Rules For WooCommerce', 'advanced-shipping-rules-for-woocommerce' ) ) ); } /** * Initializes the shipping method settings and hooks. * * - Loads the WooCommerce settings API. * - Calls `init_form_fields()` to define settings fields. * - Calls `init_settings()` to load previously saved settings. * - Hooks into WooCommerce to save settings in the admin panel. * * @return void */ private function init(): void { // Load the settings API $this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings $this->init_settings(); // This is part of the settings API. Loads settings you previously init. // Save settings in admin if you have any defined add_action( 'woocommerce_update_options_shipping_' . $this->instance_id, array( $this, 'process_admin_options' ) ); } /** * Initializes the form fields for the shipping method settings. * * - Checks if WooCommerce taxes are enabled. * - Defines the base form fields, including the shipping method title. * - Adds a 'tax_status' field if WooCommerce taxes are enabled. * * @return void */ public function init_form_fields(): void { // Check if WooCommerce taxes are enabled $tax_enabled = get_option( 'woocommerce_calc_taxes' ) === 'yes'; // Define the form fields $this->instance_form_fields = array( 'title' => array( 'title' => __( 'Method Title', 'advanced-shipping-rules-for-woocommerce' ), 'type' => 'text', 'description' => __( 'This controls the title which the user sees during checkout.', 'advanced-shipping-rules-for-woocommerce' ), 'default' => __( 'Advanced Shipping Rules For WooCommerce', 'advanced-shipping-rules-for-woocommerce' ), 'desc_tip' => true, ), ); // Add the 'tax_status' field only if WooCommerce taxes are enabled if ( $tax_enabled ) { $this->instance_form_fields['tax_status'] = array( 'title' => __( 'Tax Status', 'advanced-shipping-rules-for-woocommerce' ), 'type' => 'select', 'description' => __( 'Determine whether shipping is taxable.', 'advanced-shipping-rules-for-woocommerce' ), 'default' => 'taxable', 'desc_tip' => true, 'options' => array( 'taxable' => __( 'Taxable', 'advanced-shipping-rules-for-woocommerce' ), 'none' => __( 'None', 'advanced-shipping-rules-for-woocommerce' ), ), ); } } /** * Outputs the admin options form for the shipping method. * * - Generates the settings HTML for the defined form fields. * - Displays a custom section for managing shipping groups. * - Retrieves and sanitizes selected shipping group IDs from the database. * - Loads and displays the selected shipping groups. * - Provides a button to add new shipping groups (initially disabled). * * @return void */ public function admin_options(): void { ?>
|
instance_id, array() ); // Recupera i gruppi associati
$selected_group_ids = is_array( $selected_group_ids ) ? array_map( 'absint', $selected_group_ids ) : array();
if ( ! empty( $selected_group_ids ) ) {
$groups = get_posts(
array(
'post_type' => 'asrfwoo_group',
'numberposts' => -1,
'orderby' => 'post__in',
'include' => $selected_group_ids,
)
);
$has_legacy_groups = false;
foreach ( $groups as $g ) {
if ( get_post_meta( absint( $g->ID ), '_group_cost', true ) === '' ) {
$has_legacy_groups = true;
break;
}
}
if ( $has_legacy_groups ) {
echo '
' . esc_html__( 'One or more rule groups are missing a Shipping Cost. Enter the cost for each group and save to complete the upgrade.', 'advanced-shipping-rules-for-woocommerce' ) . ' |