Is it possible to implement the same thing than the jQuery code below in PHP?
I use it to hide errors made during importing products from wholesaler.
Here is the jQuery code I use:
jQuery(document).ready(function( $ ){
$('.woocommerce-product-details__short-description:has(p:nth-of-type(2)) p').addClass('ukryj-klase-krotki');
});
How does it work:
jQuery checks if in product short description is more than one <p>
element. If there is more – it adds class ‘ukryj-klase-krotki’ to all <p>
elements. Then I add CSS class to hide first <p>
element generated in product short description so the only one I want is visible:
CSS code:
.woocommerce-product-details__short-description .ukryj-klase-krotki{
display: none;
}
.woocommerce-product-details__short-description .ukryj-klase-krotki ~ .ukryj-klase-krotki{
display: block;
}
Any help?
2
Answers
If you want to install a plugin to add the script look here: https://it.wordpress.org/plugins/search/header+and+footer+scripts/.
If you want to use PHP code instead you need to add the following code inside the
functions.php
file of your active theme (if I use it in the child theme):As suggested by the comments above you should fix the problem, as soon as possible, by changing the incorrect product data.
First read Template structure & Overriding templates via a theme to understand how you can override WooCommerce template files via your active child theme.
The template to be overridden is
single-product/short-description.php
Once copied to your chid theme in "woocommerce" folder > "single-product" subfolder, open/edit
short-description.php
file, and replace with:Here, using
substr_count()
php function, we count the number of</p>
(closing tags) from product short description, to addukryj-klase-krotki
as class selector to all<p>
children tags usingstr_replace()
function. Tested and works.