Woocommerce Dynamic Pricing Plugin: add custom text before price - woocommerce

I'm using the woocommerce Dynamic Pricing Plugin - how can I change the "FROM" field and replace with custom text?
Here's my code which is attempting to filter the original plugins code;
The TEST text works fine, but the price is returning as 0.00
add_filter( 'wc_dynamic_pricing_price_html', 'custom_single_price');
function custom_single_price ($html, $_product) {
$from = '<span class="test">' . _x( 'TEST:', 'min_price', 'woocommerce' ) . ' </span>';
$html = '<del>' . WC_Dynamic_Pricing_Compatibility::wc_price( $base_price ) . '</del><ins> ' . $from . WC_Dynamic_Pricing_Compatibility::wc_price( $display_price ) . '</ins>';
return $html;
}

Related

Make brand name linkable under product title in Woocommerce

I am trying to place a clickable link for brands under the product title on Woocommerce single product pages.
I have successfully been able to put the brand under the title, but can't make it a link to the brand category page.
Any help?
I'm using the WooCommerce Brands plugin.
And here is the code I'm using:
add_action( 'woocommerce_single_product_summary', 'wc_brands_add_brand_name', 8 );
function wc_brands_add_brand_name() {
global $product;
$brands = implode(', ', wp_get_post_terms($product->get_id(), 'product_brand', ['fields' => 'names']));
echo "<p>Brand: " . '' . $brands . '' . "</p>";
}
get_the_term_list is the right function to use in a situation where you want to show links of terms of a post.
In get_the_term_list you can set before, after & separator, so you don't even have to make an if check for show/hide the string.
Here is the code:
/**
* Display product brands
*/
function vh_wc_display_brand_links() {
global $product;
echo get_the_term_list( $product->get_id(), 'product_brand', '<p class="product-brands">' . __( 'Brands:', 'text-domain' ) . ' ', ', ', '</p>' );
}
add_action( 'woocommerce_single_product_summary', 'vh_wc_display_brand_links', 8 );

Add image title and/or description to the caption

Is it possible to manipulate the output of the image captions within the_content in such a way that not only the caption, but also the image title and/or description (defined in the media library) are output under each image?
Possibly via the functions.php?
Yes it is possible to manipulate the on the_content.
The easiest way which it looks like you are wanting to do is add a filter in your functions.php file.
I just pulled this from the Codex and modified a bit so you can get the gist of what I'm talking about.
add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );
function my_img_caption_shortcode( $output, $attr, $content ) {
$attr = shortcode_atts( array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr );
if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
return '';
}
if ( $attr['id'] ) {
$attr['id'] = 'id="' . esc_attr( $attr['id'] ) . '" ';
}
return '<div ' . $attr['id']
. 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" '
. 'style="max-width: ' . ( 10 + (int) $attr['width'] ) . 'px;">'
. do_shortcode( $content )
. '<p class="wp-caption-text">' . $attr['caption'] . '</p>'
. '</div>';
}
What you can then do is if you see the $attr['caption'], you can modify how you would like it to be. So you can do: get_the_title($attr['id']) to add the title or get_post_meta($attr['id'], '_wp_attachment_image_alt', TRUE); to get the image alt.
To make things easier if you want to grab all the details on a specific attachment you can use a function like this and then get whatever you want.
Here's an example of adding the title to your caption. The format is "Title - Caption"
. '<p class="wp-caption-text">' . get_the_title($attr['id']) . " - " . $attr['caption'] . '</p>'
Link to Code reference: https://developer.wordpress.org/reference/hooks/img_caption_shortcode/
Maybe i'm not understanding your question but it seems to me that you're using the Gutenberg editor?
... All the features you're looking for are already built-in.
If you want to set the alt="" or title="" attributes you can do it on the right side action column, by clicking on the image itself, while in the Gutenberg editor.
For the figcaption, you can set it right under the image, by clicking on the image itself, while in the Gutenberg editor.

Woocommerce loop title location

