Custom text after price just for guest users in Woocommerce - woocommerce

I'm using this piece of code to show custom text after product price in woocommerce:
function cw_change_product_price_display( $price ) {
$price .= ' <span id="tooltips">incl. VAT</span>';
return $price;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
How to make that text visible just for guest users, NOT for logged in?
Thanks!

Here is the solution:
function cw_change_product_price_display( $price ) {
if( ! is_user_logged_in() ) {
$price .= ' <span id="tooltips">incl. VAT</span>';
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );

Related

Add a custom text before the sale price in WooCommerce

Add a custom text before the price if the product has a sale price in WooCommerce
function add_custom_text_before_sale_prices( $price, $product ) {
// Text
$text_regular_price = __("Regular Price: ");
$text_final_price = __("Final Price: ");
if ( $product->is_on_sale() ) {
$has_sale_text = array(
'<del>' => '<del>' . $text_regular_price,
'<ins>' => '<br>'.$text_final_price.'<ins>'
);
$return_string = str_replace(
array_keys( $has_sale_text ),
array_values( $has_sale_text ),
$price
);
return $return_string;
}
return $price;
} `add_filter( 'woocommerce_get_price_html', 'add_custom_text_before_sale_prices', 100, 2 );`
But I am getting output -
USD100.00
Final Price: USD50.00
But I want
Regular Price: USD100.00
Final Price: USD50.00
you can use this code for it
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
function cw_change_product_price_display( $price ) {
// Your additional text in a translatable string
$text = __('TEXT');
// returning the text before the price
return $text . ' ' . $price;
}
Here are two snippets of code you can use, You can add in an active child theme function.php file.
Method 1: Add text before regular price and sale price
//Add text before regular price and sale price
function vh_rp_sale_price_html( $price, $product ) {
if ( $product->is_on_sale() ) :
$has_sale_text = array(
'<del>' => '<del>Regular Price: ',
'<ins>' => '<br>Sale Price: <ins>'
);
$return_string = str_replace(array_keys( $has_sale_text ), array_values( $has_sale_text ), $price);
else :
$return_string = 'Regular Price: ' . $price;
endif;
return $return_string;
}
add_filter( 'woocommerce_get_price_html', 'vh_rp_sale_price_html', 100, 2 );
Method 2: Add text before regular price only
//Add text before regular price only
function vh_rp_price_html( $price, $product ) {
$return_string = 'Regular Price: ' . $price;
return $return_string;
}
add_filter( 'woocommerce_get_price_html', 'vh_rp_price_html', 100, 2 );
See the attachment Output

Extra product info on Shop page

How do I display the categories of an individual product under the product name on the Shop page listing?
screenshot of Shop page:
You should try this to show the product category above the price of each product.
add_filter( 'woocommerce_get_price_html', 'woo_shipping_with_price_display' );
add_filter( 'woocommerce_cart_item_price', 'woo_shipping_with_price_display' );
function woo_shipping_with_price_display( $price ) {
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
$text = '';
if ( $product_cats && ! is_wp_error ( $product_cats ) ){
$single_cat = array_shift( $product_cats );
$text = '<div itemprop="name" class="product_category_title"><span>'.__(esc_attr($single_cat->name)).'</span></div>';
}
if ($price == true) {
return $text . $price;
}
else {
return $text;
}
}
You can change/update html,class etc.. from line:
$text = '<div itemprop="name" class="product_category_title"><span>'.__(esc_attr($single_cat->name)).'</span></div>';

Adding custom checkbox to WooCommerce product variation options

I've added a custom field to variable products in the WC admin. Using the action:
action_woocommerce_variation_options_pricing
But it's displaying in the wrong location. I'd ideally like the checkbox to display in the options section above. But i can't find the action to move it there.
I've spent ages looking through the WC docs, but can't find the correct action. Does anyone know the correct way for me to move the checkbox to this location?
You can use the woocommerce_variation_options action hook, to add a custom checkbox to the WooCommerce product variation options.
(Copied from: views/html-variation-admin.php, line 188)
<?php do_action( 'woocommerce_variation_options', $loop, $variation_data, $variation ); ?>
So you get:
// Add checkbox
function action_woocommerce_variation_options( $loop, $variation_data, $variation ) {
$is_checked = get_post_meta( $variation->ID, '_mycheckbox', true );
if ( $is_checked == 'yes' ) {
$is_checked = 'checked';
} else {
$is_checked = '';
}
?>
<label class="tips" data-tip="<?php esc_attr_e( 'This is my data tip', 'woocommerce' ); ?>">
<?php esc_html_e( 'Checkbox:', 'woocommerce' ); ?>
<input type="checkbox" class="checkbox variable_checkbox" name="_mycheckbox[<?php echo esc_attr( $loop ); ?>]"<?php echo $is_checked; ?>/>
</label>
<?php
}
add_action( 'woocommerce_variation_options', 'action_woocommerce_variation_options', 10, 3);
// Save checkbox
function action_woocommerce_save_product_variation( $variation_id, $i ) {
if ( ! empty( $_POST['_mycheckbox'] ) && ! empty( $_POST['_mycheckbox'][$i] ) ) {
update_post_meta( $variation_id, '_mycheckbox', 'yes' );
} else {
update_post_meta( $variation_id, '_mycheckbox', 'no' );
}
}
add_action( 'woocommerce_save_product_variation', 'action_woocommerce_save_product_variation', 10, 2 );

How to list custom type post in metabox?

I've created 2 custom type post in Wordpress. Now i what to create custom metabox in one of them to select posts from second custom type post and display it in front. I cannot find how to figure out this problem, i've tried query post but nothing was displayed.
Please check the code to create Metabox named Company Address for example seller custom post type.
Meta box company address for seller custom post type added:
/**
* Meta box company address for seller custom post type added
*/
function wdbs_add_seller_metaboxes() {
add_meta_box(
'wdbs_seller_company_address',
'Company Address',
'wdbs_seller_company_address',
'seller',
'normal',
'default'
);
}
add_action( 'add_meta_boxes', 'wdbs_add_seller_metaboxes' );
Meta box field company address html:
/**
* Meta box field company address html
*/
function wdbs_seller_company_address() {
global $post;
wp_nonce_field( basename( __FILE__ ), 'seller_fields' );
$company_address = get_post_meta( $post->ID, 'company_address', true );
echo '<textarea type="text" name="company_address" class="widefat" rows="6">' . esc_textarea( $company_address ) . '</textarea>';
}
Save Seller metafields:
/**
* SAVE SELLER METAFIELDS
* Saves values for company address meta field
*/
function wdbs_save_seller_meta( $post_id, $post ) {
// Return if the user doesn't have edit permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
if ( ! isset( $_POST['company_address'] ) || ! wp_verify_nonce( $_POST['seller_fields'], basename(__FILE__) ) ) {
return $post_id;
}
$seller_meta['company_address'] = esc_textarea( $_POST['company_address'] );
foreach ( $seller_meta as $key => $value ) :
if ( 'revision' === $post->post_type ) {
return;
}
if ( get_post_meta( $post_id, $key, false ) ) {
update_post_meta( $post_id, $key, $value );
} else {
add_post_meta( $post_id, $key, $value);
}
if ( ! $value ) {
delete_post_meta( $post_id, $key );
}
endforeach;
}
add_action( 'save_post', 'wdbs_save_seller_meta', 1, 2 );

How can i get price in woocommerce cart product title hook

I am working on woocommerce cart page with add_filter "woocommerce_in_cart_product_title", There I want to show price with title.
Like "Product title ($price)"
i have tried this code, but i can not get price.
add_filter( 'woocommerce_in_cart_product_title', 'cart_product_title', 20, 3);
function cart_product_title( $title, $values, $cart_item_key ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
print_r($items);
foreach ($items as $cart_item_key => $values) {
echo $name = $values['prodaddons'][0]['name'].' --- ';
echo $price = $values['prodaddons'][0]['price'].'<br>';
}
echo 'aaaa-'.$title.'-'.$pricees;
}
try this :
add_filter( 'woocommerce_cart_item_name', 'cart_product_title', 20, 3);
function cart_product_title( $title, $values, $cart_item_key ) {
return $title . ' - ' . $values[ 'line_total' ] . '€';
}
I don't know your version of woocommerce but woocommerce_in_cart_product_title is depreciated, it's why I use woocommerce_cart_item_name, so you should try both ;)

Resources