Get prices only on specific category for Woocommerce - wordpress

I am trying to display prices next to variations on Woocommerce, which I could do just fine. Now instead of showing prices on ALL variations I just want to show prices next to variations belonging to a specific category.
This is what I have come up with so far and logically it makes sense, but not working on this particular category. Any suggestions on how to troubleshoot?
//get prices of variations
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $product->id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
$itemPrice = strip_tags (woocommerce_price( $_product->get_price() ));
//this is where you can actually customize how the price is displayed
return $term . ' (' . $itemPrice . ')';
}
return $term;
//Add prices to variations
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $termItem ) $categories[] = $termItem->slug;
if ( in_array( 'custom-pet-portrait-painting', $categories ) ) {
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
} //end function
}
Or can I use get_terms to wrap the below code in which shows the variations price? I pretty much just need to show the prices for variations pertaining to one category.
if (has_term ('my-category', 'product_cat')) {
//get prices of variations
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $product->id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
$itemPrice = strip_tags (woocommerce_price( $_product->get_price() ));
//this is where you can actually customize how the price is displayed
return $term . ' (' . $itemPrice . ')';
}
return $term;
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
} //end has_term

Related

Woocommerce - add and remove product automaticly from specific category when product's sale schedule start and ended

I have written a code to add product to specific category when product's sale schedule start and remove from those category when product's sale schedule ended. This is my code but not working. Could anybody help me?
function zt_wc_scheduled_sales() {
$data_store = WC_Data_Store::load( 'product' );
// Sales which are due to start.
$product_ids = $data_store->get_starting_sales();
if ( $product_ids ) {
do_action( 'wc_before_products_starting_sales', $product_ids );
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( $product ) {
$sale_price = $product->get_sale_price();
wp_set_object_terms( $product_id, 'pishnahad', 'product_cat', true );
$product->save();
}
}
do_action( 'wc_after_products_starting_sales', $product_ids );
WC_Cache_Helper::get_transient_version( 'product', true );
delete_transient( 'wc_products_onsale' );
}
// Sales which are due to end.
$product_ids = $data_store->get_ending_sales();
if ( $product_ids ) {
do_action( 'wc_before_products_ending_sales', $product_ids );
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( $product ) {
wp_remove_object_terms( $product_id, 'pishnahad', 'product_cat' );
$product->save();
}
}
do_action( 'wc_after_products_ending_sales', $product_ids );
WC_Cache_Helper::get_transient_version( 'product', true );
delete_transient( 'wc_products_onsale' );
}
}
add_action( 'woocommerce_scheduled_sales', 'zt_wc_scheduled_sales' );
I have fixed bugs in your code.
Firts i have replaced get_starting_sales() and get_ending_sales() with custom query. so now you can get product ids
then I rearrange and removed unused code.
function zt_wc_scheduled_sales() {
$data_store = WC_Data_Store::load( 'product' );
global $wpdb;
// Sales which are due to start.
$product_ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT postmeta.post_id FROM {$wpdb->postmeta} as postmeta
LEFT JOIN {$wpdb->postmeta} as postmeta_2 ON postmeta.post_id = postmeta_2.post_id
LEFT JOIN {$wpdb->postmeta} as postmeta_3 ON postmeta.post_id = postmeta_3.post_id
WHERE postmeta.meta_key = '_sale_price_dates_from'
AND postmeta_2.meta_key = '_price'
AND postmeta_3.meta_key = '_sale_price'
AND postmeta.meta_value > 0
AND postmeta.meta_value < %s",
time()
)
);
if ( $product_ids ) {
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( $product ) {
wp_set_object_terms( $product_id, 'pishnahad', 'product_cat', true );
$product->save();
}
}
}
// Sales which are due to end.
$product_ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT postmeta.post_id FROM {$wpdb->postmeta} as postmeta
LEFT JOIN {$wpdb->postmeta} as postmeta_2 ON postmeta.post_id = postmeta_2.post_id
LEFT JOIN {$wpdb->postmeta} as postmeta_3 ON postmeta.post_id = postmeta_3.post_id
WHERE postmeta.meta_key = '_sale_price_dates_to'
AND postmeta_2.meta_key = '_price'
AND postmeta_3.meta_key = '_regular_price'
AND postmeta.meta_value > 0
AND postmeta.meta_value < %s",
time()
)
);
if ( $product_ids ) {
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( $product ) {
wp_remove_object_terms( $product_id, 'pishnahad', 'product_cat' );
$product->save();
}
}
}
}
add_action( 'woocommerce_scheduled_sales', 'zt_wc_scheduled_sales' );
Tested and works.

How to search variable product by its variation id in woocommerce admin product search box?

I am facing the issue that I want to search for every product IDs including product variants in woocommerce admin. How to search product by variation id in woocommerce admin ?
can you please help me?
Thanks in advance.
Finally I have implement the above functionality by modifying the filter "posts_search".
By using bellow code you can search variable products by their variations id in woocommerce admin product search area.
add_filter( 'posts_search', 'product_search' );
function product_search( $where ) {
global $pagenow, $wpdb, $wp;
if ( 'edit.php' != $pagenow || ! is_search() || ! isset( $wp->query_vars['s'] ) || 'product' != $wp->query_vars['post_type'] ) {
return $where;
}
$search_ids = array();
$terms = explode( ',', $wp->query_vars['s'] );
foreach ($terms as $term){
if (is_numeric($term)){
$post_type = get_post_type( $term );
if($post_type == 'product_variation'){
$search_ids[] = wp_get_post_parent_id($term);
}else{
$search_ids[] = $term;
}
}
// Attempt to get a SKU
$sku_to_id = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_parent FROM {$wpdb->posts} LEFT JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id WHERE meta_key='_sku' AND meta_value LIKE %s;", '%' . $wpdb->esc_like( wc_clean( $term ) ) . '%' ) );
$sku_to_id = array_merge( wp_list_pluck( $sku_to_id, 'ID' ), wp_list_pluck( $sku_to_id, 'post_parent' ) );
if (sizeof($sku_to_id) > 0) {
$search_ids = array_merge($search_ids, $sku_to_id);
}
}
$search_ids = array_filter( array_unique( array_map( 'absint', $search_ids ) ) );
if ( sizeof( $search_ids ) > 0 ) {
$where = str_replace( 'AND (((', "AND ( ({$wpdb->posts}.ID IN (" . implode( ',', $search_ids ) . ")) OR ((", $where );
}
return $where;
}

