Woocommerce override grouped product title - woocommerce

I've seen this on other themes but can't work out how it has been done. In my grouped product list, the title lists as Parent product title --> Child product title. I need it only to list the Child product title.
I can see the code to alter is:
' . $child_product['product']->post->post_title . '
Either the post_title needs to overridden, or the code altered to...what?

(Although this is old, it's still a common question with a prominent google ranking)
Woocommerce defines a filter, woocommerce_product_title, which allows you to pass a product's title through a function that modifies the way it will display.
Add a filter, probably in your theme's functions.php
add_filter('woocommerce_product_title', 'clean_up_title');
This is the function I'm currently using to accomplish this, no promises that it's the best way possible:
function clean_up_title($title){
// check to see if there is an html arrow in the title
if (strpos($title, '→')){
$separator = '→';
}
// otherwise assume it's the character
else {
$separator = '→';
}
// split the title into multiple parts at the arrows
$prog_array = explode($separator, $title);
// get the last part, the actual product name
$prog_name = end($prog_array);
// slice off any leading or trailing whitespace
return trim($prog_name);
}

This would be a bit cleaner to do. Just 'return' the product title rather than 'edit' it.
function clean_product_title($title, $product) {
return $product->post->post_title;;
}
add_filter('woocommerce_product_title', 'clean_product_title', 10, 2);

Related

Filter by Attribute not filtering on shop page

I’ve developed a new theme for a website, but i seem to have a problem related to widget Filter By Attribute.
When i use the filter, that comes with the gutenberg block editor for widgets, the filter attribute widget doesn’t filter anything. (i guess it uses ajax to filter, but its not filtering anything)
When i use the add_filter('use_widgets_block_editor', '__return_false'); to remove the gutenberg and use a new filter by attribute (which is a different widget, than the one that comes with Gutenberg), the widget works, because it filters the query by passing some params on the url, but has a problem. It shows variations that are out of stock, which is something that have been fixed with the new widget, using the product lookup tables, and it's exactly what i'm trying to achieve here.
I strongly believe this is related to the AJAX call it uses to filter the shop page, so i guess i'm missing an ID or Class, to the product wrapper or something.
Does anyone also had this problem?
If you want to see the page in question, is this one https://www.hiima-store.com/bch/shop/
When you go into Filters, you can see both widgets there. (there are two for sizes. the first one uses query params and the second one, is the one that is not filtering).
I did a custom script regarding that.
It might help someone with the same problem.
It will hide the product if the variation is out of stock.
add_action('woocommerce_before_shop_loop_item_title', 'out_of_stock_variations_loop');
function out_of_stock_variations_loop()
{
global $product;
if (isset($_GET["filter_size"])) { // check if the attribute is in the url
if ($product->product_type == 'variable') {
$available = $product->get_available_variations();
if ($available) foreach ($available as $instockvar) {
if (isset($instockvar['attributes']['attribute_pa_' . __('size', 'hiima')])) {
var_dump('entrei');
if (($instockvar['attributes']['attribute_pa_.' . __('size', 'hiima')] == $_GET['filter_size']) && (!$instockvar['max_qty'] > 0)) {
global $product;
$id = $product->get_id();
echo "<style>.post-$id{display: none}</style>";
}
}
}
}
if (!$product->is_in_stock()) {
global $product;
$id = $product->get_id();
echo "<style>.post-$id{display: none}</style>";
}
}
}

Get the title of the original page in your default language and add it as body class to the translated page on WordPress with WPML

Here's my problem:
add_filter( 'body_class', 'wpml_body_class');
function wpml_body_class( $class ) {
global $sitepress, $post;
if( $sitepress->get_default_language() != ICL_LANGUAGE_CODE ) {
$original_id = icl_object_id( $post->ID, get_post_type(), true, $sitepress->get_default_language() );
$class[] = strtolower(get_the_title( $original_id ));
}
return $class;
}
This code works fine. Essentially, I use $sitepress as a global to get my default language and then I extract the ID to match it with get_the_title, so, at the end of the day, I added the title as a class name to the body, so I can easily replicate the style of the original page without adding a line on my CSS stylesheet file on the translated page, in this case in French.
So far so good, except for a caveat:
Since this is the title, if I have a title like Our Team, I have to add a dash to the style, and it is going to change base on how many words I have. If I have to use the URL instead, the process to extract it with WordPress is more complex, so I was wondering if it is possible to add a regular expression to add a dash if I have any space. Or if everyone else knows how to extract the URL instead of get_the_title I couldn't be more grateful.
what you need is sanitize_title_with_dashes() for your purpose :) which is provided by WP . Reference https://codex.wordpress.org/Function_Reference/sanitize_title_with_dashes

Adding a <span> to widget title in Wordpress

