add class to widget_shopping_cart_content in woocommerce - wordpress

I want to add class to widget_shopping_cart_content in WooCommerce without JS and Jquery. I want to filter fragment in function file.
JS and jquery are not working on this file.
I have found this code in class-wc-ajax.php file and I want to modify this code.
public static function get_refreshed_fragments() {
ob_start();
woocommerce_mini_cart();
$mini_cart = ob_get_clean();
$data = array(
'fragments' => apply_filters( 'woocommerce_add_to_cart_fragments', array(
'div.widget_shopping_cart_content' => '<div class="widget_shopping_cart_content">' . $mini_cart . '</div>',
)
),
'cart_hash' => apply_filters( 'woocommerce_add_to_cart_hash', WC()->cart->get_cart_for_session() ? md5( json_encode( WC()->cart->get_cart_for_session() ) ) : '', WC()->cart->get_cart_for_session() ),
);
wp_send_json( $data );
}
I want to add dropdown-menu class in this <div class="widget_shopping_cart_content dropdown-menu">' . $mini_cart . '</div>'; but how can I didn't know.
Can please any one help me.

Option 1: str_replace();
add_filter('woocommerce_add_to_cart_fragments', 'mm_string_replace_add_to_cart_fragments');
function mm_string_replace_add_to_cart_fragments($fragments){
return $fragments['div.widget_shopping_cart_content'] = str_replace('class="widget_shopping_cart_content"', 'class="widget_shopping_cart_content dropdown-menu"', $fragments['div.widget_shopping_cart_content']);
}
This does not require additional call to woocommerce_mini_cart() and is much faster but less flexible then other method mentioned below.
Option 2: Overwrite the get_refreshed_fragments function
add_filter('woocommerce_add_to_cart_fragments', 'mm_change_add_to_cart_fragments');
function mm_change_add_to_cart_fragments(){
ob_start();
woocommerce_mini_cart();
$mini_cart = ob_get_clean();
return array(
'div.widget_shopping_cart_content' => '<div class="widget_shopping_cart_content dropdown-menu">' . $mini_cart . '</div>',
)
}
Essentially we are recalling the entire function but with some modifications. So first run on get_refreshed_fragments goes to waste. This is not ideal solution but this is what we have at hand. Use this if you want to modify the html in more detail.

Related

Add different WordPress excerpt formats to different templates

