本文整理汇总了PHP中WC_Order::add_fee方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Order::add_fee方法的具体用法?PHP WC_Order::add_fee怎么用?PHP WC_Order::add_fee使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Order
的用法示例。
在下文中一共展示了WC_Order::add_fee方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: set_fee
/**
* Create or update an order fee.
*
* @param WC_Order $order Order data.
* @param array $fee Item data.
* @param string $action 'create' to add fee or 'update' to update it.
* @throws WC_REST_Exception Invalid data, server error.
*/
protected function set_fee($order, $fee, $action)
{
if ('create' === $action) {
// Fee name is required.
if (empty($fee['name'])) {
throw new WC_REST_Exception('woocommerce_rest_invalid_fee_item', __('Fee name is required.', 'woocommerce'), 400);
}
$fee_data = new stdClass();
$fee_data->id = sanitize_title($fee['name']);
$fee_data->name = $fee['name'];
$fee_data->amount = isset($fee['total']) ? floatval($fee['total']) : 0;
$fee_data->taxable = false;
$fee_data->tax = 0;
$fee_data->tax_data = array();
$fee_data->tax_class = '';
// If taxable, tax class and total are required.
if (isset($fee['tax_status']) && 'taxable' === $fee['tax_status']) {
if (!isset($fee['tax_class'])) {
throw new WC_REST_Exception('woocommerce_rest_invalid_fee_item', __('Fee tax class is required when fee is taxable.', 'woocommerce'), 400);
}
$fee_data->taxable = true;
$fee_data->tax_class = $fee['tax_class'];
if (isset($fee['total_tax'])) {
$fee_data->tax = isset($fee['total_tax']) ? wc_format_refund_total($fee['total_tax']) : 0;
}
}
$fee_id = $order->add_fee($fee_data);
if (!$fee_id) {
throw new WC_REST_Exception('woocommerce_rest_cannot_create_fee', __('Cannot create fee, try again.', 'woocommerce'), 500);
}
} else {
$fee_args = array();
if (isset($fee['name'])) {
$fee_args['name'] = $fee['name'];
}
if (isset($fee['tax_class'])) {
$fee_args['tax_class'] = $fee['tax_class'];
}
if (isset($fee['total'])) {
$fee_args['line_total'] = floatval($fee['total']);
}
if (isset($fee['total_tax'])) {
$fee_args['line_tax'] = floatval($fee['total_tax']);
}
$fee_id = $order->update_fee($fee['id'], $fee_args);
if (!$fee_id) {
throw new WC_REST_Exception('woocommerce_rest_cannot_update_fee', __('Cannot update fee, try again.', 'woocommerce'), 500);
}
}
}
示例2: foreach
/**
* Successful Payment!
**/
function successful_request($posted)
{
$order = new WC_Order((int) $posted["wooorderid"]);
$var = "";
if (strlen($this->md5key) > 0) {
foreach ($posted as $key => $value) {
if ($key != "hash") {
$var .= $value;
}
}
$genstamp = md5($var . $this->md5key);
if ($genstamp != $posted["hash"]) {
echo "MD5 error";
error_log('MD5 check failed for ePay callback with order_id:' . $posted["wooorderid"]);
status_header(500);
exit;
}
}
if ($order->has_status('pending')) {
// Payment completed
$order->add_order_note(__('Callback completed', 'woocommerce-gateway-epay-dk'));
if ($this->settings["addfeetoorder"] == "yes") {
$order_fee = new stdClass();
$order_fee->id = 'epay_fee';
$order_fee->name = __('Fee', 'woocommerce-gateway-epay-dk');
$order_fee->amount = isset($posted['txnfee']) ? floatval($posted['txnfee'] / 100) : 0;
$order_fee->taxable = false;
$order_fee->tax = 0;
$order_fee->tax_data = array();
$order->add_fee($order_fee);
$order->set_total($order->order_total + floatval($posted['txnfee'] / 100));
}
$order->payment_complete();
update_post_meta((int) $posted["wooorderid"], 'Transaction ID', $posted["txnid"]);
update_post_meta((int) $posted["wooorderid"], 'Card no', $posted["cardno"]);
if (isset($posted["subscriptionid"])) {
update_post_meta((int) $posted["wooorderid"], 'Subscription ID', $posted["subscriptionid"]);
}
}
echo "OK";
status_header(200);
exit;
}
示例3: set_fee
/**
* Create or update an order fee.
*
* @since 2.5.0
* @param \WC_Order $order
* @param array $fee item data
* @param string $action 'create' to add fee or 'update' to update it
* @throws WC_CLI_Exception invalid data, server error
*/
protected function set_fee($order, $fee, $action)
{
if ('create' === $action) {
// fee title is required
if (!isset($fee['title'])) {
throw new WC_CLI_Exception('woocommerce_invalid_fee_item', __('Fee title is required', 'woocommerce'));
}
$order_fee = new stdClass();
$order_fee->id = sanitize_title($fee['title']);
$order_fee->name = $fee['title'];
$order_fee->amount = isset($fee['total']) ? floatval($fee['total']) : 0;
$order_fee->taxable = false;
$order_fee->tax = 0;
$order_fee->tax_data = array();
$order_fee->tax_class = '';
// if taxable, tax class and total are required
if (isset($fee['taxable']) && $fee['taxable']) {
if (!isset($fee['tax_class'])) {
throw new WC_CLI_Exception('woocommerce_invalid_fee_item', __('Fee tax class is required when fee is taxable', 'woocommerce'));
}
$order_fee->taxable = true;
$order_fee->tax_class = $fee['tax_class'];
if (isset($fee['total_tax'])) {
$order_fee->tax = isset($fee['total_tax']) ? wc_format_refund_total($fee['total_tax']) : 0;
}
if (isset($fee['tax_data'])) {
$order_fee->tax = wc_format_refund_total(array_sum($fee['tax_data']));
$order_fee->tax_data = array_map('wc_format_refund_total', $fee['tax_data']);
}
}
$fee_id = $order->add_fee($order_fee);
if (!$fee_id) {
throw new WC_CLI_Exception('woocommerce_cannot_create_fee', __('Cannot create fee, try again', 'woocommerce'));
}
} else {
$fee_args = array();
if (isset($fee['title'])) {
$fee_args['name'] = $fee['title'];
}
if (isset($fee['tax_class'])) {
$fee_args['tax_class'] = $fee['tax_class'];
}
if (isset($fee['total'])) {
$fee_args['line_total'] = floatval($fee['total']);
}
if (isset($fee['total_tax'])) {
$fee_args['line_tax'] = floatval($fee['total_tax']);
}
$fee_id = $order->update_fee($fee['id'], $fee_args);
if (!$fee_id) {
throw new WC_CLI_Exception('woocommerce_cannot_update_fee', __('Cannot update fee, try again', 'woocommerce'));
}
}
}
示例4: add_order_item
/**
* Add an item to the provided order
*
* @since 3.0.0
* @param \WC_Order $order
* @param array $item Parsed item data from CSV
* @param string $type Line item type
* @return int|false ID of the inserted order item, false on failure
*/
private function add_order_item(WC_Order $order, $item, $type)
{
$result = false;
switch ($type) {
case 'line_item':
$product = $this->get_product_for_item($item);
$args = $this->prepare_product_args($item);
$result = $order->add_product($product, $args['qty'], $args);
if (!$result) {
wc_csv_import_suite()->log(sprintf(__('> > Warning: cannot add order item "%s".', 'woocommerce-csv-import-suite'), esc_html($identifier)));
}
break;
case 'shipping':
$args = array('order_item_name' => $item['method_title'], 'order_item_type' => 'shipping');
// we're using wc_add_order_item instead of $order->add_shipping because
// we do not want the order total to be recalculated
$result = wc_add_order_item($order->id, $args);
if (!$result) {
wc_csv_import_suite()->log(sprintf(__('> > Warning: cannot add shipping method "%s".', 'woocommerce-csv-import-suite'), esc_html($item['title'])));
}
break;
case 'tax':
$args = array('order_item_name' => $item['code'], 'order_item_type' => 'tax');
$result = wc_add_order_item($order->id, $args);
if (!$result) {
wc_csv_import_suite()->log(sprintf(__('> > Warning: cannot add tax "%s".', 'woocommerce-csv-import-suite'), esc_html($item['label'])));
}
break;
case 'coupon':
$result = $order->add_coupon($item['code'], $item['amount']);
if (!$result) {
wc_csv_import_suite()->log(sprintf(__('> > Warning: cannot add coupon "%s".', 'woocommerce-csv-import-suite'), esc_html($item['code'])));
}
break;
case 'fee':
$order_fee = new stdClass();
$order_fee->id = sanitize_title($item['title']);
$order_fee->name = $item['title'];
$order_fee->amount = isset($item['total']) ? floatval($item['total']) : 0;
$order_fee->taxable = false;
$order_fee->tax = 0;
$order_fee->tax_data = array();
$order_fee->tax_class = '';
// if taxable, tax class and total are required
if (isset($item['taxable']) && $item['taxable']) {
$order_fee->taxable = true;
$order_fee->tax_class = $item['tax_class'];
if (isset($item['total_tax'])) {
$order_fee->tax = isset($item['total_tax']) ? wc_format_refund_total($item['total_tax']) : 0;
}
if (isset($item['tax_data'])) {
$tax_data = isset($item['tax_data']['total']) ? $item['tax_data']['total'] : $item['tax_data'];
$order_fee->tax = wc_format_refund_total(array_sum($tax_data));
$order_fee->tax_data = array_map('wc_format_refund_total', $tax_data);
}
}
$result = $order->add_fee($order_fee);
if (!$result) {
wc_csv_import_suite()->log(sprintf(__('> > Warning: cannot add fee "%s".', 'woocommerce-csv-import-suite'), esc_html($item['title'])));
}
break;
}
// store original order item ID
if ($result && isset($item['order_item_id']) && $item['order_item_id'] > 0) {
wc_update_order_item_meta($result, '_original_order_item_id', $item['order_item_id']);
}
return $result;
}