WordPress WooCommerce get_sign_up_fee based on available variations - wordpress

I am using some custom logics to show or hide variations in variable products using this hook woocommerce_get_children https://stackoverflow.com/a/67804835/6829420 and its working fine as per my expectations.
However, I am having wrong 'Sign Up Fee' in the variable product page because its keep getting the values from one of my hidden variations. Here below is my try and what I have done so far.
add_filter( 'woocommerce_subscriptions_product_price_string', 'subscr_product_price_string', 10, 3 );
function subscr_product_price_string( $subscription_string, $product, $include ){
if (is_admin() || (! $product->is_type( 'variable-subscription' ))) return $subscription_string;
if ( $include['sign_up_fee'] && $product->get_sign_up_fee( $product ) > 0 ) {
$available_variations = $product->get_available_variations();
echo count($available_variations);
echo $product->get_sign_up_fee();
exit;
//echo $prices = get_price_sign_up_fee($product);
exit;
}
Say for example, I have 3 variations "Blue, Yellow and Green" and I have hide "Blue" and go to variation product page.
Then If I do this,
$available_variations = $product->get_available_variations();
echo count($available_variations);
Then its showing 2 but my echo $product->get_sign_up_fee(); keep getting value from "Blue" which has the lowest value but this is wrong value.
Can someone guide me how can I get sign up values based on my available variations only.
I have already updated default From value using below code with woocommerce_variable_price_html hook and its working fine as its getting value based on available variations.
function updateFromDefaultValue($price, $product) {
$prefix = sprintf('%s: ', __('From', 'iconic'));
$min_price_regular = $product->get_variation_regular_price('min', true);
$min_price_sale = $product->get_variation_sale_price('min', true);
$max_price = $product->get_variation_price('max', true);
$min_price = $product->get_variation_price('min', true);
$price = ($min_price_sale == $min_price_regular) ?
wc_price($min_price_regular) :
'<del>' . wc_price($min_price_regular) . '</del>' . '<ins>' . wc_price($min_price_sale) . '</ins>';
return ($min_price == $max_price) ?
$price :
sprintf('%s%s', $prefix, $price);
}
add_filter('woocommerce_variable_price_html', 'updateFromDefaultValue', 10, 2);
Thanks
UPDATE
I have found a hook woocommerce_subscriptions_product_price_string_inclusions through which I am getting an updated sign_up_fee value based on my "Available Subscription". Below is my code.
add_filter('woocommerce_subscriptions_product_price_string_inclusions', 'updateSubscriptionPeriod', 10, 2);
function updateSubscriptionPeriod( $include, $product ) {
if (is_admin() || (! $product->is_type( 'variable-subscription' ))) return $include;
$min_max_variation_data = $product->get_min_and_max_variation_data( array_keys( $prices ) );
if(empty($min_max_variation_data) || empty($min_max_variation_data['subscription'])) return $include;
$include['sign_up_fee'] = $min_max_variation_data['subscription']['signup-fee'];
$include['trial_length'] = 5;
$include['subscription_period'] = 50;
$include['subscription_length'] = $min_max_variation_data['subscription']['interval'];
$include['subscription_price'] = $min_max_variation_data['max']['price'];
return $include;
}
Now I am able to get value 20, please see screenshot below.
But as you can see the difference in my screenshot, I am still not getting the updated values here every 3 years with a 2-week free trial a as it should be said, very 4 years with a 2-month free trial because this is what my values from my available variations.
You can also see I am trying to put some static values as well as dynamic values like this.
$include['trial_length'] = 5;
$include['subscription_period'] = 50;
$include['subscription_length'] = $min_max_variation_data['subscription']['interval'];
$include['subscription_price'] = $min_max_variation_data['max']['price'];
But its just not updating in my product page.
Can someone guide me please how can I update these due values.
Thanks in advance.
UPDATE 2
I have searched across and found woocommerce_subscriptions_product_price_string hook and here is my attempt.
add_filter( 'woocommerce_subscriptions_product_price_string', 'subscriptions_custom_price_string', 20, 3 );
function subscriptions_custom_price_string( $price_string, $product, $args ) {
// Get the trial length to check if it's enabled
$trial_length = $args['trial_length'];
$speriod = $args['subscription_period'];
$sfee = $args['sign_up_fee'];
$sfee = wc_price($sfee);
$sign_up_fee = isset($args['sign_up_fee']) ? __(" and a $sfee sign-up fee", "woocommerce") : '';
if( $trial_length > 0 )
$price_string = $args['price'] . ' / ' . $speriod . $sign_up_fee;
return $price_string;
}
And its showing like this below.
So now basically its updating but its not correct as I lost a format and some other important values.
Can someone guide how can I properly overwrite this filter ?
Thanks

woocommerce_variable_subscription_price_html is the correct hook that you should be using, and here's an example that might work for you:
add_filter( 'woocommerce_variable_subscription_price_html', 'variable_subscription_custom_price_html', 10, 2 );
function variable_subscription_custom_price_html( $price, $product ) {
// Unhook to avoid infinite loop.
remove_filter( 'woocommerce_variable_subscription_price_html', __FUNCTION__, 10, 2 );
// List of product IDs for variations you're hiding.
$hidden_products = array( 4171 );
// Check if the product has any variations that you manually hid.
$hidden = array_intersect( $hidden_products, get_children( array(
'post_parent' => $product->get_id(),
'post_type' => 'product_variation',
'fields' => 'ids',
) ) );
if ( ! empty( $hidden ) ) {
$prices = $product->get_variation_prices( true )['price'];
asort( $prices ); // sort from lowest to highest price
$variation = wc_get_product( array_keys( $prices )[0] );
// It's a variation, so we manually add the 'From: text'.
$price = 'From: ' . $variation->get_price_html();
}
// Re-hook this function.
add_filter( 'woocommerce_variable_subscription_price_html', __FUNCTION__, 10, 2 );
return $price;
}

Related

Add suffix text to specific product category in Woocommerce [duplicate]

I need to add 'per metre' to the price on most of my online catalogue, I tried the code on this thread in my finctions.php but I cannot get it to omit/include particular categories- it seems to be all or nothing. What am I doing wrong?
I have edited the code as such:
/*add 'per metre' after selected items*/
add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
// HERE define your product categories (can be IDs, slugs or names)
$product_categories = array('fabric','haberdashery', 'lining',);
if( ! has_term( $product_categories, 'fasteners', 'patches', 'remnnants', $product->get_id() ) )
$price .= ' ' . __('per metre');
return $price;
}
I want 'fabrics', 'haberdashery', 'lining' to show per metre, and 'fasteners', 'patches', 'remnants' to NOT show the suffix.
I have tried variations of the code -my exclusions in the top bit and the inclusions in the second part, and with/without the "( ! has term" section, but whichever I do takes all the suffix messages away, or applies to all categories.
It would be amazing if I could get this to work as have previously been using a very bloated plug-in. I'm only basically capable in this stuff so please feel free to talk me through it as if I am an idiot.
There is a little mistake in your code in the has_term() function.
To handle parent product categories, we will use a custom conditional function instead of has_tem().
I have also added some code to handle the product variation selected price of variable products, So try this instead:
// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
$parent_term_ids = $categories_ids = array(); // Initializing
$taxonomy = 'product_cat';
$product_id = $product_id == 0 ? get_the_id() : $product_id;
if( is_string( $categories ) ) {
$categories = (array) $categories; // Convert string to array
}
// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
$result = (array) term_exists( $category, $taxonomy );
if ( ! empty( $result ) ) {
$categories_ids[] = reset($result);
}
}
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}
add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 10, 2 );
function conditional_price_suffix( $price, $product ) {
// Handling product variations
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
// HERE define your product categories (can be IDs, slugs or names)
$product_categories = array('fabric','haberdashery', 'lining');
if( has_product_categories( $product_categories, $product_id ) )
$price .= ' ' . __('per metre');
return $price;
}
Code goes in function.php file of your active child theme (or active theme). tested and works.

