Woocommerce How to get Term id from multiple slugs in css - php

Good Day,
I want to have a custom CSS for certain woo-commerce product category pages, to exclude the sidebar and make the content full width of the page. now I can get the CSS to work on these pages but I have over 450 categories. is there a way to make the term-id dynamic for excluded categories
the script I have so far is as follows
if ( !is_product_category( array( 'cat1','cat3','cat4','cat6') ) ) {
add_action( 'wp_head', function () { ?>
<style>
/*Hide SideBar*/
#primary {
width: 100%!important;
border-right: 0px!important;
}
.term-1 .ast-right-sidebar #secondary,.term-3 .ast-right-sidebar #secondary,.term-4 .ast-right-sidebar #secondary,.term-6 .ast-right-sidebar #secondary{
border-left: 0px!important
}
.term-1.sidebar-main,.term-3 .sidebar-main,.term-4 .sidebar-main,.term-6 .sidebar-main {
display:none!important
}
.term-1 #secondary,.term-3 #secondary,.term-4 #secondary,.term-6 #secondary {
display: none!important;
border-right: 0px solid #eee!important;
}
.term-1 .widget-area .secondary,.term-3 .widget-area .secondary,.term-4 .widget-area .secondary,.term-6 .widget-area .secondary
{
display: none!important;
}
.term-1 .ast-right-sidebar #primary,.term-3 .ast-right-sidebar #primary,.term-4 .ast-right-sidebar #primary,.term-6 .ast-right-sidebar #primary {
border-right: 0px solid #eee!important;
}
</style>
<?php } );
}
I would like to dynamically add the term-id to the CSS so that the CSS is more compact and does not get too long. any suggestions, please

#CBroe thanks for pointing me in the right direction.
The first thing I did is make a code snippet that you can copy and paste into the functions.php page of your child's theme or use plugins like code snippet. so when your theme updates you do not lose any of your code.
the first part that I did was as follows, I added this CSS to the body of the page.
add_filter('body_class', 'add_custom_body_class');
function add_custom_body_class($classes){
if(is_product_category(array( 'cat1','cat3','cat4','cat6'))){
$classes[] = 'your_new_class_here';
}
return $classes;
}
then I create a new snippet where I will add the class into the header now keep in mind that you do not want to have this code on every page so we will also add the is_product_category to the snippet to show code only when we are on that page
if ( !is_product_category( array( 'cat1','cat3','cat4','cat6') ) ) {
add_action( 'wp_head', function () { ?>
<style>
/*Hide SideBar*/
body.your_new_class_here .ast-right-sidebar #secondary { border-left: 0px!important; }
body.your_new_class_here .sidebar-main { display:none!important; }
#primary {
width: 100%!important;
border-right: 0px!important;
}
#secondary {
display: none!important;
border-right: 0px solid #eee!important;
}
</style>
<?php } );
}
What this does is add the CSS classes to the pages that you specified, hope this helped someone else

Related

WooCommerce: Remove thumbnails from gallery but keep slider navigation

