If regular price is blank show only sales price in Woocommerce - wordpress

Just like the title said:
How can i show only the sales price in Woocommerce if the regular price is blank?

I would go for something like:
$regular_price = get_post_meta( get_the_ID(), '_regular_price', true);
$price_sale = get_post_meta( get_the_ID(), '_sale_price', true);
if ($regular_price == '') {
echo $price_sale;
} else {
echo $regular_price;
}

add_filter( 'woocommerce_get_price_html', 'change_product_price_html', 10, 2 );
function change_product_price_html( $price_html, $product ) {
global $post;
$id = !empty( $product->get_id() ) ? $product->get_id() : $post->ID;
$regular_price = get_post_meta( $id, '_regular_price', true);
$sale_price = get_post_meta( $id, '_sale_price', true);
$price_html = ( $regular_price == '' ) ? $sale_price : $regular_price;
return $price_html;
}
Please use this code in your active theme's functions.php file .

Related

How to set post tags by some meta keys?

I prepared following code to overwrite post tags following several meta keys.
Now, it works for only one meta key. How can I apply it to several meta keys?
<?php
global $post;
$post_id = $post->ID;
$tags[] = get_post_meta($post_id, 'aaaaa', $single);
$tags[] = get_post_meta($post_id, 'bbbbb', $single);
$tags[] = get_post_meta($post_id, 'ccccc', $single);
$tags = array_unique( array_filter( $tags ) ); // here, adding array_filter to remove empty get_post_meta results
wp_set_post_tags( $post_id, $tags, false );
?>
By above code, meta value of meta key aaaaa only applied as post tag, and meta value of meta key bbbbb and ccccc is ignored.
After I hear advise below, I tried this code too.
<?php
global $post;
$post_id = $post->ID;
$tags = get_post_meta($post_id, 'aaaaa', $single);
if (!empty($tag) ) { wp_add_post_tags( $post_id, $$tag ); }
$tags = get_post_meta($post_id, 'bbbbb', $single);
if (!empty($tag) ) { wp_add_post_tags( $post_id, $$tag ); }
$tags = get_post_meta($post_id, 'ccccc', $single);
if (!empty($tag) ) { wp_add_post_tags( $post_id, $$tag ); }
wp_set_post_tags( $post_id, $tags, false );
?>
Then, all tag is eliminatd.
Your code looks right, but I suggest that you check if the meta key bbbbb and ccccc does have value or not int he first place. So try to display something like that.
Also, give this a go and try to add one tag in time.
https://developer.wordpress.org/reference/functions/wp_add_post_tags/
Like this:
<?php
global $post;
$post_id = $post->ID;
$tag = get_post_meta($post_id, 'aaaaa', $single);
if (!empty($tag) ) { wp_add_post_tags( $post_id, $$tag ); }
$tag = get_post_meta($post_id, 'bbbbb ', $single);
if (!empty($tag) ) { wp_add_post_tags( $post_id, $$tag ); }
$tag = get_post_meta($post_id, 'ccccc', $single);
if (!empty() ) { wp_add_post_tags( $post_id, $$tag ); }
?>
Hope this help you.
global $post;
$post_id = $post->ID;
$array_tags_and_ids = [
$post_id => ["aaaaa", "bbbbb", "ccccc"]
];
foreach ($array_tags_and_ids as $key => $value) {
// re-call the value and get only the text value
$implode_tags_names = implode( ', ', $value );
// wordpress function
wp_set_post_tags( $key, $implode_tags_names, true );
}
I changed third argument to true(default is false), and it started to work!
like this
global $post;
$post_id = $post->ID;
$tags[] = get_post_meta( $post->ID, 'aaaaa', true );
$tags[] = get_post_meta( $post->ID, 'bbbbb', true );
$tags[] = get_post_meta( $post->ID, 'ccccc', true );
$tags = array_unique( array_filter( $tags ) );
wp_set_post_tags($post_id , $tags, false );

How to get the vendor_ids for each of the vendors with products on the cart page in woocommerce

global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
echo "<b>".$_product->post_title.'</b> <br> Quantity: '.$values['quantity'].'<br>';
$price = get_post_meta($values['product_id'] , '_price', true);
echo " Price: ".$price."<br>";
I have used the following to get data about the products on the cart and display it. is it possible to get the vendor of each product too and how can I do that in woo commerce? again from the cart page because I want to echo the vendor address back under each product on the cart page.
add_filter( 'woocommerce_get_item_data', 'wc_add_address_to_cart', 10, 2 );
function wc_add_address_to_cart( $other_data, $cart_item ) {
$post_data = get_post( $cart_item['product_id'] );
$post_data->post_author;
$vendor_id = $post_data->post_author;
echo '<br>';
$Add = 'Address: ';
$city = 'City: ';
$tel = 'Phone: ';
echo $test;
$user_id = $vendor_id;
// Get all user meta data for $user_id
$meta = get_user_meta( $user_id );
// Filter out empty meta data
$meta = array_filter( array_map( function( $a ) {
return $a[0];
}, $meta ) );
echo $Add;
print_r( $meta ['_vendor_address_1'] );
echo '<br>';
echo $city;
print_r( $meta['_vendor_city'] );
echo '<br>';
echo $tel;
print_r( $meta['_vendor_phone'] );
return $other_data;
}
Finally got what I was looking for. Just add that to the functions.php

