<?php
/*
Plugin Name: Custom Shipping Method
Plugin URI:
Description: WooCommerce Custom Shipping Method
Version: 1.0
Author: Your Name
*/
if (!defined(‘ABSPATH’)) {
exit;
}
function custom_shipping_method_init() {
if (!class_exists(‘Custom_Shipping_Method’)) {
class Custom_Shipping_Method extends WC_Shipping_Method {
/**
* Weight unit
*/
private $weight_unit;
/**
* Constructor for shipping class
*/
public function __construct($instance_id = 0) {
$this->id = ‘custom_shipping_method’;
$this->instance_id = absint($instance_id);
$this->title = ‘사용자 정의 배송’;
$this->method_title = ‘사용자 정의 배송 방법’;
$this->method_description = ‘배송 방법 설정을 관리합니다’;
$this->supports = array(
‘shipping-zones’,
‘instance-settings’,
‘instance-settings-modal’
);
$this->weight_unit = get_option(‘woocommerce_weight_unit’);
$this->init();
}
/**
* Initialize shipping method
*/
private function init() {
$this->init_form_fields();
$this->init_settings();
$this->enabled = $this->get_option(‘enabled’);
$this->title = $this->get_option(‘title’);
add_action(‘woocommerce_update_options_shipping_’ . $this->id,
array($this, ‘process_admin_options’));
add_action(‘woocommerce_update_options_shipping_’ . $this->id,
array($this, ‘clear_transients’));
}
/**
* Initialize form fields
*/
public function init_form_fields() {
$this->instance_form_fields = array(
‘enabled’ => array(
‘title’ => ‘활성화’,
‘type’ => ‘checkbox’,
‘label’ => ‘이 배송 방법 활성화’,
‘default’ => ‘yes’
),
‘title’ => array(
‘title’ => ‘배송방법명’,
‘type’ => ‘text’,
‘description’ => ‘고객에게 표시될 배송 방법 이름’,
‘default’ => $this->method_title,
‘desc_tip’ => true
),
‘cost’ => array(
‘title’ => ‘기본 배송비’,
‘type’ => ‘number’,
‘default’ => ‘0’,
‘description’ => ‘기본 배송비를 입력하세요’,
‘desc_tip’ => true,
‘custom_attributes’ => array(
‘step’ => ‘100’,
‘min’ => ‘0’
)
),
‘cost_per_weight’ => array(
‘title’ => $this->weight_unit.’당 추가 배송비’,
‘type’ => ‘number’,
‘default’ => ‘0’,
‘description’ => ‘무게당 추가되는 배송비’,
‘desc_tip’ => true,
‘custom_attributes’ => array(
‘step’ => ‘100’,
‘min’ => ‘0’
)
),
‘min_weight’ => array(
‘title’ => ‘최소 배송 무게 (‘.$this->weight_unit.’)’,
‘type’ => ‘number’,
‘default’ => ‘0’,
‘description’ => ‘이 무게 이상일 때 배송 가능’,
‘desc_tip’ => true,
‘custom_attributes’ => array(
‘step’ => ‘0.1’,
‘min’ => ‘0’
)
),
‘max_weight’ => array(
‘title’ => ‘최대 배송 무게 (‘.$this->weight_unit.’)’,
‘type’ => ‘number’,
‘default’ => ‘0’,
‘description’ => ‘0으로 설정시 무제한’,
‘desc_tip’ => true,
‘custom_attributes’ => array(
‘step’ => ‘0.1’,
‘min’ => ‘0’
)
),
‘excluded_countries’ => array(
‘title’ => ‘제외 국가’,
‘type’ => ‘multiselect’,
‘class’ => ‘wc-enhanced-select’,
‘options’ => WC()->countries->get_shipping_countries()
)
);
}
/**
* Calculate shipping cost
*/
public function calculate_shipping($package = array()) {
$cost = (float)$this->get_option(‘cost’, 0);
$has_shipping_items = false;
$weight = 0;
// 배송 가능한 상품이 있는지 확인하고 무게 계산
foreach ($package[‘contents’] as $item) {
if ($item[‘data’]->needs_shipping()) {
$has_shipping_items = true;
$product_weight = (float)$item[‘data’]->get_weight();
if (!empty($product_weight)) {
$weight += $product_weight * $item[‘quantity’];
}
}
}
// 배송 가능 여부 확인
if (!$has_shipping_items) {
return;
}
// 무게가 있는 경우에만 무게 기반 배송비 계산
$min_weight = (float)$this->get_option(‘min_weight’, 0);
$max_weight = (float)$this->get_option(‘max_weight’, 0);
if (($min_weight > 0 && $weight < $min_weight) ||
($max_weight > 0 && $weight > $max_weight)) {
return;
}
$cost_per_weight = (float)$this->get_option(‘cost_per_weight’, 0);
$cost += $weight * $cost_per_weight;
// 배송비가 0 이상일 때만 배송 방법 추가
if ($cost >= 0) {
$rate = array(
‘id’ => $this->id . $this->instance_id,
‘label’ => $this->title,
‘cost’ => $cost,
‘calc_tax’ => ‘per_order’
);
$this->add_rate($rate);
}
}
/**
* Check if shipping method is available
*/
public function is_available($package) {
if ($this->enabled === ‘no’) {
return false;
}
// 배송지 검사
if (isset($package[‘destination’])) {
$shipping_country = $package[‘destination’][‘country’];
$shipping_state = $package[‘destination’][‘state’];
$shipping_postcode = $package[‘destination’][‘postcode’];
// 제외된 국가 확인
$excluded_countries = $this->get_option(‘excluded_countries’, array());
if (!empty($excluded_countries) &&
in_array($shipping_country, $excluded_countries)) {
return false;
}
// 배송지 주소가 비어있는 경우
if (empty($shipping_country)) {
return false;
}
}
return apply_filters(
‘woocommerce_shipping_’ . $this->id . ‘_is_available’,
true,
$package,
$this
);
}
/**
* Clear transients
*/
public function clear_transients() {
wc_delete_shipping_transients();
}
/**
* Validate settings fields
*/
public function validate_settings_fields($form_fields = array()) {
if (!wp_verify_nonce($_POST[‘_wpnonce’], ‘woocommerce-settings’)) {
return false;
}
return $form_fields;
}
}
}
}
/**
* Add shipping method to WooCommerce
*/
function add_custom_shipping_method($methods) {
$methods[‘custom_shipping_method’] = ‘Custom_Shipping_Method’;
return $methods;
}
// 후크 등록
add_action(‘woocommerce_shipping_init’, ‘custom_shipping_method_init’);
add_filter(‘woocommerce_shipping_methods’, ‘add_custom_shipping_method’);
// 관리자 스타일 추가
add_action(‘admin_enqueue_scripts’, function() {
wp_enqueue_style(‘custom-shipping-admin-style’,
plugins_url(‘/assets/css/admin.css’, __FILE__));
});