'DHL','fedex'=>'FedEx','acs'=>'ACS','boxnow'=>'BOX NOW','geniki'=>'Γενική Ταχυδρομική','speedex'=>'SPEEDEX','elta'=>'ΕΛΤΑ Courier','couriercenter'=>'Courier Center','skroutz'=>'Skroutz Last Mile'];
$brand=$brands[strtolower((string)($quote['carrier']??''))]??'';
return $brand&&stripos($name,$brand)===false?trim($brand.' '.$name):$name;
}
function courierapp_connect_transit_label($days): string {
if(!is_numeric($days)||!is_finite((float)$days)||(float)$days<1||(float)$days>60||(float)$days!==floor((float)$days))return '';
$n=(int)$days;$greek=str_starts_with(determine_locale(),'el');
return $greek?'Εκτιμώμενη μεταφορά: '.$n.($n===1?' εργάσιμη ημέρα':' εργάσιμες ημέρες'):'Estimated transit: '.$n.($n===1?' business day':' business days');
}
function courierapp_connect_brand_markup(): string {
if (!courierapp_compat_get_option('courierapp_connect_checkout_credit', false)) return '';
return '
'.esc_html(str_starts_with(determine_locale(),'el')?'Επίλεξε υπηρεσία αποστολής':'Choose your shipping service').' ';
}
function courierapp_connect_carrier_assets(): array {
$saved=courierapp_compat_get_option('courierapp_connect_carrier_logos',[]);if(!is_array($saved))return [];
$assets=[];foreach(courierapp_connect_valid_logo_ids($saved) as $carrier=>$id)$assets[$carrier]=courierapp_connect_logo_attachment_url($id);return $assets;
}
function courierapp_connect_group_label(string $group): string {
$el=str_starts_with(determine_locale(),'el');
return match($group){'merchant_contract'=>$el?'Η συνεργαζόμενη μεταφορική του καταστήματος':'Store carrier agreement','premium_express'=>$el?'CourierAPP · Premium Express':'CourierAPP · Premium Express',default=>$el?'CourierAPP · Αποστολές εξωτερικού':'CourierAPP · International shipping'};
}
// Add a separate table heading without replacing the merchant/theme template or radio controls.
add_action('woocommerce_before_template_part',function($name,$path,$located,$args){
if($name!=='cart/cart-shipping.php'||!courierapp_connect_can_quote())return;
foreach($args['available_methods']??[] as $rate){
if($rate->get_method_id()==='courierapp_connect'){
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- courierapp_connect_brand_markup escapes its URL and label and returns fixed local markup.
echo '| '.wp_kses_post(courierapp_connect_brand_markup()).' |
';break;
}
}
},10,4);
// Keep the shipping cell across both columns of the native totals table.
// The original template still owns addresses, taxes, radio inputs and package handling.
$courierapp_connect_shipping_buffers=[];
add_action('woocommerce_before_template_part',function($name,$path,$located,$args)use(&$courierapp_connect_shipping_buffers){
if($name!=='cart/cart-shipping.php'||!courierapp_connect_can_quote())return;
foreach($args['available_methods']??[]as $r)if($r->get_method_id()==='courierapp_connect'){ob_start();$courierapp_connect_shipping_buffers[]=ob_get_level();break;}
},20,4);
add_action('woocommerce_after_template_part',function($name)use(&$courierapp_connect_shipping_buffers){
if($name!=='cart/cart-shipping.php'||!$courierapp_connect_shipping_buffers||end($courierapp_connect_shipping_buffers)!==ob_get_level())return;
array_pop($courierapp_connect_shipping_buffers);$html=ob_get_clean();
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Trusted WooCommerce template HTML; only the fixed column layout is changed.
echo preg_replace('/]*>.*?<\\/th>\\s* | ]*)>/s',' | ',$html,1);
},10,1);
add_action('woocommerce_after_shipping_rate',function($rate){
if($rate->get_method_id()==='courierapp_connect')echo '';
});
add_filter('woocommerce_cart_shipping_method_full_label',function($label,$rate){
if($rate->get_method_id()!=='courierapp_connect')return $label;
$meta=$rate->get_meta_data();$time=courierapp_connect_transit_label($meta['_courierapp_eta_days']??null);$assets=courierapp_connect_carrier_assets();$carrier=$meta['_courierapp_carrier']??'';
$image=isset($assets[$carrier])?' ':'';
if((float)$rate->get_cost()===0.0)$label.=' · '.esc_html(str_starts_with(determine_locale(),'el')?'Δωρεάν μεταφορικά':'Free shipping');
return $image.$label.''.esc_html(courierapp_connect_group_label($meta['_courierapp_service_group']??'international')).''.($time?''.esc_html($time).'':'');
},10,2);
// Public presentation metadata only; account identifiers and credentials never enter Store API.
add_action('init',function(){
if(!function_exists('woocommerce_store_api_register_endpoint_data'))return;
woocommerce_store_api_register_endpoint_data([
'endpoint'=>\Automattic\WooCommerce\StoreApi\Schemas\V1\CartSchema::IDENTIFIER,'namespace'=>'courierapp',
'data_callback'=>function(){
$result=[];if(WC()->shipping())foreach(WC()->shipping()->get_packages() as $package)foreach($package['rates']??[] as $rate){
if($rate->get_method_id()!=='courierapp_connect')continue;$m=$rate->get_meta_data();
$result[$rate->get_id()]=['carrier'=>$m['_courierapp_carrier']??'','group'=>courierapp_connect_group_label($m['_courierapp_service_group']??'international'),'packing'=>courierapp_connect_packing_projection($m['_courierapp_checkout']??null,$m['_courierapp_request']??null)];
}return ['rates'=>(object)$result];
},
'schema_callback'=>fn()=>['rates'=>['description'=>'Shipping presentation metadata','type'=>'object','readonly'=>true,'additionalProperties'=>['type'=>'object','properties'=>['carrier'=>['type'=>'string'],'group'=>['type'=>'string'],'packing'=>['type'=>['object','null']]]]]],
'schema_type'=>ARRAY_A,
]);
},20);
add_filter('render_block',function($content,$block){
$name=$block['blockName']??'';
if(!in_array($name,['woocommerce/cart-order-summary-shipping-block','woocommerce/checkout-shipping-methods-block'],true))return $content;
return $content.'';
},10,2);
// Native Store API field is rendered by WooCommerce Blocks beneath the service name.
add_filter('woocommerce_shipping_method_add_rate',function($rate,$args,$method){
if($method->id==='courierapp_connect'&&method_exists($rate,'set_delivery_time'))$rate->set_delivery_time(courierapp_connect_transit_label($args['meta_data']['_courierapp_eta_days']??null));
return $rate;
},10,3);
add_action('wp_enqueue_scripts',function(){
if(!is_cart()&&!is_checkout())return;
wp_enqueue_style('courierapp-connect-shipping',plugins_url('assets/shipping.css',__FILE__),[],COURIERAPP_CONNECT_VERSION);
if(wp_script_is('wc-blocks-data-store','registered')){
wp_enqueue_script('courierapp-connect-shipping-blocks',plugins_url('assets/shipping-blocks.js',__FILE__),['wp-element','wp-data','wc-blocks-data-store','courierapp-packing-view'],COURIERAPP_CONNECT_VERSION.'-ux5',true);
wp_add_inline_script('courierapp-connect-shipping-blocks','window.courierappShippingBrand='.wp_json_encode(['showCredit'=>(bool)courierapp_compat_get_option('courierapp_connect_checkout_credit',false),'logo'=>plugins_url('assets/courierapp-logo.png',__FILE__),'carriers'=>courierapp_connect_carrier_assets(),'prompt'=>str_starts_with(determine_locale(),'el')?'Επίλεξε υπηρεσία αποστολής':'Choose your shipping service','error'=>str_starts_with(determine_locale(),'el')?'Η επιλογή δεν αποθηκεύτηκε. Δοκίμασε ξανά.':'The selection was not saved. Please try again.']).';','before');
}
},30);
|