I want to remove the thumbnails from the gallery (flexslider) of the product single page.
But I want to keep the arrow for the previous/next images (in case there is more than 1 image).
I found the following code:
add_action( 'woocommerce_product_thumbnails', 'enable_gallery_for_multiple_thumbnails_only', 5 );
function enable_gallery_for_multiple_thumbnails_only() {
global $product;
if( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( get_the_id() );
}
if( empty( $product->get_gallery_image_ids() ) ) {
remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
}
}
Source: https://stackoverflow.com/a/56238267/1788961
The problem is, that the function removes the thumbnail and the arrows.
Is there any way to keep the arrows?
And I know, that I could use display:none or maybe change the template file.
But I'm searching a solution with an own function.
if you want keep only arrow then you just put this code in functions.php:
// for arrow on single product page slide
add_filter( 'woocommerce_single_product_carousel_options', 'sf_update_woo_flexslider_options' );
/**
* Filer WooCommerce Flexslider options - Add Navigation Arrows
*/
function sf_update_woo_flexslider_options( $options ) {
$options['directionNav'] = true;
return $options;
}
And this code put it in your theme style.css file:
/*add for arrow on main image slide*/
ul.flex-direction-nav {
position: absolute;
top: 30%;
z-index: 99999;
width: 100%;
left: 0;
margin: 0;
padding: 0px;
list-style: none;
}
li.flex-nav-prev {float: left;}
li.flex-nav-next {float: right;}
a.flex-next {visibility:hidden;}
a.flex-prev {visibility:hidden;}
a.flex-next::after {
visibility:visible;content: '\f054';
font-family: 'Font Awesome 5 Free';
margin-right: 10px;
font-size: 20px;
font-weight: bold;
}
a.flex-prev::before {
visibility:visible;
content: '\f053';
font-family: 'Font Awesome 5 Free';
margin-left: 10px;
font-size: 20px;
font-weight: bold;
}
ul.flex-direction-nav li a {
color: black;
}
ul.flex-direction-nav li a:hover {
text-decoration: none;
}
.flex-control-nav .flex-control-thumbs{
display: none;
}
add_filter( 'woocommerce_single_product_carousel_options', 'sf_update_woo_flexslider_options' );
/**
* Filer WooCommerce Flexslider options - Add Navigation Arrows
*/
function sf_update_woo_flexslider_options( $options ) {
$options['directionNav'] = true;
$options['controlNav'] = false;
return $options;
}
The given approved answer is close but still resorts to CSS to hide the thumbnails.
You can disable controlNav directly on the same filter you use to show the arrows.

WP + storefront Can´t make top header nav and logo responsive

Hi there and thanks in advance,
NOTE: The site is NSFW, just some ebooks for adults (romance and that stuff, but the ebooks covers may be hot).
My environment:
I have WP with a woocommerce, storefront+bookshop child theme
I have the Snippets plugins where I add all my PHP code
And I add CSS to Custom CSS within customizer
Now the problem:
I can´t figure out how to make site header, top nav bar and logo + buttons responsive.
The fact is that I could get some Top header styling using some PHP and CSS, BUT when I go to mobile view, the Top nav var, header, logo and menu goes crazy!
I need to fix that, I don´t know what to try (I googled a lot, tried changing values, %, float, inline, block, important, whatever) but I can´t get the right tip.
I don{t know if the child theme or storefront itself is overriding my attempts, maybe I have to unhook some function.
Maybe is just as simple as putting all 3 PHP snippets into one div and many spans tag, but I can´t figure out how I am burnt today.
I would thank any advice.
Here goes my CSS
/* Logo size for mobile site */
#media screen and (max-width: 768px) {
.site-header .site-branding img {
max-height: none !important;
max-width: none !important;
width: 322px !important;
}
}
/*Remove title from Shop page*/
.woocommerce-products-header
{display: none;}
/*Hide Pages Tite with custom Class added to PHP snippets*/
.hidetitle .entry-header {
display:none;
}
/*Make primary Pages full width*/
body.woocommerce #primary {
width: 100%;
}
/*Align Search Bar and make text pink*/
#woocommerce-product-search-field-0 {
display:inline-block;
width:100%;
color: #fe00a1;
border: solid 1px #fe00a1;
}
/*Edited Cart*/
#site-header-cart{
padding-bottom:20px;
width:15%;
}
/*Changed cart icon bag f\290 to to cart icon f\217 */
.site-header-cart .cart-contents:after,
.storefront-handheld-footer-bar ul li.cart > a:before {
content: “\f217”;
}
/*Align Help link*/
#help{
display:inline-block;
padding-left: 35px;
padding-right: 35px;
}
/*Align Gift card*/
#gift-cardl{
margin-left:80px;
}
/*Add top padding to rectangle*/
#rectangle {
padding:17px;
}
**PHP snippets**
add_action( ‘storefront_header’, ‘header_custom_gift_button’, 40 );
function header_custom_gift_button() { ?>
<span>
<button id=”gift-cardl”>Gift Card</button>
</span>
<?php
}
add_action( ‘storefront_header’, ‘header_custom_help_link’, 40 );
function header_custom_help_link() { ?>
<span style=”width:100%;”>
Help
</span>
<?php
}
add_action( ‘storefront_header’, ‘header_custom_subscribe_button’, 40 );
function header_custom_subscribe_button() { ?>
<button class=”subscribe_newsletter_btn”>Get Daily Book Bargains</button>
</span>
<?php
}