I am trying to change the color of the first word in the widget title in wordpress. I am using WP v.3.7.1 and a custom child theme that I created off of the twentythirteen theme.
I just need to wrap the first word in a and can style it from there. I tried to add the following code to the function.php but it only works for one of the three widgets that I have. I have tried other widgets and it doesn't work either.
add_filter ('widget_title', 'wpzoom_fix_widgets');
function wpzoom_fix_widgets($old_title) {
$title = explode(" ", $old_title,2);
$titleNew = "<span>$title[0]</span> $title[1]";
return $titleNew;
}
Any suggestions?
Thank you!
Take a look at the first answer on WordPress Answsers, it looks close to what you are looking for:
add_filter('widget_title', my_title);
function my_title($title) {
// Cut the title to 2 parts
$title_parts = explode(' ', $title, 2);
// Throw first word inside a span
$title = '<span class="my_class">'.$title_parts[0].'</span>';
// Add the remaining words if any
if(isset($title_parts[1]))
$title .= ' '.$title_parts[1];
return $title;
}
Good luck :)

Single Page Navigation Menu Dynamically Generated

hHi all! I have posted this question on the WP support forums, but the community doesn't seem to be as active as stack's, so I am taking a chance here!
I am looking for a plugin that would automatically create a navigation menu (through the use of shortcodes for example) on a long single page documentation page.
The long page is divided into sections. I can imagine using a shortcode at the beginning of every section, and this will create a menu that would be displayed in a sidebar for example (called through a second shortcode perhaps, or a widget)
Any thoughts? Advice?
Thanks!
Use [section]Section Title[/section] shortcodes, then [section_navigation] where you want the navigation links output.
This works, but with a massive caveat -- that [section_navigation] needs to be in your post/page after the other [section] shortcodes... otherwise it generates an empty list.
You should be ok to use it in your theme by putting <?php echo do_shortcode("[section_navigation]");?> in sidebar.php. It will work as long as get_sidebar() is after the_content() in your theme templates (it usually is).
This to go in functions.php
$whit_sections = "";
// [section]My Section Title[/section]
function whit_section_shortcode( $atts, $title = null ) {
// $content is the title you have between your [section] and [/section]
$id = urlencode(strip_tags($title));
// strip_tags removes any formatting (like <em> etc) from the title.
// Then urlencode replaces spaces and so on.
global $whit_sections;
$whit_sections .= '<li>'.$title.'</li>';
return '<span id="'.$id.'">'.$title.'</span>';
}
add_shortcode('section', 'whit_section_shortcode');
// [section_navigation]
function whit_section_navigation_shortcode( $atts, $title = null ) {
global $whit_sections;
return '<ul class="section-navigation">'.$whit_sections.'</ul>';
}
add_shortcode('section_navigation', 'whit_section_navigation_shortcode');

Why wouldn't these theme functions work with Drupal D6?

I'm at my wits end with Drupal 6 right now, and I'm hoping a second pair of eyes might be able to point out some syntax sublety I was missing earlier this morning.
Lets say I have a theme called my_theme.
This is a theme that subthemes from Ginkgo, a theme which which in turn subthemes from
Rubik, whith in tun subthemes from Tao, a fairly common CSS reset theme.
I'm trying to understand how to declare my own theme functions, so I can
clean up the mark up on a fee page I'm workign on for site.
Now my understanding of theming is along the lines of ' if register a
theming function with hook_theme, passing it into an array, with the
function's name, you'be able to call it in future with
theme_function_name from within your theme.
For example, if have a module called my, I'd immplent the hook_theme
like below, defining the name of the function, and defining which
argument should be passed into it:
<?php
function my_theme() {
$items = array();
$items['randomtext'] = array('arguments' => array('element' => NULL));
$items['button_links'] = array('arguments' => array( '$links' => NULL, '$attributes' => NULL));
return $items;
};
Now I have the functions registered, I just need to implement them like
this somewhat contrived example:
function theme_randomtext($element) {
$output = ' <h1>This is some very random text with' . ' this text concatenated: ' . $element . '</h1>';
return $output;
}
I can now use this new function by calling
<php
print theme('randomtext', 'some arbitrary words')
?>
And expect the following content to be returned:
<h1>This is some very random text with this text concatenated: some arbitrary words</h1>
I'm getting nada.
And I don't know why. When I look at the theme registry, I see my
function listed as my_randomtext - I've tried calling both of these
options, in case I should have been adding the theme prefix:
theme('my_randomtext', 'getting desparate');
theme('randomtext', 'really losing my rag now');
Still no luck.
Why might these theme functions not be working? And what advantage does
this give you over simply declaring a function like so in a theme?
function manual_random_text($element) {
$output = ' <h1>This is gives me everything I aleady need,
'without relying on the weird themeing sytem. It's perfect for' . $element . '</h1>';
return $output;
}
Ah, after a bit of distance from the problem, I see the issue.
For this to work, I need to add the prefix of the theme I declare the function in, to make it available to me for use.
So if my module name was my, the desired theme function name would be:
function my_random_text($element) {
$output = ' <h1>,
'This is finally outputting content. Now I can: ' . $element . '</h1>';
return $output;
}
The thing is, if I'm creating an override to a theme function that doesn't already exist, like random_text, there are no functions for it to override anyway.
The term theme override suggests there being a default theming function to override - in many themes, we're not making explicit overrides of functions, but creating alternatives to use in specific cases.
I'm now assuming that I'd need to define this in a module if I wanted to define a function as theme_function_name rather than themename_theme_function_name.
This page here about theming on Drupal.org was a useful quide in finding out some of this info.

Resources