Woocommerce making sidebar appear at the bottom of the page - wordpress

I installed woocommerce on a non woo theme, I wish to style it, without much change. But when I installed it, the sidebar is displayed out of any container (widgets are shown in their divs but without a wrapper). Now I used
add_action('woocommerce_before_main_content', 'my_theme_wrapper_start', 10);
add_action('woocommerce_after_main_content', 'my_theme_wrapper_end', 10);
function my_theme_wrapper_start() {
echo '<section id="main">';
}
function my_theme_wrapper_end() {
echo '</section>';
}
From the documentation, and this works for woocommerce just fine. But my sidebar is still at the bottom, without a wrapper. Is there a way to wrap the sidebar as well, and place it next the content of the shop, using just hooks, not copying any files out of the plugin?

I figured it out, this is a 'framework' that works for me, in case anybody needs it:
<?php
if (!function_exists('custom_open_woocommerce_content_wrappers')) {
function custom_open_woocommerce_content_wrappers(){
echo '<div class="container shop_container"><div class="row">';
}
}
if (!function_exists('custom_close_woocommerce_content_wrappers')) {
function custom_close_woocommerce_content_wrappers(){
echo '</div></div>';
}
}
if (!function_exists('custom_product_wrapper_open')) {
function custom_product_wrapper_open(){
echo '<div class="span8 content_with_right_sidebar">';
}
}
if (!function_exists('custom_product_wrapper_close')) {
function custom_product_wrapper_close(){
echo '</div>';
}
}
if (!function_exists('custom_before_shop_loop_sidebar')) {
function custom_before_shop_loop_sidebar() {
echo '<aside class="span4 sidebar sidebar_right">';
dynamic_sidebar(get_theme_mod('shop_sidebar', ''));
echo '</aside>';
}
}
add_action( 'woocommerce_after_shop_loop', 'custom_before_shop_loop_sidebar', 20);
if (!function_exists('custom_prepare_woocommerce_wrappers')) {
function custom_prepare_woocommerce_wrappers(){
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_output_content_wrapper', 10);
remove_action( 'woocommerce_after_shop_loop', 'woocommerce_output_content_wrapper_end', 10);
add_action( 'woocommerce_before_main_content', 'custom_open_woocommerce_content_wrappers', 10 );
add_action( 'woocommerce_after_main_content', 'custom_close_woocommerce_content_wrappers', 10 );
add_action( 'woocommerce_before_shop_loop', 'custom_product_wrapper_open', 10 );
add_action( 'woocommerce_after_shop_loop', 'custom_product_wrapper_close', 10 );
}
}
add_action( 'wp_head', 'custom_prepare_woocommerce_wrappers' );
This will create a wrapper with a right sidebar. You can customize it further if you need to. Hope it helps.

Related

How to remove gutenberg_render_title_tag from theme function file?

I have try remove action but not working.
add_action( 'wp_loaded', 'remove_my_action' );
function remove_my_action() {
remove_action( 'wp_head', 'gutenberg_render_title_tag', -1 );
}
or
add_action( 'init', 'remove_my_action' );
function remove_my_action() {
remove_action( 'wp_head', 'gutenberg_render_title_tag', -1 );
}
i am using yoast plugin. Yoast plugin also using remove action https://github.com/Yoast/wordpress-seo/blob/trunk/src/integrations/front-end-integration.php#L220 but it's not working. Still tag coming two time. i wants to remove gutenberg title tag.
I got the solution for this.
add_action( 'wp', 'remove_default_guten_title' );
function remove_default_guten_title() {
foreach ( gutenberg_get_template_type_slugs() as $template_type ) {
add_filter( str_replace( '-', '', $template_type ) . '_template', 'remove_default_title', 21, 3 );
}
}
function remove_default_title() {
remove_action( 'wp_head', 'gutenberg_render_title_tag', 1 );
return gutenberg_dir_path() . 'lib/template-canvas.php';
}

WooCommerce Shop page thumbnail wrap

I am looking at this: https://www.businessbloomer.com/woocommerce-visual-hook-guide-archiveshopcat-page/
I would like to wrap thumb in shop page into another div.
If I use following code I could achieve something, but the problem is many themes remove or dont use woocommerce_before_shop_loop_item_title and use some different code and I cannot effectively do my action.
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
add_action( 'woocommerce_before_shop_loop_item_title', 'my_woocommerce_before_shop_loop_item_title', 10 );
function my_woocommerce_before_shop_loop_item_title() {
global $product;
$post_id = $product->get_id();
$html = woocommerce_get_product_thumbnail('woocommerce_thumbnail');
echo '<div class="my">' . $html . '</div>';
}
Is there action or hook that processes product thumbnail to which I could do my own action? Regardless of what is happening before and after that?
Please try to implement in this way.
//replace the functionprefix with your functions prefix
function functionprefix_wrap_loop_product_image() {
if ( ! class_exists( 'WooCommerce' ) ) return; //* exit if WooCommerce not active
add_action( 'woocommerce_before_shop_loop_item_title' , 'functionprefix_product_loop_image_wrapper_open', 10 );
add_action( 'woocommerce_shop_loop_item_title' , 'functionprefix_product_loop_image_wrapper_close', 10 );
}
add_action( 'woocommerce_before_shop_loop' , 'functionprefix_wrap_loop_product_image', 3 );
//replace the functionprefix with your functions prefix
function functionprefix_product_loop_image_wrapper_open() {
echo '<div class="image-wraper">';
}
function functionprefix_product_loop_image_wrapper_close() {
echo '</div>';
}