Save value from a custom row added after the order table and display it in WooCommerce orders and emails

This code I wrote displays personalized information on the WooCommerce checkout page.
add_action( 'woocommerce_cart_totals_after_order_total', 'show_total_discount_cart_checkout', 9999 );
add_action( 'woocommerce_review_order_after_order_total', 'show_total_discount_cart_checkout', 9999 );
function show_total_discount_cart_checkout() {
$discount_total = 0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$subtotal = WC()->cart->get_product_subtotal( $product, $cart_item['quantity'] );
$total = WC()->cart->total;
$pctm = 90.00;
$valor_descontado = $total - ($total / 100 * $pctm);
$sale_price = '10%';
$discount = ( WC()->cart->total - $valor_descontado );
$discount_total = $discount;
}
if ( $discount_total > 0 ) {
echo '<tr><th>VOCÊ RECEBERÁ DE CASHBACK:</th><td data-title="You">' . wc_price( $discount_total + WC()->cart->get_discount_total() ) .'</td></tr>';
}
}
The result:
I need this information to also be displayed in WooCommerce orders and emails. I believe I can come up with a solution myself to display this value on several other pages, but can someone first tell me how to save/store the value of this calculation?
First of all, I've rewritten your existing code for the following reasons:
Requesting subtotals and totals is best done outside the foreach loop, because otherwise these values ​​will be overwritten every time
The result of $subtotal is not used anywhere in your code
Since the result of $subtotal is not used anyway, loop through the cart seems unnecessary
$sale_price is also not used anywhere in your code
Since $discount_total = $discount it is not necessary to use a new variable
A session variable is created/added
Your existing code, but optimized:
function action_woocommerce_after_order_total() {
// WC Cart NOT null
if ( ! is_null( WC()->cart ) ) {
// Get cart
$cart = WC()->cart;
// Getters
$cart_total = $cart->total;
$cart_discount_total = $cart->get_discount_total();
// Settings
$pctm = 90;
// Calculations
$discounted_value = $cart_total - ( $cart_total / 100 * $pctm );
$discount_total = $cart_total - $discounted_value;
// Greater than
if ( $discount_total > 0 ) {
// Result
$result = $discount_total + $cart_discount_total;
// The Output
echo '<tr class="my-class">
<th>' . __( 'VOCÊ RECEBERÁ DE CASHBACK', 'woocommerce' ) . '</th>
<td data-title="You">' . wc_price( $result ) . '</td>
</tr>';
// Set session
WC()->session->set( 'session_result', $result );
}
}
}
add_action( 'woocommerce_cart_totals_after_order_total', 'action_woocommerce_after_order_total', 10 );
add_action( 'woocommerce_review_order_after_order_total', 'action_woocommerce_after_order_total', 10 );
To answer your question:
Step 1) We get the result from the session variable and add it as order data, so that we can use/obtain this information everywhere via the $order object
// Add as custom order meta data and reset WC Session variable
function action_woocommerce_checkout_create_order( $order, $data ) {
// Isset
if ( WC()->session->__isset( 'session_result' ) ) {
// Get
$result = (float) WC()->session->get( 'session_result' );
// Add as meta data
$order->update_meta_data( 'result', $result );
// Unset
WC()->session->__unset( 'session_result' );
}
}
add_action( 'woocommerce_checkout_create_order', 'action_woocommerce_checkout_create_order', 10, 2 );
Step 2) Use the woocommerce_get_order_item_totals filter hook, which will allow you to add a new row to the existing tables with the $result.
The new row will be added in:
Email notifications
Order received (thank you page)
My account -> view order
function filter_woocommerce_get_order_item_totals( $total_rows, $order, $tax_display ) {
// Get meta
$result = $order->get_meta( 'result' );
// NOT empty
if ( ! empty ( $result ) ) {
// Add new row
$total_rows['total_result']['label'] = __( 'VOCÊ RECEBERÁ DE CASHBACK', 'woocommerce' );
$total_rows['total_result']['value'] = wc_price( $result );
}
return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 );