I added the following code to my functions.php file in WordPress 6.1.1 to display excerpts.
function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
function new_excerpt_more($more) {
return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');
...but I also have a use case to show the full excerpt without a read more link.
On page template 1 I add the below code to display the excerpt:
<?php echo the_excerpt(); ?>
...and it displays the excerpt as per the functions.php file but how do I create a 2nd excerpt without the read more link and apply it to page template 2?
Is there a parameter I can use within the_excerpt(parameter); or can I use something like wp_trim_excerpt https://developer.wordpress.org/reference/functions/wp_trim_excerpt/ maybe?
I came across the below code that is supposed to do what I want
function wpex_get_excerpt( $args = array() ) {
// Default arguments.
$defaults = array(
'post' => '',
'length' => 40,
'readmore' => false,
'readmore_text' => esc_html__( 'read more', 'text-domain' ),
'readmore_after' => '',
'custom_excerpts' => true,
'disable_more' => false,
);
// Apply filters to allow child themes mods.
$args = apply_filters( 'wpex_excerpt_defaults', $defaults );
// Parse arguments, takes the function arguments and combines them with the defaults.
$args = wp_parse_args( $args, $defaults );
// Apply filters to allow child themes mods.
$args = apply_filters( 'wpex_excerpt_args', $args );
// Extract arguments to make it easier to use below.
extract( $args );
// Get the current post.
$post = get_post( $post );
// Get the current post id.
$post_id = $post->ID;
// Check for custom excerpts.
if ( $custom_excerpts && has_excerpt( $post_id ) ) {
$output = $post->post_excerpt;
}
// No custom excerpt...so lets generate one.
else {
// Create the readmore link.
$readmore_link = '' . $readmore_text . $readmore_after . '';
// Check for more tag and return content if it exists.
if ( ! $disable_more && strpos( $post->post_content, '<!--more-->' ) ) {
$output = apply_filters( 'the_content', get_the_content( $readmore_text . $readmore_after ) );
}
// No more tag defined so generate excerpt using wp_trim_words.
else {
// Generate an excerpt from the post content.
$output = wp_trim_words( strip_shortcodes( $post->post_content ), $length );
// Add the readmore text to the excerpt if enabled.
if ( $readmore ) {
$output .= apply_filters( 'wpex_readmore_link', $readmore_link );
}
}
}
// Apply filters and return the excerpt.
return apply_filters( 'wpex_excerpt', $output );
}
Output using:
<?php echo wpex_get_excerpt ( $defaults = array(
'length' => 40,
'readmore' => true,
'readmore_text' => esc_html__( 'read more', 'wpex-boutique' ),
'custom_excerpts' => true,
) ); ?>
...but doesn't seem to workas intended. Outputs the excerpt with no link but the args don't see to work when changed. Would be perfect for my use otherwise. Maybe someone sees how to fix this code?
Thanks

Media Modal Gutenberg missing styles

So today, all my websites were updated to the new release of WordPress 5.9.1. Good good. However, my custom blocks in Gutenberg that are containing an image element are breaking the style of the media modal (where you can add an image directly in the post).
I started a new project, just to test if it was my theme, or the plugins, but even without any plugin (except ACF Pro) and on the Twenty Twenty-Two theme, if I add my registration code in the functions.php file of 2022 theme, I get the same problem.
Here's the register-block code:
add_action('acf/init', 'my_acf_init_block_types');
function my_acf_init_block_types() {
if( function_exists('acf_register_block_type') ) {
acf_register_block_type(array(
'name' => 'carousel',
'title' => __('Carrousel'),
'description' => __(''),
'render_template' => 'web/blocks/carousel.php',
'category' => 'custom-blocks',
'icon' => 'images-alt',
'keywords' => array( 'carousel', 'carrousel'),
'supports' => array( 'anchor' => true),
));
}
}
And I've created a Field group trying the image with the array annnnnd the one using only the URL.
What I tried:
no plugins (except ACF)
WP theme (2022)
custom theme with no functions
adding the registration code to 2022 theme (same error)
Please, help a sister our here.
I think it was cause by the 5.9.1 update
You can use this in functions.php as temporary fix
function fix_media_views_css() {
echo '<link rel="stylesheet" id="fix-media-views-css" href="'.get_bloginfo('url').'/wp-includes/css/media-views.min.css?ver=5.9.1" media="all">';
}
add_action('admin_footer', 'fix_media_views_css');
I've added that piece of code to my functions.php file (at the end, no biggy).
function acf_filter_rest_api_preload_paths( $preload_paths ) {
if ( ! get_the_ID() ) {
return $preload_paths;
}
$remove_path = '/wp/v2/' . get_post_type() . 's/' . get_the_ID() . '?context=edit';
$v1 = array_filter(
$preload_paths,
function( $url ) use ( $remove_path ) {
return $url !== $remove_path;
}
);
$remove_path = '/wp/v2/' . get_post_type() . 's/' . get_the_ID() . '/autosaves?context=edit';
return array_filter(
$v1,
function( $url ) use ( $remove_path ) {
return $url !== $remove_path;
}
);
}
add_filter( 'block_editor_rest_api_preload_paths', 'acf_filter_rest_api_preload_paths', 10, 1 );
It works perfectly like before. I've tried to downversion it to 5.9 and it worked as well, but it takes more time/effort and many mistakes can happen.
Hope it helps more than one.
ACF is aware of the issue: https://github.com/AdvancedCustomFields/acf/issues/612
Here is the temp fix, paste in your functions.php:
function acf_filter_rest_api_preload_paths( $preload_paths ) {
global $post;
$rest_path = rest_get_route_for_post( $post );
$remove_paths = array(
add_query_arg( 'context', 'edit', $rest_path ),
sprintf( '%s/autosaves?context=edit', $rest_path ),
);
return array_filter(
$preload_paths,
function( $url ) use ( $remove_paths ) {
return ! in_array( $url, $remove_paths, true );
}
);
}
add_filter( 'block_editor_rest_api_preload_paths', 'acf_filter_rest_api_preload_paths', 10, 1 );

How to add extra css class to comment-reply-link generated by comment_reply_link function ?

So, here is the problem in adding css class in WordPress comment reply section
comment_reply_link function adds css class comment-reply-link in reply button. Is there as way I can add new css class to comment-reply-link ?
I know that I can do that using jquery but is there any way of doing so without using jquery ?
With this, I add class to comment-reply-link
<?php
$myclass = 'icon-share-alt';
echo preg_replace( '/comment-reply-link/', 'comment-reply-link ' . $myclass,
get_comment_reply_link(array_merge( $args, array(
'add_below' => $add_below,
'depth' => $depth,
'max_depth' => $args['max_depth']))), 1 );
?>
Screenshot:
You can add a filter to the comment_reply_link function.
function comment_reply_link_filter($content){
return '<div class="yourclass">' . $content . '</div>';
}
add_filter('comment_reply_link', 'comment_reply_link_filter', 99);
I know it's not directly on the element, but you can now style the element with .yourclass > .comment-reply-link.
Combining a couple of the other answers
function custom_comment_reply_link($content) {
$extra_classes = 'button button--small button--white';
return preg_replace( '/comment-reply-link/', 'comment-reply-link ' . $extra_classes, $content);
}
add_filter('comment_reply_link', 'custom_comment_reply_link', 99);
This adds the value of $extra_classes to all reply links 'class' attribute.

Shortcode displaying strangely

I'm trying to get a shortcode to add an id to a div, and that part works fine, but it adds some extra stuff and I'm not sure if it's because I did something wrong, or if it's something else. Here is the actual shortcode:
function jump_function($params){
//Extract parameters and supply default values
extract( shortcode_atts( array(
'id' => ''
), $params ) );
//The parameters are now stored as variables
return do_shortcode('<div id="' . $id . '"</div>');
}
add_shortcode( 'jump', 'jump_function' );
I'm trying to get it to display as
<div id="id-here"></div>
but on the page it's displaying as this:
<div div="" <="" id="id-here" style="position: relative;"><div></div></div>
Did I do anything wrong?
do_shortcode doesn't work like this...
you don't need it.
function jump_function($params){
//Extract parameters and supply default values
extract( shortcode_atts( array(
'id' => ''
), $params ) );
//The parameters are now stored as variables
return '<div id="' . $id . '"</div>';
}
add_shortcode( 'jump', 'jump_function' );
http://codex.wordpress.org/do_shortcode

Drupal, Ubercart, products templates (prices templates)

I want to change the template of my Ubercart products. More in detail, I want to add the label "From..." in front of each price. But I cannot find the template to do it.
I'm also using theme developer module and this is what I get:
zen_uc_product_price <
phptemplate_uc_product_price <
theme_uc_product_price
But I cannot find such files.
thanks
You can also make a node-product.tpl.php file, if so this is the way to get prices:
<?php
$context = array(
'type' => 'product',
'revision' => 'altered', // using altered to get the bare price with no themeing
'field' => 'sell_price',
'subject' => array('node' => $node)
);
$dp = uc_price($node->sell_price, $context);
$context['field'] = 'list_price';
$lp = uc_price($node->list_price, $context);
?>
<div class="price clear-block <?php if($dp != $lp) print 'discounted'; ?>">
<?php print 'From: ' . $node->content['display_price']['#value']; ?>
<?php //print $node->content['list_price']['#value']; ?>
</div>
This became a bit more than it should be:
Use:
content['display_price']['#value']; ?>
Unless you want to theme discounted pricing :-)
Just copied out from one of my projects.
Last: you can probably use theme_uc_product_price:
you add a function in template.php (Pasting in default implementation from uc_product.module
function zen_uc_product_price($price, $context, $options = array()) {
$output = '<div class="product-info '. implode(' ', (array)$context['class']) .'">';
$output .= uc_price($price, $context, $options);
$output .= '</div>';
return $output;
}
Inspect the $context variable for when to add the "From" part.
To add a prefix or suffix to the price, you can go to
/admin/store/settings/store/edit/format

Resources