custom function displaying on all pages

WordPress|storefront|woo-commerce.
i am trying to display header on desktop view and footer on mobile responsive for specific page id 1.but even using specific page id both classes shows on page id 1 header,footer.Tried both id and class with cascading rule of hierarchy.Here id abc displays on page id 1 desktop view and abcd on page id 1 mobile.
My code for child function.php is
add_action( 'storefront_header', 'header_content1', 30 );
function header_content1() { ?>
<div id/class="abc" > my text with css</div><?php
}
add_action( 'storefront_after_footer', 'header_content2', 10 );
function header_content2() { ?>
<div id/class="abcd" > my text with css</div><?php
}
CSS child theme.
.page-id-1 .abc {
text-align: right;
padding-top: 0.02em;
color: #0000ff;
}
.abc{
display:none!important;
}
.abcd{
display:none!important;
}
.page-id-1 .abcd{
display:none!important;
visibility:hidden;
}
#media only screen and (max-width: 599px){
.abc{
display:none!important;
}
.abcd{
display:none!important;
}
.page-id-1 .abc{
display:none !important;
visibility:hidden;
}
.page-id-1 .abcd {
display:block !important;
text-align: center;
padding-top: 0.02em;
color: #0000ff;
}
}
A better approach would be to only load your function on the page that is needed instead of hidding your HTML in all pages but one...
You can check the page ID before running the function with something like this:
if (is_page('1') {
add_action( 'storefront_header', 'header_content1', 30 );
function header_content1() { ?>
<div id/class="abc" > my text with css</div><?php
}
}

Overriding widget via functions.php

Woocommerce comes with a default product filter widget, located in
/wp-content/plugins/woocommerce/includes/widgets/class-wc-widget-layered-nav-filters.php
I have a lot of products/categories etc and using this looks very messy. I have tried to tidy it up and turn it into an accordion menu which seems to work as can be seen from code at bottom.
I am struggling, however, to add it into my theme.
I have created my own widget using:
<?php>
class Custom_WC_Widget_Layered_Nav extends WC_Widget_Layered_Nav {
Public function widget( $args, $instance ) {
//taken details prom default plugin and inserted here
}
?>
<script>
var acc = document.getElementsByClassName("widget-title");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight){
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
}
}
</script>
I have saved this in:
/wp-content/themes/my-theme/woocommerce/includes/widgets/custom-wc-widget-layered-nav.php
I then added the following code into functions.php:
add_action( 'widgets_init', 'err_override_woocommerce_widgets', 15 );
function err_override_woocommerce_widgets() {
if ( class_exists( 'WC_Widget_Layered_Nav' ) ) {
unregister_widget( 'WC_Widget_Layered_Nav' );
include_once( 'wp-content/themes/my-theme/woocommerce/includes/widgets/custom-wc-widget-layered-nav.php' );
register_widget( 'Custom_WC_Widget_Layered_Nav' );
}
}
The new code in the functions.php seems to give errors:
Fatal error: Class 'Custom_WC_Widget_Layered_Nav' not found in /homepages/8/d692312546/htdocs/Mytheme/wp-includes/class-wp-widget-factory.php on line 106
Is there anything I can change to avoid this error?
as you can see below, the coding seems to work and have desired affect.
var acc = document.getElementsByClassName("widget-title");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight){
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
}
}
h1.widget-title {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 5px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;
margin-top:5px;
}
/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
h1.widget-title.active, h1.widget-title:hover {
background-color: #ccc;
}
/* Style the accordion panel. Note: hidden by default */
aside>ul {
padding: 0 18px;
background-color: white;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
}
h1.widget-title:after {
content: '\02795'; /* Unicode character for "plus" sign (+) */
font-size: 13px;
color: #777;
float: right;
margin-left: 5px;
}
h1.widget-title.active:after {
content: "\2796"; /* Unicode character for "minus" sign (-) */
}
#secondary .widget {
font-size: 0.86em;
}
.widget-area > aside {
background: white;
border: 1px solid #ededed;
border-radius: 6px;
padding: 5px 20px;
margin-bottom: 5px;
width:20em
}
.widget-title {
font-size: 1.43em;
}
<div class="widget-area" id="secondary" role="complementary">
<aside class="widget woocommerce widget_layered_nav" id="Woocommerce-layered-nav-1">
<h1 class="widget-title">Title1</h1>
<ul>
<li class="layered-nav-term">
nav term
<span class="count">(10)</span>
</li>
</ul>
</aside>
<aside class="widget woocommerce widget_layered_nav" id="Woocommerce-layered-nav-2">
<h1 class="widget-title">Title2</h1>
<ul>
<li class="layered-nav-term">
nav term2
<span class="count">(10)</span>
</li>
</ul>
</aside>
</div>
According to this article
you have to change the path according to where you saved this widget:
add_action( 'widgets_init', 'err_override_woocommerce_widgets', 15 );
function err_override_woocommerce_widgets() {
// Ensure our parent class exists to avoid fatal error (thanks Wilgert!)
if ( class_exists( 'WC_Widget_Layered_Nav' ) ) {
unregister_widget( 'WC_Widget_Layered_Nav' );
include_once( 'woocommerce/includes/widgets/custom-wc-widget-layered-nav.php' );
register_widget( 'Custom_WC_Widget_Layered_Nav' );
}
}
Hope it works!
UPDATE:
What comes in mind is first save the script inside your js folder and name it something like custom.js then you have to register that script as follow:
// Register your assets during `wp_enqueue_scripts` hook inside `functions.php`.
function custom_register_scripts() {
// Give the path of the script
wp_register_script('js-custom', 'path/to/js/custom.js',array('jquery'));
}
then you can enqueue your script into where your widget is.
public function widget( $args, $instance ) {
// Enqueue needed assets inside the `widget` function.
wp_enqueue_script('js-custom');
// Output widget contents.
}

How I can exclude an php template from if else

I use Wordpress on my website and there is a small section of php code from which I want to exclude a php template.
<?php if (is_user_logged_in() ) { ?>
.content_right { float: right !important; border-right:0px !important; border-left: 1px solid #EAE6E6 ;}
.content_right .shadowblock_out { border-right:0px !important;}
.content_left {float: left !important;}
<?php } else { ?>
.content_right {display:none}
.content_right .shadowblock_out {display:none}
.content_left {float: left !important; width: 100%;}
.box {margin: 1px 4px; float: left; width: 24% !important;}
.box .grido {display: block;height: 133px;margin: 2px 3px 5px;overflow: hidden;width: 210px;border: 1px solid #ebebeb;}
#directory {width: 930px !important;}
<?php }?>
And I want exclude "single-ad_listing.php" template from ELSE, because I want to shpw the sidebar for this template. How I can do that?
Thanks in advance.
You can check if a single page is being displayed and further you can give the post type as parameter to be specific.
if ( is_singular( "ad_listing" ) ) { // here you need to put the post type
// what you want here
} else {
// what you want elsewhere
}

Categories