Auto update product custom field based on WooCommerce product stock

I have an ACF field "DeliverySpeed" attached to Products with the values 'Fast' and 'Slow'
I would like to update this field to change to value 'Slow' every time the product stock is zero or less (product on backorder)
I am still learning PHP so far this is what I got to but I am sure different things are missing, I just don't know in which direction to go:
based on acf update field documentation
function automatically_change_delivery( ) {
global $product;
$fieldkey = "DeliverySpeed";
$valueslow = "Slow";
if($product->get_stock_quantity()<0) { update_field( $field_key, $valueslow, $post_id );
} }
Thank you in advance for the attention and advice.
The following code will update automatically your product custom field once order has reduced product stock levels. So when product has stock the custom field value will be 'Fast' otherwise 'Slow'.
The code:
add_action( 'woocommerce_payment_complete', 'update_product_custom_field_after_reduced_stock_levels', 20, 2 );
add_action( 'woocommerce_order_status_completed', 'update_product_custom_field_after_reduced_stock_levels', 20, 2 );
add_action( 'woocommerce_order_status_processing', 'update_product_custom_field_after_reduced_stock_levels', 20, 2 );
add_action( 'woocommerce_order_status_on-hold', 'update_product_custom_field_after_reduced_stock_levels', 20, 2 );
function update_product_custom_field_( $order_id, $order = '' ) {
// Continue only when order has reduced product stock levels
if ( wc_string_to_bool( get_post_meta( $order_id, '_order_stock_reduced', true ) ) )
return $order_id; // Exit
if( ! $order || ! is_a( $order, 'WC_Order') ) {
$order = wc_get_order( $order_id ); // Get the WC_Order object if it's empty
}
$field_key = 'DeliverySpeed';
// Loop through order items
foreach ( $order->get_items() as $item ) {
$product = $cart_item['data'];
$product_id = $product->get_id();
$stock_qty = $product->get_stock_quantity();
$field_value = get_field( $field_key, $product_id ); // Get ACF field value
if ( $stock_qty <= 0 && $field_value === 'Fast' ) {
update_field( $field_key, 'Slow', $product_id );
}
elseif ( $stock_qty > 0 && $field_value === 'Slow' ) {
update_field( $field_key, 'Fast', $product_id );
}
}
}
Code goes in functions.php file of the active child theme (or active theme). It should works.

