我使用了函数$extra_charges
和woo_add_extra_fee()
中使用的变量wl8OrderPlacedTriggerSomething()
。由于某种原因,它总是打印为零(即使在满足if条件后)。
<?php
$extra_charges=0;
public function woo_add_extra_fee() {
global $woocommerce;
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
$current_gateway = WC()->session->chosen_payment_method;
if($current_gateway=='cod'){
global $extra_charges;
$current_gateways_detail = $available_gateways[$current_gateway];
$disable_cod_adv = $current_gateways_detail->settings['disable_cod_adv'];
if($disable_cod_adv && $disable_cod_adv=='yes'){return $totals;}
$current_gateway_id = $current_gateways_detail->id;
$current_gateway_title = $current_gateways_detail->title;
$extra_charges_id = 'woocommerce_'.$current_gateway_id.'_extra_charges';
$extra_charges_type = $extra_charges_id.'_type';
$extra_charge_min_amount = (float)$current_gateways_detail->settings['extra_charge_min_amount'];
$extra_charges = (float)$current_gateways_detail->settings['extra_charges'];
$extra_charges_type = $current_gateways_detail->settings['extra_charges_type'];
$roundup_type = $current_gateways_detail->settings['roundup_type'];
$extra_charges_msg = $current_gateways_detail->settings['extra_charges_msg'];
if(!$extra_charges_msg){$extra_charges_msg = 'COD Charges';}
//get cart total
$total = $woocommerce->cart->subtotal;
if($extra_charges){
if($extra_charges_type=="percentage"){
$total = $total + round(($total*$extra_charges)/100,2);
}else{
$total = $total + $extra_charges;
}
if($roundup_type>0)
{
$extra_add = $roundup_type -($total%$roundup_type);
$total = $total+$extra_add;
$extra_charges = $extra_charges+$extra_add;
}
//$this->current_extra_charge_min_amount = $extra_charge_min_amount;
//$this->current_gateway_title = $current_gateway_title;
//$this->current_gateway_extra_charges = $extra_charges;
//$this->current_gateway_extra_charges_type_value = $extra_charges_type;
$extra_fee_option_taxable = 0;
//if cart total less or equal than $min_order, add extra fee
//$extra_fee_option_label = sprintf(__('%s Extra Charges <small>for purchase less than %s</small>','askoracle'),$this->current_gateway_title,woocommerce_price($this->current_extra_charge_min_amount));
$extra_fee_option_label = $extra_charges_msg;
//$extra_fee_option_label = get_woocommerce_currency();
$extra_charges = 0;
if (get_woocommerce_currency() == "SAR")
$extra_charges = 20;
if (get_woocommerce_currency() == "QAR")
$extra_charges = 20;
if (get_woocommerce_currency() == "AED")
$extra_charges = 0;
if (get_woocommerce_currency() == "EGP")
$extra_charges = 90;
if (get_woocommerce_currency() == "JOD")
$extra_charges = 4;
if (get_woocommerce_currency() == "BHD")
$extra_charges = 2;
if (get_woocommerce_currency() == "KWD")
$extra_charges = 2;
if (get_woocommerce_currency() == "OMR")
$extra_charges = 2;
if (get_woocommerce_currency() == "LBP")
$extra_charges = 7500;
if($extra_charge_min_amount>=$total || empty($extra_charge_min_amount)){
$woocommerce->cart->add_fee($extra_fee_option_label, $extra_charges, $extra_fee_option_taxable );
}
}
}
}
}
$hook_to = 'woocommerce_thankyou';
$what_to_hook = 'wl8OrderPlacedTriggerSomething';
$prioriy = 111;
$num_of_arg = 1;
add_action($hook_to, $what_to_hook, $prioriy, $num_of_arg);
function wl8OrderPlacedTriggerSomething($order_id){
global $extra_charges;
//do something...
update_post_meta( $order_id, "cod_fee", $extra_charges);
}