Woocommerce variation price get

I would like to GET the actual variation price.
The code default woocommerce (/plugins/woocommerce/templates/single-product/add-to-cart/variable.php):
<div class="woocommerce-variation-price">
{{{ data.variation.price_html }}} </div>
I how to GET this "data.variation.price_html"?
I tried this:
$pricenow = $product->get_variation_price();
<? echo $pricenow; ?>
But, only displays the lowest price...
How do I do this so that your preferred price range?
Thank you for your help!
Please add below code to your current theme's function.php file and view the changes on front side (i.e. Variable product detail page)
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
if ( empty( $term ) ) return $term;
if ( empty( $product->id ) ) return $term;
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $product->id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
return $term . ' (' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ')';
}
return $term;
}
<?php
global $product;
if ($product->is_type( 'simple' )) { ?>
<p class="price"><?php echo $product->get_price_html(); ?></p>
<?php } ?>
<?php
if($product->product_type=='variable') {
$available_variations = $product->get_available_variations();
$count = count($available_variations)-1;
$variation_id=$available_variations[$count]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation.
$variable_product1= new WC_Product_Variation( $variation_id );
$regular_price = $variable_product1 ->regular_price;
$sales_price = $variable_product1 ->sale_price;
echo $regular_price+$sales_price;
}
?>

WooCommerce Show price next to product variation

I have a list of variations of a product but I would like to show the price next to it. This way the user can immediately see the price difference between each variations. http://cl.ly/15150P3D1U3k
Any way how to do this in WooCommerce knowing my site is multi-langual?
Thank you!
Please try this. Which i have just tested in the latest WordPress and WooCommerce. Just add it to your functions file.
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
if ( empty( $term ) ) return $term;
if ( empty( $product->id ) ) return $term;
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $product->id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
return $term . ' (' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ')';
}
return $term;
}

Next / Previous Post Links from Same Category and Alphabetically ordered

RESOLVED
I'm exhaustively searching for a method to provide Next and Previous Post Links in a different way from which it usually appears in Single Post.
By DEFAULT it:
Is chronological ordered
Links to posts from all blog categories
But I NEED it:
ALPHABETICALLY ordered
Linking to posts from SAME CATEGORY only
I'm not a developer but I found two codes and I think if I could merge both the problem would be solved. Could someone help me please?
CODE 1 - Turn Next/Prev links alphabetcally, but not from same category (source)
function filter_next_post_sort($sort) {
$sort = "ORDER BY p.post_title ASC LIMIT 1";
return $sort;
}
function filter_next_post_where($where) {
global $post, $wpdb;
return $wpdb->prepare("WHERE p.post_title > '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'",$post->post_title);
}
function filter_previous_post_sort($sort) {
$sort = "ORDER BY p.post_title DESC LIMIT 1";
return $sort;
}
function filter_previous_post_where($where) {
global $post, $wpdb;
return $wpdb->prepare("WHERE p.post_title < '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'",$post->post_title);
}
add_filter('get_next_post_sort', 'filter_next_post_sort');
add_filter('get_next_post_where', 'filter_next_post_where');
add_filter('get_previous_post_sort', 'filter_previous_post_sort');
add_filter('get_previous_post_where', 'filter_previous_post_where');
CODE 2 - Turn Next/Prev links from same category, but not alphabetically (source)
add_filter( 'get_next_post_join', 'navigate_in_same_taxonomy_join', 20);
add_filter( 'get_previous_post_join', 'navigate_in_same_taxonomy_join', 20 );
function navigate_in_same_taxonomy_join() {
global $wpdb;
return " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
}
add_filter( 'get_next_post_where' , 'navigate_in_same_taxonomy_where' );
add_filter( 'get_previous_post_where' , 'navigate_in_same_taxonomy_where' );
function navigate_in_same_taxonomy_where( $original ) {
global $wpdb, $post;
$where = '';
$taxonomy = 'category';
$op = ('get_previous_post_where' == current_filter()) ? '<' : '>';
$where = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
return $original ;
$term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
$term_array = array_map( 'intval', $term_array );
if ( ! $term_array || is_wp_error( $term_array ) )
return $original ;
$where = " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
return $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $where", $post->post_date, $post->post_type );
}
After weeks of searching for a solution, here is the FINAL ANSWER!
THANK YOU FOR HELP ME!
You should use get_adjacent_post(); this is provide you next or previous post.
This is for previous post:
<?php
$prev_post = get_adjacent_post( true, '', true, 'taxonomy_slug' ); ?>
if ( is_a( $prev_post, 'WP_Post' ) ) {
?>
<?php echo get_the_title( $prev_post->ID ); ?>
<?php } ?>
This is for next post:
<?php
$next_post = get_adjacent_post( true, '', false, 'taxonomy_slug' );
if ( is_a( $next_post, 'WP_Post' ) ) {
?>
<?php echo get_the_title( $next_post->ID ); ?>
<?php } ?>

Resources