本文整理汇总了PHP中WC_Order::add_coupon方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Order::add_coupon方法的具体用法?PHP WC_Order::add_coupon怎么用?PHP WC_Order::add_coupon使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Order
的用法示例。
在下文中一共展示了WC_Order::add_coupon方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: set_coupon
/**
* Create or update an order coupon.
*
* @param WC_Order $order Order data.
* @param array $coupon Item data.
* @param string $action 'create' to add coupon or 'update' to update it.
* @throws WC_REST_Exception Invalid data, server error.
*/
protected function set_coupon($order, $coupon, $action)
{
// Coupon discount must be positive float.
if (isset($coupon['discount']) && 0 > floatval($coupon['discount'])) {
throw new WC_REST_Exception('woocommerce_rest_invalid_coupon_total', __('Coupon discount must be a positive amount.', 'woocommerce'), 400);
}
if ('create' === $action) {
// Coupon code is required.
if (empty($coupon['code'])) {
throw new WC_REST_Exception('woocommerce_rest_invalid_coupon_coupon', __('Coupon code is required.', 'woocommerce'), 400);
}
$coupon_id = $order->add_coupon($coupon['code'], floatval($coupon['discount']));
if (!$coupon_id) {
throw new WC_REST_Exception('woocommerce_rest_cannot_create_order_coupon', __('Cannot create coupon, try again.', 'woocommerce'), 500);
}
} else {
$coupon_args = array();
if (isset($coupon['code'])) {
$coupon_args['code'] = $coupon['code'];
}
if (isset($coupon['discount'])) {
$coupon_args['discount_amount'] = floatval($coupon['discount']);
}
$coupon_id = $order->update_coupon($coupon['id'], $coupon_args);
if (!$coupon_id) {
throw new WC_REST_Exception('woocommerce_rest_cannot_update_order_coupon', __('Cannot update coupon, try again.', 'woocommerce'), 500);
}
}
}
示例2: array
/**
* Charge Payment
* Method ini digunakan untuk mendapatkan link halaman pembayaran Veritrans
* dengan mengirimkan JSON yang berisi data transaksi
*/
function charge_payment($order_id)
{
global $woocommerce;
$order_items = array();
$cart = $woocommerce->cart;
$order = new WC_Order($order_id);
// add discount
// WC()->cart->add_discount( 'veritrans' );
$cart->add_discount('veritrans');
$order->add_coupon('veritrans', WC()->cart->get_coupon_discount_amount('veritrans'), WC()->cart->get_coupon_discount_tax_amount('veritrans'));
$order->set_total(WC()->cart->shipping_total, 'shipping');
$order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount');
$order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax');
$order->set_total(WC()->cart->tax_total, 'tax');
$order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax');
$order->set_total(WC()->cart->total);
// $order->add_coupon('veritrans',10000);
// end of add discount
Veritrans_Config::$isProduction = $this->environment == 'production' ? true : false;
Veritrans_Config::$serverKey = Veritrans_Config::$isProduction ? $this->server_key_v2_production : $this->server_key_v2_sandbox;
Veritrans_Config::$is3ds = true;
Veritrans_Config::$isSanitized = $this->enable_sanitization == 'yes' ? true : false;
$params = array('transaction_details' => array('order_id' => $order_id, 'gross_amount' => 0), 'vtweb' => array());
$enabled_payments = array();
// if ($this->enable_credit_card == 'yes'){
// $enabled_payments[] = 'credit_card';
// }
// check enabled payment
if ($this->enable_credit_card == 'yes') {
$params['vtweb']['enabled_payments'] = 'credit_card';
}
if ($this->enable_permata_va == 'yes') {
$params['vtweb']['enabled_payments'] = 'bank_transfer';
}
// add bin filter
$bins = $this->bin_filter;
$bins = explode(',', $bins);
$params['vtweb']['credit_card_bins'] = $bins;
$customer_details = array();
$customer_details['first_name'] = $order->billing_first_name;
$customer_details['last_name'] = $order->billing_last_name;
$customer_details['email'] = $order->billing_email;
$customer_details['phone'] = $order->billing_phone;
$billing_address = array();
$billing_address['first_name'] = $order->billing_first_name;
$billing_address['last_name'] = $order->billing_last_name;
$billing_address['address'] = $order->billing_address_1;
$billing_address['city'] = $order->billing_city;
$billing_address['postal_code'] = $order->billing_postcode;
$billing_address['phone'] = $order->billing_phone;
$billing_address['country_code'] = strlen($this->convert_country_code($order->billing_country) != 3) ? 'IDN' : $this->convert_country_code($order->billing_country);
$customer_details['billing_address'] = $billing_address;
$customer_details['shipping_address'] = $billing_address;
if (isset($_POST['ship_to_different_address'])) {
$shipping_address = array();
$shipping_address['first_name'] = $order->shipping_first_name;
$shipping_address['last_name'] = $order->shipping_last_name;
$shipping_address['address'] = $order->shipping_address_1;
$shipping_address['city'] = $order->shipping_city;
$shipping_address['postal_code'] = $order->shipping_postcode;
$shipping_address['phone'] = $order->billing_phone;
$shipping_address['country_code'] = strlen($this->convert_country_code($order->shipping_country) != 3) ? 'IDN' : $this->convert_country_code($order->billing_country);
$customer_details['shipping_address'] = $shipping_address;
}
$params['customer_details'] = $customer_details;
//error_log(print_r($params,true));
$items = array();
if (sizeof($order->get_items()) > 0) {
foreach ($order->get_items() as $item) {
if ($item['qty']) {
$product = $order->get_product_from_item($item);
$veritrans_item = array();
$veritrans_item['id'] = $item['product_id'];
$veritrans_item['price'] = $order->get_item_subtotal($item, false);
$veritrans_item['quantity'] = $item['qty'];
$veritrans_item['name'] = $item['name'];
$items[] = $veritrans_item;
}
}
}
// Shipping fee
if ($order->get_total_shipping() > 0) {
$items[] = array('id' => 'shippingfee', 'price' => $order->get_total_shipping(), 'quantity' => 1, 'name' => 'Shipping Fee');
}
// Tax
if ($order->get_total_tax() > 0) {
$items[] = array('id' => 'taxfee', 'price' => $order->get_total_tax(), 'quantity' => 1, 'name' => 'Tax');
}
// Discount
if ($order->get_cart_discount() > 0) {
$items[] = array('id' => 'totaldiscount', 'price' => $order->get_cart_discount() * -1, 'quantity' => 1, 'name' => 'Total Discount');
}
// Fees
if (sizeof($order->get_fees()) > 0) {
$fees = $order->get_fees();
//.........这里部分代码省略.........
示例3: update_cart_by_woocart
public function update_cart_by_woocart($order_id, $data)
{
global $wp;
global $wpdb, $woocommerce, $pwa;
$xml = simplexml_load_string($data);
$order = new WC_Order($order_id);
$billing_address = array('first_name' => (string) $xml->ProcessedOrder->BuyerInfo->BuyerName, 'last_name' => '', 'company' => '', 'email' => (string) $xml->ProcessedOrder->BuyerInfo->BuyerEmailAddress, 'phone' => '', 'address_1' => '', 'address_2' => '', 'city' => '', 'state' => '', 'postcode' => '', 'country' => '');
$shipping_address = array('first_name' => (string) $xml->ProcessedOrder->ShippingAddress->Name, 'last_name' => '', 'company' => '', 'email' => '', 'phone' => '', 'address_1' => (string) $xml->ProcessedOrder->ShippingAddress->AddressFieldOne, 'address_2' => (string) $xml->ProcessedOrder->ShippingAddress->AddressFieldTwo, 'city' => (string) $xml->ProcessedOrder->ShippingAddress->City, 'state' => (string) $xml->ProcessedOrder->ShippingAddress->State, 'postcode' => (string) $xml->ProcessedOrder->ShippingAddress->PostalCode, 'country' => (string) $xml->ProcessedOrder->ShippingAddress->CountryCode);
$order->set_address($shipping_address, 'shipping');
add_post_meta($order_id, '_payment_method', 'pwa');
add_post_meta($order_id, '_payment_method_title', 'Pay with Amazon');
$total_amount = 0;
$subtotal_amount = 0;
$shipping_amount = 0;
$ClientRequestId = 0;
try {
foreach ($xml->ProcessedOrder->ProcessedOrderItems->ProcessedOrderItem as $item) {
// XML DATA
$ClientRequestId = (int) $item->ClientRequestId;
foreach ($item->ItemCharges->Component as $amount_type) {
$item_charge_type = (string) $amount_type->Type;
if ($item_charge_type == 'Shipping') {
$Shipping = (string) $amount_type->Charge->Amount;
}
}
$shipping_amount = $shipping_amount + $Shipping;
}
} catch (Exception $e) {
$param['message'] = 'IOPN Notifications : Caught exception : ' . $e->getMessage() . '.';
$this->generate_log($param);
}
if ($ClientRequestId == 0) {
$order->set_address($billing_address, 'billing');
}
// CART DATA
$cartdata = '';
$user_id = 0;
$prefix = $wpdb->prefix;
$carts = $wpdb->get_results("SELECT * FROM `" . $prefix . "pwa_before_cart_save` WHERE id = {$ClientRequestId} ");
foreach ($carts as $key => $value) {
$cartdata = maybe_unserialize($value->cart_data);
$user_id = $value->user_id;
}
update_post_meta($order_id, '_customer_user', $user_id);
// ENTRY
try {
foreach ($cartdata->cart_contents as $key => $value) {
$product_id = $value['product_id'];
$cart_product = get_product($product_id);
$product = array();
$product['order_item_name'] = $cart_product->get_title();
$product['order_item_type'] = 'line_item';
$order_item_id = wc_add_order_item($order_id, $product);
wc_add_order_item_meta($order_item_id, '_qty', $value['quantity']);
wc_add_order_item_meta($order_item_id, '_product_id', $product_id);
wc_add_order_item_meta($order_item_id, '_line_total', $value['line_total']);
wc_add_order_item_meta($order_item_id, '_line_subtotal', $value['line_subtotal']);
wc_add_order_item_meta($order_item_id, '_line_tax', $value['line_tax']);
wc_add_order_item_meta($order_item_id, '_line_subtotal_tax', $value['line_subtotal_tax']);
wc_add_order_item_meta($order_item_id, '_line_tax_data', maybe_serialize($value['line_tax_data']));
foreach ($value['line_tax_data']['total'] as $tax_rate_id => $tax_data) {
$tax_class = $wpdb->get_results("SELECT * FROM `" . $prefix . "woocommerce_tax_rates` WHERE tax_rate_id = {$tax_rate_id} ");
wc_add_order_item_meta($order_item_id, '_tax_class', $tax_class[0]->tax_rate_class);
}
if ($value['variation_id'] > 0) {
wc_add_order_item_meta($order_item_id, '_variation_id', $value['variation_id']);
}
foreach ($value['variation'] as $attrib_key => $attrib_value) {
$meta_key = str_replace('attribute_', '', $attrib_key);
wc_add_order_item_meta($order_item_id, $meta_key, $attrib_value);
}
$this->reduce_order_stock($product_id, $value['quantity']);
$total_amount = $total_amount + $value['line_total'] + $value['line_tax'];
$subtotal_amount = $subtotal_amount + $value['line_subtotal'];
}
} catch (Exception $e) {
$param['message'] = 'IOPN Notifications : Caught exception : ' . $e->getMessage() . '.';
$this->generate_log($param);
}
add_post_meta($order_id, '_order_total', $total_amount + $shipping_amount);
add_post_meta($order_id, '_order_shipping', $shipping_amount);
add_post_meta($order_id, '_cart_discount', $cartdata->discount_cart);
add_post_meta($order_id, '_cart_discount_tax', $cartdata->discount_cart_tax);
add_post_meta($order_id, '_order_tax', $cartdata->tax_total);
$shipitem = array();
$shipitem['order_item_name'] = (string) $xml->ProcessedOrder->ShippingServiceLevel;
$shipitem['order_item_type'] = 'shipping';
$order_shipping_id = wc_add_order_item($order_id, $shipitem);
wc_add_order_item_meta($order_shipping_id, 'method_id', str_replace(' ', '_', strtolower((string) $xml->ProcessedOrder->ShippingServiceLevel)));
wc_add_order_item_meta($order_shipping_id, 'cost', $shipping_amount);
if (!empty($cartdata->taxes)) {
foreach ($cartdata->taxes as $key => $value) {
$order->add_tax($key, $value);
}
}
if (!empty($cartdata->applied_coupons)) {
foreach ($cartdata->applied_coupons as $key => $value) {
$order->add_coupon($value, $cartdata->coupon_discount_amounts[$value], $cartdata->coupon_discount_tax_amounts[$value]);
}
}
//.........这里部分代码省略.........
示例4: set_coupon
/**
* Create or update an order coupon
*
* @since 2.2
* @param \WC_Order $order
* @param array $coupon item data
* @param string $action 'create' to add coupon or 'update' to update it
* @throws WC_API_Exception invalid data, server error
*/
private function set_coupon($order, $coupon, $action)
{
// coupon amount must be positive float
if (isset($coupon['amount']) && floatval($coupon['amount']) < 0) {
throw new WC_API_Exception('woocommerce_invalid_coupon_total', __('Coupon discount total must be a positive amount', 'woocommerce'), 400);
}
if ('create' === $action) {
// coupon code is required
if (empty($coupon['code'])) {
throw new WC_API_Exception('woocommerce_invalid_coupon_coupon', __('Coupon code is required', 'woocommerce'), 400);
}
$coupon_id = $order->add_coupon($coupon['code'], isset($coupon['amount']) ? floatval($coupon['amount']) : 0);
if (!$coupon_id) {
throw new WC_API_Exception('woocommerce_cannot_create_order_coupon', __('Cannot create coupon, try again', 'woocommerce'), 500);
}
} else {
$coupon_args = array();
if (isset($coupon['code'])) {
$coupon_args['code'] = $coupon['code'];
}
if (isset($coupon['amount'])) {
$coupon_args['discount_amount'] = floatval($coupon['amount']);
}
$coupon_id = $order->update_coupon($coupon['id'], $coupon_args);
if (!$coupon_id) {
throw new WC_API_Exception('woocommerce_cannot_update_order_coupon', __('Cannot update coupon, try again', 'woocommerce'), 500);
}
}
}
示例5: 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;
}