Woocommerce hook after product is published

I'm using below
add_action('transition_post_status', 'my_product_update', 1000, 3);
function my_product_update($new_status, $old_status, $post) {
if($new_status == 'publish' && $post->post_type == "product") {
$product_id = $post->ID;
$product = new WC_Product($product_id);
echo $product->get_price();
}
}
I figure it out that $product->get_price() is string(0) "".
I think it beacause I retrieve the price before it's saved.
But I can retrieve the name by using $product->post->post_title
Any idea how I can get the price right after the product is published?
Thank you.
Try This :
function wpa104760_default_price( $post_id, $post ){
echo $price = get_post_meta( $post_id, '_regular_price', true);
echo $sale = get_post_meta( $post_id, '_sale_price', true);
// exit;
}
add_action( 'woocommerce_process_product_meta', 'wpa104760_default_price',1000,2 );

change "out of stock" text with html element woocommerce

i want to change the "out of stock" text with html element woocommerce.
Currently, I'm using:
add_filter( 'woocommerce_get_availability', 'change_out_of_stock_text', 1, 2);
function change_out_of_stock_text( $availability, $_product ) {
global $post;
if ( ! $_product->is_in_stock() ) {
$out_of_stock = "Out of Stock";
if(get_post_meta( $post->ID, '_text_field', true )!="") {
$out_of_stock = get_post_meta( $post->ID, '_text_field', true );
}
$availability['availability'] = __($out_of_stock, 'woocommerce');
}
return $availability;
}
It can change the "Out of Stock" text with the value from custom input field i made in product page but i want to show the html element from that value to shop page as well. For example if the value is 'Sold out', it should output the link text as well "Sold out".
I also tried woocommerce_stock_html but it got the same problem:
function change_out_of_stock_text( $html, $_availability, $_product ){
global $post;
$out_of_stock = "Out of Stock";
$_availability = $_product->get_availability();
if(get_post_meta( $post->ID, '_text_field', true )!="") {
$out_of_stock = get_post_meta( $post->ID, '_text_field', true );
}
$html = '<p class="stock ' . esc_attr( $_availability['class'] ) . '">' . $out_of_stock . '</p>';
return $html;
}
add_filter( 'woocommerce_stock_html', 'change_out_of_stock_text', 10, 3 );
Seems like you have the right idea but you are't targeting the appropriate filter which is woocommerce_get_availability
function so_34126704_availability( $array, $product ){
if ( ! $_product->is_in_stock() ) && ( $status = get_post_meta( $product->id, '_text_field', true ) ) != "" ){
$availability['availability'] = $status;
}
return $array;
}
add_filter( 'woocommerce_get_availability', 'so_34126704_availability', 10, 2 );
to filter the output html you need to filter woocommerce_stock_html. it's a bit half-baked because I don't know what you'd want to do with the classes or where you are linking to, but here's a shell example of that:
function so_34126704_availability_html( $html, $_availability, $product ){
$availability = $product->get_availability(); // for some reason the $_availability does not pass the entire array, only the availability key
$status = get_post_meta( $product->id, '_text_field', true );
$html = '<p class="stock ' . esc_attr( $availability['class'] ) . '"><a href="#somewhere"/>' . esc_html( $status ) . '</a></p>';
return $html;
}
add_filter( 'woocommerce_stock_html', 'so_34126704_availability_html', 10, 3 );

WordPress add filter to wp_get_attachment_link

I need custom class for filter to wp_get_attachment_link. So what I so:
function modify_attachment_link( $markup ) {
global $post;
return str_replace( '<a href', '<a class="view" rel="galleryid-'. $post->ID .'" href', $markup );
}
add_filter( 'wp_get_attachment_link', 'modify_attachment_link' );
It's work fine. But what I need to do in case if Link thumbnails to: Attachment Page
I mean I don't need a custom class at this case. Any help please?
And core function for wp_get_attachment_link is:
function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false ) {
$id = intval( $id );
$_post = & get_post( $id );
if ( empty( $_post ) || ( 'attachment' != $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) )
return __( 'Missing Attachment' );
if ( $permalink )
$url = get_attachment_link( $_post->ID );
$post_title = esc_attr( $_post->post_title );
if ( $text )
$link_text = esc_attr( $text );
elseif ( $size && 'none' != $size )
$link_text = wp_get_attachment_image( $id, $size, $icon );
else
$link_text = '';
if ( trim( $link_text ) == '' )
$link_text = $_post->post_title;
return apply_filters( 'wp_get_attachment_link', "<a href='$url' title='$post_title'>$link_text</a>", $id, $size, $permalink, $icon, $text );
}
So I mean if ( $permalink ) I don't need to add custom class for this function.
Try
function modify_attachment_link( $markup, $id, $size, $permalink ) {
global $post;
if ( ! $permalink ) {
$markup = str_replace( '<a href', '<a class="view" rel="galleryid-'. $post->ID .'" href', $markup );
}
return $markup;
}
add_filter( 'wp_get_attachment_link', 'modify_attachment_link', 10, 4 );
That may work

Resources