Show "Out of Stock" text , next to variation in Woocommerce [duplicate]

This question already has answers here:
How to add variation stock status to Woocommerce product variation dropdown
(4 answers)
Closed 4 years ago.
I Would like next to variation to show "Out of Stock" when product variation is out of stock. Below, I am using the code from this answer:
add_filter( 'woocommerce_variation_option_name',
'customizing_variations_terms_name', 10, 1 );
function customizing_variations_terms_name( $term_name ){
if(is_admin())
return $term_name;
global $product;
$second_loop_stoped = false;
// Get available product variations
$product_variations = $product->get_available_variations();
// Iterating through each available product variation
foreach($product_variations as $variation){
$variation_id = $variation['variation_id'];
$variation_obj = new WC_Product_Variation( $variation_id );
## WOOCOMMERCE RETRO COMPATIBILITY ##
if ( version_compare( WC_VERSION, '3.0', '<' ) ) # BEFORE Version 3 (older)
{
$stock_status = $variation_obj->stock_status;
$stock_qty = intval($variation_obj->stock);
// The attributes WC slug key and slug value for this variation
$attributes_arr = $variation_obj->get_variation_attributes();
}
else # For newest verions: 3.0+ (and Up)
{
$stock_status = $variation_obj->get_stock_status();
$stock_qty = $variation_obj->get_stock_quantity();
// The attributes taxonomy key and slug value for this variation
$attributes_arr = $variation_obj->get_attributes();
}
if(count($attributes_arr) != 1) // Works only for 1 attribute set in the product
return $term_name;
// Get the terms for this attribute
foreach( $attributes_arr as $attr_key => $term_slug){
// Get the attribute taxonomy
$term_key = str_replace('attribute_', '', $attr_key );
// get the corresponding term object
$term_obj = get_term_by( 'slug', $term_slug, $term_key );
if( $term_obj->name == $term_name ){ // If the term name matches we stop the loops
$second_loop_stoped = true;
break;
}
}
if($second_loop_stoped)
break;
}
if( $stock_qty>0 )
return $term_name .= ' - ' . $stock_status . ' ('.$stock_qty.')';
else
return $term_name .= ' - ' . $stock_status;
}
but when test on my site, it show "Out of Stock" for all variations i have on selected product. For example on this product it show "Out of Stock" for all variations i have, but in fact, only variation "50 ליטר" is in real "Out Of Stock". other variations are In Stock ! I using Woocommerce 3.3.5 version. Can someone to tell me what i need to do to show out of stock next to variations if product is out of stock for real, because i m using radio buttons as replacement for standard dropdown button from Woocommerce.
Seems over complicated, I managed to do it this way. Seems to work, displays out of stock for the correct variations when I tested it.
add_filter( 'woocommerce_variation_option_name', 'customizing_variations_terms_name', 10, 1 );
function customizing_variations_terms_name( $term_name ) {
global $product;
// Get available product variations
$product_variations = $product->get_available_variations();
// product_variation terms are normalized to lowercase with
// spaces replaced by dashes.
// if your term name contains capital letters or spaces i.e. 'SIZE 6'
// the product variation will be lowercase with dashes, i.e. 'size-6'
/*
$term_name = str_replace(' ', '-', strtolower($term_name));
*/
foreach ( $product_variations as $product_variation ) {
if( isset( $product_variation['attributes'] ) ) {
$key = array_search($term_name, $product_variation['attributes']);
if( $key !== false && ! $product_variation['is_in_stock'] ) {
return $term_name . ' - Out of Stock';
}
}
}
return $term_name;
}
In addition to this you can grey out the product so they can't select it if that's something that are after.
add_filter( 'woocommerce_variation_is_active', 'grey_out_variations_when_out_of_stock', 10, 2 );
function grey_out_variations_when_out_of_stock( $grey_out, $variation ) {
if ( ! $variation->is_in_stock() )
return false;
return true;
}