Remove Contact Form 7 CSS and JS Unless Contact form 7 shortcode is used in the page

I want to show the css and javascript only when the shortcode is used in that page. If the short code not present in the wordpress page then the js and css of contact form should not be shown. For that what i have done is i have pasted the following code in my active themes function.php file.
add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );
The above code totally removes the js and css of contact form 7 plugin. What i need is if contact form 7 shortcode is pasted then both should be shown.
Here is the answer for your question. If there is not shortcode the css and js of contact form will be removed and if there is shortcode css and js will be added.
function rjs_lwp_contactform_css_js() {
global $post;
if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'contact-form-7') ) {
wp_enqueue_script('contact-form-7');
wp_enqueue_style('contact-form-7');
}else{
wp_dequeue_script( 'contact-form-7' );
wp_dequeue_style( 'contact-form-7' );
}
}
add_action( 'wp_enqueue_scripts', 'rjs_lwp_contactform_css_js');
I needed the other version that corresponds widgets and shortcodes in theme file.
add_filter( 'wpcf7_load_css', '__return_false' );
add_filter( 'wpcf7_load_js', '__return_false' );
remove_action( 'wp_enqueue_scripts','wpcf7_recaptcha_enqueue_scripts', 20 );
add_filter('pre_do_shortcode_tag', 'enqueue_wpcf7_css_js_as_needed', 10, 2 );
function enqueue_wpcf7_css_js_as_needed ( $output, $shortcode ) {
if ( 'contact-form-7' == $shortcode ) {
wpcf7_recaptcha_enqueue_scripts();
wpcf7_enqueue_scripts();
wpcf7_enqueue_styles();
}
return $output;
}
You use below code.You can add your pages Id in this code.
function dvk_dequeue_scripts() {
$load_scripts = false;
if( is_singular() ) {
$post = get_post();
if( has_shortcode($post->post_content, 'contact-form-7') ) {
$load_scripts = true;
}
}
if( ! $load_scripts ) {
wp_dequeue_script( 'contact-form-7' );
wp_dequeue_style( 'contact-form-7' );
}
}
add_action( 'wp_enqueue_scripts', 'dvk_dequeue_scripts', 99 );
Reference

How to remove WooCommerce shop images?

I'm trying for few hours now to disable the images from my woocommerce shop but couldn't find any solution yet, here's a screenshot of how the shop page (not product page) is looking now and what I want to remove:
https://imgur.com/XEELei1
I've already tried adding:
// Remove product images from the shop loop
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
// Remove sale badges from the shop loop
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
In functions.php, but that image is still there.
Thank you.
Try below code if it is shop page
function before_imageless_product() {
if( !has_post_thumbnail( get_the_id() ) ){
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
echo '<div class="no-image">';
}
}
add_action( 'woocommerce_before_shop_loop_item', 'before_imageless_product', 9 );
function after_imageless_product() {
if( !has_post_thumbnail( get_the_id() ) ){
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
echo '</div>';
}
}
add_action( 'woocommerce_after_shop_loop_item', 'after_imageless_product', 9 );
This solved the problem:
.products .box-image {display:none;}
.products .product-small .box-text {display:flex!important; }
.products .product-small .price-wrapper {margin:20px;}

How does one replace WooCommerce default placeholder image for shop categories

I've tried the following code, which im using successfully with product thumbnails but for categories haven't found a way to do it.
add_action( 'init', 'mw_replace_woocommerce_placeholders' );
function mw_replace_woocommerce_placeholders() {
add_filter('woocommerce_placeholder_img', 'custom_woocommerce_placeholder_img');
function custom_woocommerce_placeholder_img( $src ) {
$src = '<span class="thumb-placeholder"><i class="icon-camera"></i></span>';
return $src;
}
}
Any tips?
This should work
add_action( 'init', 'custom_placeholder' );
function custom_placeholder() {
if ( is_product_category() ){
add_filter('woocommerce_placeholder_img_src','custom_placeholder_img');
function custom_placeholder_img($src) {
$upload_dir = wp_upload_dir();
$uploads = untrailingslashit( $upload_dir['baseurl'] );
$src = $uploads . '/your/directory/custom_placeholder.jpg';
return $src;
}
}
}
Or you could replace the placeholder located at "wp-content/plugins/woocommerce/assets/images/placeholder.png" with your own.
The line:
if ( is_product_category() ){
in functions.php makes the theme reliant on the plugin, so if you deactivate woocommerce for testing, it breaks the site. At least that's what happened to me.

Resources