Ich konnte erfolgreich eine Woocommerce-Gebühr aus meiner functions.php mit den folgenden hinzufügen. Insbesondere rufe ich sie innerhalb der Aktion "gform_after_submission" auf, um eine geplante Zahlung anzupassen, wenn der Nutzer seine preisbezogenen Optionen mit einem Schwerkraftformular ändert.
Hinweis: Benötigt die Bestell-ID ($ order_id) für die WooCommerce damit Sie die Gebühr zu
1 hinzufügen möchten: Richten Sie die WooCommerce Gebühr Objekt
$feename = 'Option Adjustment';
$feeamount = 40;
//The following sets up the fee object in a format accepted by woocommerce
$fee = array('name' => $feename, 'amount' => $feeamount, 'taxable' => false, 'tax_class' => '');
2. Rufen Sie die Funktion fügt hinzu, dass die Gebühr (# 3)
blb_add_fee($fee, $order_id);
3. die Funktion, die Gebühr (Angepasst von http://woocommerce.wp-a2z.org/oik_api/wc_abstract_orderadd_fee/)
ergänzt
function blb_add_fee($fee, $order_id) {
$item_id = wc_add_order_item($order_id, array(
'order_item_name' => $fee['name'],
'order_item_type' => 'fee'
));
if (! $item_id) {
return false;
}
if ($fee['taxable']) {
wc_add_order_item_meta($item_id, '_tax_class', $fee['tax_class']);
} else {
wc_add_order_item_meta($item_id, '_tax_class', '0');
}
wc_add_order_item_meta($item_id, '_line_total', wc_format_decimal($fee['amount']));
wc_add_order_item_meta($item_id, '_line_tax', wc_format_decimal($fee['tax']));
// Save tax data - Since 2.2
$tax_data = array_map('wc_format_decimal', $fee['tax_data']);
wc_add_order_item_meta($item_id, '_line_tax_data', array('total' => $tax_data));
do_action('woocommerce_order_add_fee', $order_id, $item_id, $fee);
//Remove the following line (blb_calculate_totals) if you dont need to recalculate your totals
blb_calculate_totals($and_taxes = false, $order_id);
return $item_id;
}
4. Schließlich sollten Sie Ihre Summen neu zu berechnen (die so genannte oben „blb_calculate_totals“ verwendet und angepasst von http://woocommerce.wp-a2z.org/oik_api/wc_abstract_ordercalculate_totals/)
function blb_calculate_totals($and_taxes = false, $order_id) {
$cart_subtotal = 0;
$cart_total = 0;
$fee_total = 0;
$cart_subtotal_tax = 0;
$cart_total_tax = 0;
/*
if ($and_taxes && wc_tax_enabled()) {
$this->calculate_taxes();
}
*/
// line items
$order = new WC_Order($order_id);
foreach ($order->get_items() as $item) {
$cart_subtotal += wc_format_decimal(isset($item['line_subtotal']) ? $item['line_subtotal'] : 0);
$cart_total += wc_format_decimal(isset($item['line_total']) ? $item['line_total'] : 0);
$cart_subtotal_tax += wc_format_decimal(isset($item['line_subtotal_tax']) ? $item['line_subtotal_tax'] : 0);
$cart_total_tax += wc_format_decimal(isset($item['line_tax']) ? $item['line_tax'] : 0);
}
//$this->calculate_shipping();
foreach ($order->get_fees() as $item) {
$fee_total += $item['line_total'];
}
$order->set_total($cart_subtotal - $cart_total, 'cart_discount');
$order->set_total($cart_subtotal_tax - $cart_total_tax, 'cart_discount_tax');
$grand_total = round($cart_total + $fee_total + $order->get_total_shipping() + $order->get_cart_tax() + $order->get_shipping_tax(), wc_get_price_decimals());
$order->set_total($grand_total, 'total');
return $grand_total;
}
, dass die Arbeit für mich nicht, weil ich die Gebühr müssen in ein Stück passen von Code, den ich schreibe. Ich habe etwas herausgefunden. Ich werde unten teilen. – MJJ