Woocommerce sortable columns not working

I've added a couple of custom field columns to our Woocommerce orders list in the admin of WordPress using the methods below, but the sort is not working....
add_filter( 'manage_edit-shop_order_columns', 'my_wc_columns' );
function my_wc_columns($columns){
$new_columns = (is_array($columns)) ? $columns : array();
unset( $new_columns['order_actions'] );
$new_columns['program_id'] = 'Program';
$new_columns['constituent_id'] = 'Constituent ID';
$new_columns['order_actions'] = $columns['order_actions'];
return $new_columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'my_wc_column_values', 2 );
function my_wc_column_values($column){
global $post;
if ( $column == 'program_id' ) {
$program = get_post_meta( $post->ID, '_program_id', true );
$program_title = get_the_title($program);
$column_val = (isset($program) && $program>0 ? $program_title : 'All');
echo '<span>' . my_programs_get_name( $column_val ) . ' (' . $program . ')</span>';
}
if ( $column == 'constituent_id' ) {
$consid = get_post_meta( $post->ID, 'constituent_id', true );
$column_val = (isset($consid) && $consid != "") ? $consid : "";
echo '<span>' . $column_val . '</span>';
}
}
// Make column sortable
add_filter( "manage_edit-shop_order_sortable_columns", 'my_wc_column_sort' );
function my_wc_column_sort( $columns ) {
$custom = array(
'program_id' => '_program_id',
'constituent_id' => 'constituent_id',
);
return wp_parse_args( $custom, $columns );
}
I expected to have an issue perhaps with the program name, since it is an id that needs to be translated via a custom function to a name, but neither column is sorting properly. The records change order after clicking their column titles, but I cannot tell how the sort is being done. The program is not sorting on name or ID and both are seem random but consistent. Keep in mind both fields are custom fields that may or may not have a value defined. How can I make this sortable?
Here's a good tutorial on custom sortable columns. After you register the column, you need to handle the actual sorting. Sadly, that part doesn't happen automagically. Untested, but adapted from the above tutorial:
add_action( 'pre_get_posts', 'manage_wp_posts_be_qe_pre_get_posts', 1 );
function manage_wp_posts_be_qe_pre_get_posts( $query ) {
/**
* We only want our code to run in the main WP query
* AND if an orderby query variable is designated.
*/
if ( $query->is_main_query() && ( $orderby = $query->get( 'orderby' ) ) ) {
switch( $orderby ) {
// If we're ordering by 'program_id'
case 'program_id':
// set our query's meta_key, which is used for custom fields
$query->set( 'meta_key', '_program_id' );
/**
* Tell the query to order by our custom field/meta_key's
* value
*
* If your meta value are numbers, change 'meta_value'
* to 'meta_value_num'.
*/
$query->set( 'orderby', 'meta_value' );
break;
}
}
}

Resources