Building custom woocommerce template on top of storefront.
I want to replace/change some html for the individual item titles within the loop functionality for the category pages. The "add to cart" link, price, etc. are all located in woocommerce/templates/loop directory. None of the files in that directory appear contain the html for the product title for this functionality.
Where do I find this thing? I swear the most annoying thing about building on woocommerce is trying to find where all the pieces live...
woocommerce/includes/wc-template-functions.php
woocommerce_template_loop_product_title function
function woocommerce_template_loop_product_title() {
echo '<h3 class="woocommerce-loop-product__title">' . get_the_title() . '</h3>';
}
You need change the information in the file functions.php, preferably in the theme child file.
What you put there will normally be rewritten because it have priority.
Like Randy wrote it, this is a pluggable function you will find in
wp-content\plugins\woocommerce\includes\wc-template-functions.php
if ( ! function_exists( 'woocommerce_template_loop_product_title' ) ) {
/**
* Show the product title in the product loop. By default this is an H2.
*/
function woocommerce_template_loop_product_title() {
echo '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</h2>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
To override it, just copy paste the code without the condition in the functions.php file of your child theme and modify it according to your needs.
Ex: Change h2 for h3
function woocommerce_template_loop_product_title() {
echo '<h3 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</h3>';
}

Display Woocommerce Sale Badge with Percentage Saved [duplicate]

On WooCommerce product single pages, if a product owns a sales price, the normal price is crossed out and behind it, the sale price is highlighted.
My question:
How can I add a label like "Old Price: XX Dollar" and "New Price: XX Dollar" instead of only the crossed out and the new price (sale price)?
Update 2 (for simple and variable products + solved the bug on same variations prices)
when products are on sale, you can add custom labels just as you want using a custom function hooked in woocommerce_sale_price_html and woocommerce_variation_sale_price_html filters hooks (for simple and variables products.
For the min / max prices in variables products, we need a different function hooked in woocommerce_variation_sale_price_html filter hook.
Here is that code:
add_filter('woocommerce_variation_sale_price_html','sale_prices_custom_labels', 10, 2 );
add_filter('woocommerce_sale_price_html','sale_prices_custom_labels', 10, 2 );
function sale_prices_custom_labels( $price, $product ){
if (isset($product->sale_price)) {
$price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price( $product->regular_price ). '</del>
<ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price( $product->sale_price ) . '</ins>';
}
else
{
$price = '<ins class="highlight">'.woocommerce_price( $product->regular_price ).'</ins>';
}
return $price;
}
add_filter('woocommerce_variable_sale_price_html', 'sale_prices_custom_labels_min_max', 20, 2);
function sale_prices_custom_labels_min_max( $price, $product) {
$variation_min_reg_price = $product->get_variation_regular_price('min', true);
$variation_max_reg_price = $product->get_variation_regular_price('max', true);
$variation_min_sale_price = $product->get_variation_sale_price('min', true);
$variation_max_sale_price = $product->get_variation_sale_price('max', true);
if ( $variation_min_reg_price != $variation_min_sale_price || $variation_max_reg_price != $variation_max_sale_price )
{
if($variation_min_reg_price == $variation_max_reg_price && $variation_min_sale_price == $variation_max_sale_price ){
$price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price($variation_max_reg_price) . '</del>
<ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price($variation_max_sale_price) . '</ins>';
}
elseif($variation_min_reg_price != $variation_max_reg_price && $variation_min_sale_price == $variation_max_sale_price )
{
$price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price($variation_min_reg_price) . '-' . woocommerce_price($variation_max_reg_price) . '</del>
<ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price($variation_max_sale_price) . '</ins>';
}
elseif($variation_min_reg_price == $variation_max_reg_price && $variation_min_sale_price != $variation_max_sale_price )
{
$price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price($variation_max_reg_price) . '</del>
<ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price($variation_min_sale_price) . '-' . woocommerce_price($variation_max_sale_price) . '</ins>';
}
else
{
$price = '<del class="strike">' . __('Old Price: ', 'woocommerce' ) . woocommerce_price($variation_min_reg_price) . '-' . woocommerce_price($variation_max_reg_price) . '</del>
<ins class="highlight">' . __('New Price: ', 'woocommerce' ) . woocommerce_price($variation_min_sale_price) . '-' . woocommerce_price($variation_max_sale_price) . '</ins>';
}
}
return $price;
}
You can also replace the normal <ins> and <del> html tags by something else and change or add some classes too (if is more convenient for you). At this point everithing is possible.
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.
Related answers: Conditional custom output around products sale price and regular price
In admin side you need to define your sale price and actual price so it will automatically reflect in front side as your old price and new price alternatively.
Also you need to do some code for this.

Problems Adding Woocommerce Bookings info into the Description of Google Calendar

I have synched Woocommerce Bookings with Google Calendar so that employees (with access to a shared Google Calendar) can see instantly when a customer books a trip.
I'm trying to get product add-ons to show up in the description as well. So far the only thing showing up are resources and # of persons. The following code kicks back an error:
Fatal error: Call to a member function get_order_item_totals() on a non-object in /home/content/12/10265512/html/wp-content/plugins/woocommerce-bookings/includes/integrations/class-wc-bookings-google-calendar-integration.php on line 454
I'm just learning my way around PHP out of necessity to get this system to fit our needs. Any help or suggestions would be / are greatly appreciated!
public function sync_booking( $booking_id ) {
$event_id = get_post_meta( $booking_id, '_wc_bookings_gcalendar_event_id', true );
$booking = get_wc_booking( $booking_id );
$api_url = $this->calendars_uri . $this->calendar_id . '/events';
$access_token = $this->get_access_token();
$timezone = wc_booking_get_timezone_string();
$product = $booking->get_product();
$summary = '#' . $booking->id . ' - ' . $product->get_title();
$description = '';
// Add resources in description
if ( $resource = $booking->get_resource() ) {
$description .= __( 'Resource #', 'woocommerce-bookings' ) . $resource->ID . ' - ' . $resource->post_title;
}
// Add ProductAdd on in description testing this
if ( $totals = $order->get_order_item_totals() ) {
$i = 0;
foreach ( $totals as $total ) {
$i++;
if ( $i == 1 ) {
$description .= sprintf($total['label']) . PHP_EOL;
$description .= sprintf($total['value']) . PHP_EOL;
}
}
}
// Add persons in description
if ( $booking->has_persons() ) {
$description .= ( '' != $description ) ? PHP_EOL . PHP_EOL : '';
foreach ( $booking->get_persons() as $id => $qty ) {
if ( 0 === $qty ) {
continue;
}
$person_type = ( 0 < $id ) ? get_the_title( $id ) : __( 'Person(s)', 'woocommerce-bookings' );
$description .= sprintf( __( '%s: %d', 'woocommerce-bookings'), $person_type, $qty ) . PHP_EOL;
$description .= wp_kses_post( $product->post->post_excerpt, array() ) . PHP_EOL;
$description .= sprintf("ADDITIONAL NOTES:") . PHP_EOL;
$description .= sprintf("Hey John passing static notes to Calender test") . PHP_EOL;
}
}
Edit
I tried adding Billing Email and Billing Phone and still cannot get it right. All I see in Google Calendar is Email followed by blank space.
$description .= sprintf( __('Email: %s', 'woocommerce'), $order->billing_email ) . PHP_EOL;
$description .= sprintf( __('Phone: %s', 'woocommerce'), $order->billing_phone ) . PHP_EOL;
the wp_kses_post method in the code up above does return the short description from WooCommerce and places into the Google Calendar description so I will try that next.
I'm still pluggin away at this and after looking at some other websites tried the following
$description .= sprintf( __('%s', 'woocommerce'), $order->email_order_items_table( true, false, true )) . PHP_EOL;
Which is used to pass the same information on in an email to the site admin.
<?php echo $order->email_order_items_table( true, false, true ); ?>
I'm still getting a Fatal Error call to a member function on a non-object. I contacted support at WooCommerce a few days ago. I'll report back here incase someon else is trying to do the same thing as me.
I was able to solve the issue by pulling all order info from the email_orders_table. I'm posting my answer in case someone else is attempting to do the same thing. Customer Support at WooCommerce was unable to help stating it was outside of their support policy and considered custom code.
// Add First Name of Guest
$description .= sprintf( __( 'NAME: %s', 'woocommerce-bookings' ), $booking->get_order()->billing_first_name ) . PHP_EOL;
// Add their email address
$description .= sprintf( __('EMAIL: %s', 'woocommerce-bookings'), $booking->get_order()->billing_email ) . PHP_EOL;
// Add their Phone Number
$description .= sprintf( __('PHONE: %s', 'woocommerce-bookings'), $booking->get_order()->billing_phone ) . PHP_EOL;
// Purchase Notes and WooCommerce Product Add Ons
$description .= sprintf("ADDITIONAL NOTES:") . PHP_EOL;
$description .= sprintf ( __('%s', 'woocommerce-bookings'), strip_tags($order->email_order_items_table( $order->is_download_permitted(), true, true ))) . PHP_EOL ;
This works really well after stripping the HTML tags and all the Guest information is shown in a Private Google Calendar for the entire Staff to Reference. Looking back I certainly worked around in a circle. I had forgotten.
do_action( 'woocommerce_email_after_order_table', $order, true, false );

Resources