Here is my custom ajax request callback.
I use some data with wp_remote_post and getting json results about my own woocommerce payment gateway installments.
/**
* Check avaliblity for installments via WordPress Ajax
*
* #return void
*/
function check_installment() {
if ( isset($_REQUEST) ) {
$action = data_get($_REQUEST, 'action');
if($action == 'check_installment')
{
$cart_data = WC()->session->get('cart_totals');
$binNumber = data_get($_REQUEST, 'bin');
if(!$binNumber)
{
return;
}
$_initGateway = new Woo_Ipara_Gateway();
$_initGateway = $_initGateway->checkInstallment($binNumber);
$data = [
'cardFamilyName' => data_get($_initGateway, 'cardFamilyName'),
'supportedInstallments' => data_get($_initGateway, 'supportedInstallments')
];
echo json_encode(getInstallmentComissions(data_get($_initGateway, 'cardFamilyName')));
}
}
die();
}
add_action( 'wp_ajax_check_installment', 'check_installment');
add_action( 'wp_ajax_nopriv_check_installment', 'check_installment');
Right now, payment provider, have different comissions for spesific credit card. So it means, i want to change order total after this request, when user's selected installment value.
I also found some filter, about calculated total woocommerce_calculated_total, but how to trigger this, after ajax request and user, selected installment choice?
add_filter( 'woocommerce_calculated_total', 'custom_calculated_total', 10, 2 );
function custom_calculated_total( $total, $cart ){
// some magic.
}
Any helps ? Thanks.
First of all, filter method was wrong, what i wanted to try woocommerce_calculated_total.
It must be, woocommerce_checkout_order_processed
Other issue is, WC()->cart->add_fee( "text", $fee, false, '' ); not really work correctly.
You should use, new WC_Order_Item_Fee() class in your action directly.
Here is the some part of code in my case ;
add_action('woocommerce_checkout_order_processed', 'addFeesBeforePayment', 10, 3);
function addFeesBeforePayment( $order_id, $posted_data, $order ) {
// some logic about bin verification and remote fetch about installment comissions.
$cartTotal = WC()->cart->cart_contents_total;
$newCartTotal = $cartTotal + ( $cartTotal * $defaultComission / 100 );
$installmentFee = $cartTotal * ($defaultComission / 100 );
$item_fee = new WC_Order_Item_Fee();
$item_fee->set_name( 'Kredi Kartı Komisyonu ('.$defaultInstallment.' Taksit)' ); // Generic fee name
$item_fee->set_amount( $installmentFee ); // Fee amount
$item_fee->set_tax_class( '' ); // default for ''
$item_fee->set_tax_status( 'none' ); // or 'none'
$item_fee->set_total( $installmentFee ); // Fee amount
$order->add_item( $item_fee );
$order->calculate_totals();
$order->add_order_note( 'Bu sipariş için ödeme '. $defaultInstallment . ' taksit seçeneği seçilerek oluşturuldu. Toplam taksit komisyonu, sipariş tutarına ek olarak ' . $installmentFee. ' TL karşılığında eklendi.' );
}
Happy coding.
Related
I have created a product on WooCommerce, and added two options on product detail page using the hook woocommerce_before_add_to_cart_button. Now when customers add product to cart from product detail page they have two options their. They can choose one option from these two options.
Then I have stored the user selected value in cart meta using the woocommerce hook woocommerce_add_cart_item_data.
I am using the code from this answer: Save product custom field radio button value in cart and display it on Cart page
This is my code:
// single Product Page options
add_action("woocommerce_before_add_to_cart_button", "options_on_single_product");
function options_on_single_product(){
$dp_product_id = get_the_ID();
$product_url = get_permalink($dp_product_id);
?>
<input type="radio" name="custom_options" checked="checked" value="option1"> option1<br />
<input type="radio" name="custom_options" value="option2"> option2
<?php
}
//Store the custom field
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_data_with_add_to_cart', 10, 2 );
function save_custom_data_with_add_to_cart( $cart_item_meta, $product_id ) {
global $woocommerce;
$cart_item_meta['custom_options'] = $_POST['custom_options'];
return $cart_item_meta;
}
And this is what I have tried:
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);
function add_custom_price( $cart_obj ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach ( $cart_obj->get_cart() as $key => $value ) {
$product_id = $value['product_id'];
$custom_options = $value['custom_options'];
$coupon_code = $value['coupon_code'];
if($custom_options == 'option2')
{
if($coupon_code !='')
{
global $woocommerce;
if ( WC()->cart->has_discount( $coupon_code ) ) return;
(WC()->cart->add_discount( $coupon_code ))
//code for second discount
}
else{
$percentage = get_post_meta( $product_id , 'percentage', true );
//print_r($value);
$old_price = $value['data']->regular_price;
$new_price = ($percentage / 100) * $old_price;
$value['data']->set_price( $new_price );
}
}
}
}
Now what I am trying to get with that last snippet is:
If Option1 is selected by the customer then woocommerce regular process is run.
If Option2 is selected then firstly coupon code applied to cart (if code entered by the customer) and then the price is divide by some percentage (stored in product meta) is applied afterward.
But it’s not working as expected because the changed product price is maid before and coupon discount is applied after on this changed price.
What I would like is that the coupon discount will be applied first on the product regular price and then after change this price with my custom product discount.
Is this possible? How can I achieve that?
Thanks.
This is not really possible … Why? … Because (the logic):
You have the product price
Then the coupon discount is applied to that price (afterwards)
==> if you change the product price, the coupon is will be applied to that changed price
What you can do instead:
You don't change product price
if entered the coupon is applied and …
If "option2" product is added to cart:
Apply a custom discount (a negative fee) based on the product price added after using WC_cart add_fee() method…
For this last case you will have to fine tune your additional discount.
If the coupon has not been applied or it's removed there is no additional discount.
Your custom function will be hooked in woocommerce_cart_calculate_fees action hook instead:
add_action( 'woocommerce_cart_calculate_fees', 'option2_additional_discount', 10, 1 );
function option2_additional_discount( $cart_obj ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$discount = 0;
$applied_coupons = $cart_obj->get_applied_coupons();
foreach ( $cart_obj->get_cart() as $item_values ) {
if( 'option2' == $item_values['custom_options'] && !empty($applied_coupons) ){
$product_id = $item_values['product_id'];
$percentage = get_post_meta( $product_id , 'percentage', true );
$quantity = $item_values['quantity'];
$product_reg_price = $item_values['data']->regular_price;
$line_total = $item_values['line_total'];
$line_subtotal = $item_values['line_subtotal'];
$percentage = 90;
## ----- CALCULATIONS (To Fine tune) ----- ##
$item_discounted_price = ($percentage / 100) * $product_reg_price * $item_values['quantity'];
// Or Besed on line item subtotal
$discounted_price = ($percentage / 100) * $line_subtotal;
$discount += $product_reg_price - $item_discounted_price;
}
}
if($discount != 0)
$cart_obj->add_fee( __( 'Option2 discount', 'woocommerce' ) , - $discount );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.
Adding a negative fee using the WC_Cart->add_fee() method wasn't working for me. When i check the WC Cart class, it even states you are not allowed to use a negative ammount.
See the docs.
I did the following:
create a placeholder coupon with a 'secure' code, e.g. custom_discount_fjgndfl28. Set a discount ammount of 0, so when somebody (somehow) uses this coupon outside your program the discount is still 0.
Use filter woocommerce_get_shop_coupon_data, and set all the coupon data you want for that coupon/session.
Hook into woocommerce_before_calculate_totals and set your custom coupon to the cart.
At this point the Cart should calculate everything correctly. And when it becomes an order, it also has the correct discount ammount.
Note: the coupon code is also used as a label in some templates. Use filter woocommerce_cart_totals_coupon_label to change it.
Example functions:
/**
* NOTE: All the hooks and filters below have to be called from your own
* does_it_need_custom_discount() function. I used the 'wp' hook for mine.
* Do not copy/paste this to your functions.php.
**/
add_filter('woocommerce_get_shop_coupon_data', 'addVirtualCoupon', 10, 2);
function addVirtualCoupon($unknown_param, $curr_coupon_code) {
if($curr_coupon_code == 'custom_discount_fjgndfl28') {
// possible types are: 'fixed_cart', 'percent', 'fixed_product' or 'percent_product.
$discount_type = 'fixed_cart';
// how you calculate the ammount and where you get the data from is totally up to you.
$amount = $get_or_calculate_the_coupon_ammount;
if(!$discount_type || !$amount) return false;
$coupon = array(
'id' => 9999999999 . rand(2,9),
'amount' => $amount,
'individual_use' => false,
'product_ids' => array(),
'exclude_product_ids' => array(),
'usage_limit' => '',
'usage_limit_per_user' => '',
'limit_usage_to_x_items' => '',
'usage_count' => '',
'expiry_date' => '',
'apply_before_tax' => 'yes',
'free_shipping' => false,
'product_categories' => array(),
'exclude_product_categories' => array(),
'exclude_sale_items' => false,
'minimum_amount' => '',
'maximum_amount' => '',
'customer_email' => '',
'discount_type' => $discount_type,
);
return $coupon;
}
}
add_action('woocommerce_before_calculate_totals', 'applyFakeCoupons');
function applyFakeCoupons() {
global $woocommerce;
// $woocommerce->cart->remove_coupons(); remove existing coupons if needed.
$woocommerce->cart->applied_coupons[] = $this->coupon_code;
}
add_filter( 'woocommerce_cart_totals_coupon_label', 'cart_totals_coupon_label', 100, 2 );
function cart_totals_coupon_label($label, $coupon) {
if($coupon) {
$code = $coupon->get_code();
if($code == 'custom_discount_fjgndfl28') {
return 'Your custom coupon label';
}
}
return $label;
}
Please Note: i copied these functions out of a class that handles much more, it's only to help you get going.
From here :
How to add custom stock status to products in WooCommerce 4+
Woocommerce - Check if product was created less than 60 days ago
Target > Check the product's age, if it has reached x days make the stock 'expired' (costume).
Here the code to make stock custom
//PART1
// Add new stock status options
function filter_woocommerce_product_stock_status_options( $status ) {
// Add new statuses
$status['pre_order'] = __( 'Pre order', 'woocommerce' );
$status['expired'] = __( 'Expired', 'woocommerce' );
return $status;
}
add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );
// Availability text
function filter_woocommerce_get_availability_text( $availability, $product ) {
// Get stock status
//i try change this to : switch ($product->get_stock_status){
//also deleted
switch( $product->get_stock_status() ) {
case 'pre_order':
$availability = __( 'Pre order', 'woocommerce' );
break;
case 'expired':
$availability = __( 'Expired', 'woocommerce' );
break;
}
return $availability;
}
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );
// Availability CSS class
function filter_woocommerce_get_availability_class( $class, $product ) {
// Get stock status
//i try change this to : switch ($product->get_stock_status){
//also deleted
switch( $product->get_stock_status() ) {
case 'pre_order':
$class = 'pre-order';
break;
case 'expired':
$class = 'expired';
break;
}
return $class;
}
add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 );
//PART2
// Admin stock html
function filter_woocommerce_admin_stock_html( $stock_html, $product ) {
// Simple
if ( $product->is_type( 'simple' ) ) {
// Get stock status
//i try change this to : $product->get_stock_status;
//also deleted
$product_stock_status = $product->get_stock_status();
// Variable
} elseif ( $product->is_type( 'variable' ) ) {
foreach( $product->get_visible_children() as $variation_id ) {
// Get product
$variation = wc_get_product( $variation_id );
// Get stock status
//i try change this to : $product->get_stock_status;
//also deleted
$product_stock_status = $variation->get_stock_status();
/*
Currently the status of the last variant in the loop will be displayed.
So from here you need to add your own logic, depending on what you expect from your custom stock status.
By default, for the existing statuses. The status displayed on the admin products list table for variable products is determined as:
- Product should be in stock if a child is in stock.
- Product should be out of stock if all children are out of stock.
- Product should be on backorder if all children are on backorder.
- Product should be on backorder if at least one child is on backorder and the rest are out of stock.
*/
}
}
// Stock status
switch( $product_stock_status ) {
case 'pre_order':
$stock_html = '<mark class="pre-order" style="background:transparent none;color:#33ccff;font-weight:700;line-height:1;">' . __( 'Pre order', 'woocommerce' ) . '</mark>';
break;
case 'expired':
$stock_html = '<mark class="expired" style="background:transparent none;color:#cc33ff;font-weight:700;line-height:1;">' . __( 'Contact us', 'woocommerce' ) . '</mark>';
break;
}
return $stock_html;
}
add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );
So, i has edited the code and here's the complete code.
add_filter( 'woocommerce_product_get_stock_status', 'conditional_product_status', 10, 2 );
add_filter( 'woocommerce_product_variation_get_stock_status', 'conditional_product_status', 10, 2 );
function conditional_product_status( $product_stock_status, $product ){
global $product;
$package = $product->get_attribute( 'pa_package' );
switch($package){
case 'Silver':
$var = 14*24*60*60;
break;
case 'Gold':
$var = 90*24*60*60;
break;
case 'Platinum':
$var = 180*24*60*60;
break;
default:
$var = 1*24*60*60;
break;
}
// Get the date for the product published and current date
$datetime_created = $product->get_date_created(); // Get product created datetime
$timestamp_created = $datetime_created->getTimestamp(); // product created timestamp
$datetime_now = new WC_DateTime(); // Get now datetime (from Woocommerce datetime object)
$timestamp_now = $datetime_now->getTimestamp(); // Get now timestamp
$time_delta = $timestamp_now - $timestamp_created; // Difference in seconds
if($product->is_type('simple')&& $time_delta > $var ){
$product_stock_status = 'expired';
}elseif($product->is_type('variable')&& $time_delta > $var ){
$product_stock_status = 'expired';
}
return $product_stock_status;
}
On the back end, it has changed the stock state and like the way I want it to.
I enabled wp_debug and didn't see any error messages.
The problem is that every time I editing a product (example: change the price, or change the attribute value), and then i update it, that to be always loading and never finishes.
I'm stuck here and don't know what to do.
Can someone direct me to the right path?
Thank you
I have created a new payment gateway plugin that allows customers to pay later. I will be using it with a woocommerce POS system.
I cannot get the 'customer invoice' email to send automatically - I believe it has something to do with the "pending" status of the order (which it must have in order to allow the customer to pay later). I have analysed many other answers but none have worked for me.
I have gently modified the code from this link (only - changed names and set up as plugin): https://github.com/creativelittledots/woocommerce-pay-later.
It uses a series of functions to trigger the email that I don't fully understand - but it doesn't seem to work: The default status is set to "on- hold" to allow the emails to trigger then the order is set to "pending" to allow the customer to pay for the order.
Here are the relevant snippets:
add_filter( 'woocommerce_default_order_status', array($this, 'default_order_status') );
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
add_filter( 'woocommerce_email_format_string_find', array($this, 'order_status_format_string_find') );
add_filter( 'woocommerce_email_format_string_replace', array($this, 'order_status_format_string_replace'), 10, 2 );
add_action( 'woocommerce_order_status_pending', array($this, 'send_pending_order_emails') );
add_filter( 'woocommerce_valid_order_statuses_for_payment', array($this, 'valid_order_statuses_for_payment' ), 10, 2 );
add_action( 'wp', array($this, 'change_order_to_pending_on_order_received'), 8 );`
public function valid_order_statuses_for_payment($statuses, $order) {
if( $order->is_pay_later ) {
$statuses[] = 'on-hold';
}
return $statuses;
}
/**
* Change the default order status to on-hold so that pending order emails can be triggered
*/
public function default_order_status($default) {
if( ! is_admin() && WC()->session->set( 'chosen_payment_method') == $this->id ) {
$default = 'on-hold';
}
return $default;
}
/**
* Allow Order status to be accessible from emails
*/
public function order_status_format_string_find( $find ) {
$find['order-status'] = '{order_status}';
return $find;
}
/**
* Replace Order status in emails
*/
public function order_status_format_string_replace( $replace, $email ) {
if( $email->object ) {
$replace['order-status'] = wc_get_order_status_name( $email->object->get_status() );
}
return $replace;
}
/**
* Trigger pending order emails and invoice email
*/
public function send_pending_order_emails( $order_id ) {
$emails = new WC_Emails();
$order = wc_get_order( $order_id );
$emails->customer_invoice( $order_id );
$emails->emails['WC_Email_New_Order']->trigger( $order_id );
$order->set_payment_method( $this );
}
/**
* WC Shop As Customer support on Order Received, because the default status is on hold we need to change these orders to pending
*/
public function change_order_to_pending_on_order_received() {
if( class_exists('WC_Shop_As_Customer') && ! empty( $_GET['order_on_behalf'] ) && ! empty( $_GET['key'] ) && ! empty( $_GET['send_invoice'] ) ) {
global $wp;
if ( ! isset( $wp->query_vars['order-received'] ) )
return;
// Bail if we're not shopping-as - don't display the special interface.
if ( ! WC_Shop_As_Customer::get_original_user() )
return;
$order_id = $wp->query_vars['order-received'];
if ( ! empty( $order_id ) ) {
$order = new WC_Order( absint( $order_id) );
$order->update_status( 'pending' );
}
unset( $_GET['send_invoice'] );
}
}
I also read that woocommerce_order_status_pending may not trigger emails so I have tried adding this code (no successes):
add_filter( 'woocommerce_email_actions', 'filter_woocommerce_email_actions' );
function filter_woocommerce_email_actions( $actions ){
$actions[] = 'woocommerce_order_status_pending';
return $actions;
}
I suspect either:
The code to put the order 'on hold' then trigger then back to 'pending' is not set up correctly
woocommerce_order_status_pending hook does not trigger emails
I have a little work around to trigger from the "woocommerce_thankyou" hook in functions.php but I believe that this should be done in the gateway (plus I don't want to rely on woocommerce_thankyou - this email should be based on order creation).
Thanks in advance!!!!!!
I need to disable Woocommerce COD option and make it unchecked when total price exceeded certain amount.
I tried this code, but does nothing!
add_filter('woocommerce_available_payment_gateways', 'unsetting_payment_gateway', 10, 1);
function unsetting_payment_gateway( $available_gateways ) {
global $woocommerce;
$shipping_cost = WC()->cart->get_cart_shipping_total();
$amount = $woocommerce->cart->cart_contents_total + $woocommerce->cart->tax_total + $shipping_cost;
$max = 999.9 * WCPBC()->customer->exchange_rate;
if($amount >= $max){ ?>
<script type="text/javascript">
jQuery(document).ready(function ($) {
$("#payment_method_cod").disabled = true;
$("#payment_method_cod").checked = false;
});
</script>
<?php
add_action( 'woocommerce_review_order_before_payment', 'COD_exceed_amount_before_paying_notice' );
}
return $available_gateways;
}
function COD_exceed_amount_before_paying_notice() {
wc_print_notice( __( 'COD amount exceeded!', 'woocommerce' ), 'notice' );
}
add_filter( 'woocommerce_available_payment_gateways' , 'hide_payment_gateway', 20, 1);
/**
* remove cod gateway if cart total > 100
* #param $gateways
* #return mixed
*/
function hide_payment_gateway( $gateways ){
//change whatever amount you want
if( WC()->cart->subtotal < 699 ){
// then unset the 'cod' key (cod is the unique id of COD Gateway)
unset( $gateways['cod'] );
add_action( 'woocommerce_review_order_before_payment', 'COD_exceed_amount_before_paying_notice' );
}
return $gateways;
}
function COD_exceed_amount_before_paying_notice() {
wc_print_notice( __( 'COD option not available on orders below 699.00', 'woocommerce' ), 'notice' );
}
First you need to deregister the woocommerce/assets/js/frontend/checkout.js
Then include checkout.js in your theme assets folder and register it again after theme script files
then your code will work as you expect.
for deregister use below code,
wp_deregister_script( $handle );
again register by,
wp_enqueue_script(parameters);
You should change your jquery code. Can you please try below code and check your code again. Because that is working for me.
<script type="text/javascript">
jQuery( document ).ready(function($) {
$("#payment_method_cod").prop('checked', false);
$("#payment_method_cod").attr('disabled', true);
});
</script>
I want to know if there is a action hook that can check if the subscription is successfully renewed in woocommerce ? BTW I am using woocommerce subscription plugin. I have created a functionality that records the date of the subscription order and add it to a CSV file, the function is working perfectly for the first purchase I mean when the user purchase a subscription it is recorded successfully in the CSV because I am firing up the function on woocommerce_thankyou action hook, The only issue I am facing is that I can't seem to find a hook which can execute this function on successful subscription renewal. I tried to use woocommerce_subscription_renewal_payment_complete action hook but it didn't worked below is the function that I have created.
/**
* Add subscriptions to csv.
*/
add_action( 'woocommerce_subscription_renewal_payment_complete', 'add_subs_to_csv' );
add_action( 'woocommerce_thankyou', 'add_subs_to_csv' );
function add_subs_to_csv( $order_id ) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $key => $value ) {
$meta_values = $value->get_data();
foreach ( $meta_values as $meta_key => $meta_value ) {
if ( $meta_key == 'product_id' && $meta_value == 875 ) {
$paid_date = explode( " ", get_post_meta( $order_id, '_paid_date', true ) );
$subs_paid_date = date( 'd F, Y', strtotime( $paid_date[0] ) );
wc_add_order_item_meta( $key, 'Delivery Date', $subs_paid_date );
}
}
}
}
Could the wcs_renewal_order_created hook be what you're looking for? The docs say:
WooCommerce Subscriptions stores all details of each subscription
renewal in a standard WooCommerce order, only with a special meta flag
linking it to a subscription.
These orders are always created through the wcs_create_renewal_order()
function, regardless of whether they are created for a scheduled
renewal event, manually via the WooCommerce > Edit Subscription
administration screen, or via the Subscriptions endpoints for the
WooCommerce REST API. Because of this, it’s possible to add, remove or
update the value of anything on that renewal order using this filter.
For example, this can be used to add a discount to specific renewal
orders, like the 12th order each year. It could also be used to add
one-time fee for a certain renewal order, like a special annual extra
fee on a monthly subscription.
So the above hook should trigger after payment, you'd probably just need to check if it was completed status which you could also do in your current hooks:
/**
* After WooCommerce Subscriptions Creates Renewal Order
*
* #param WC_Order Object $order
* #param Integer|WC_Subscription Object $subscription
*
* #return WC_Order $order
*/
function add_subs_to_csv( $order, $subscription ) {
if( 'completed' === $order->get_status() ) {
$items = $order->get_items();
foreach ( $items as $key => $value ) {
$meta_values = $value->get_data();
foreach ( $meta_values as $meta_key => $meta_value ) {
if ( $meta_key == 'product_id' && $meta_value == 875 ) {
$paid_date = explode( " ", get_post_meta( $order_id, '_paid_date', true ) );
$subs_paid_date = date( 'd F, Y', strtotime( $paid_date[0] ) );
wc_add_order_item_meta( $key, 'Delivery Date', $subs_paid_date );
}
}
}
}
return $order
}
add_filter( 'wcs_renewal_order_created', 'add_subs_to_csv', 10, 2 );
I had a issue at Subscription renewal and I got it fixed with below code:
/*
* FIXED : Membership got PAUSED everytime at automatic subscription renewal
*/
function change_membership_status_active( $subscription , $order ) {
global $wpdb;
if( 'completed' === $order->get_status() ) {
$membership = $wpdb->get_row( "SELECT * FROM wp_postmeta WHERE meta_key = '_subscription_id' AND meta_value = $subscription->ID" );
$mem_id = $membership->post_id;
$status = 'wcm-active';
$update_args = array( 'ID' => $mem_id, 'post_status' => $status );
wp_update_post($update_args);
}
}
add_action( 'woocommerce_subscription_renewal_payment_complete', 'change_membership_status_active', 10, 2 );