Add a CSS class to an item when Woocommerce cart is empty - php

I am trying to hide my cart when it is empty, so I decided to add a CSS class to the cart HTML item when the cart is empty, here is my current code:
function x_woocommerce_navbar_menu_item( $items, $args ) {
if (WC()->cart->cart_contents_count == 0 ) {
echo '<script type="text/javascript">
$(document).ready(function() {
$("#header_cart").addClass("zero");
});
</script>';
}
I am adding this to my functionts.php file
Am I missing anything?

It will be good if you add the class to body to make the hierarchy in CSS. Use the following code in functions.php :
function tristup_body_classes( $classes )
{
global $woocommerce;
if( is_cart() && WC()->cart->cart_contents_count == 0){
$classes[]='empty-cart';
}
return $classes;
}
add_filter( 'body_class', 'tristup_body_classes' );
This code will add a class "empty-cart" to body.
Hope this will solve your problem.

I am posting here a solution which bases on Tristup answer, as his answer is not 100% correct which observed Yahya Hussein.
add_filter( 'body_class','shoppingCartNotEmpty' );
function shoppingCartNotEmpty( $classes ) {
$classes[] = WC()->cart->get_cart_contents_count() ? 'shopping-cart-not-empty'
: 'shopping-cart-empty';
return $classes;
}
When the shopping cart is empty then the CSS class shopping-cart-empty is created, otherwise shopping-cart-not-empty is available.

Please put this code in functions.php file.
function cart_empty_add_class() {
global $woocommerce;
if ( empty($woocommerce->cart->cart_contents) ) {
echo '<script type="text/javascript">
$(document).ready(function() {
$("#header_cart").addClass("zero");
});
</script>';
exit;
}
}
add_action( 'wp_head', 'cart_empty_add_class' );

Related

Error adding standard WooCommerce product widget to blog post

I am using a code that changes the text and style of the Add to Cart button for a product added to the cart. (All CSS styles for my theme)
/* Change the text of the add to cart button for the archive and categories */
add_filter( 'woocommerce_product_add_to_cart_text', 'new_products_button_text', 20, 2 );
function new_products_button_text( $text, $product ) {
if(
$product->is_type( 'simple' )
&& $product->is_purchasable()
&& $product->is_in_stock()
&& WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product->get_id() ) )
) {
$text = 'Product in Cart';
}
return $text;
}
/* Change the add to cart button text for the product page */
add_filter( 'woocommerce_product_single_add_to_cart_text', 'new_single_product_button_text' );
function new_single_product_button_text( $text ) {
if( WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( get_the_ID() ) ) ) {
$text = 'Product in Cart';
}
return $text;
}
add_action( 'wp_footer', 'action_wp_footer' );
function action_wp_footer() {
?>
<script>
jQuery(document).ready(function($) {
var selector = '.add_to_cart_text:contains("Product in Cart")';
// Selector contains specific text
if ( $( selector ).length > 0 ) {
$( selector ).addClass( 'product-is-added' );
} else {
$( selector ).removeClass( 'product-is-added' );
}
});
</script>
<?php
}
add_action( 'wp_footer', 'ajax_button_text_js_script' );
function ajax_button_text_js_script() {
$text = __('Product in Cart', 'woocommerce');
?>
<script>
jQuery(function($) {
var text = '<?php echo $text; ?>', $this;
$(document.body).on('click', '.ajax_add_to_cart', function(event){
$this = $(this); // Get button jQuery Object and set it in a variable
});
$(document.body).on('added_to_cart', function(event,b,data){
var buttonText = '<span class="add_to_cart_text product-is-added">'+text+'</span><i class="cart-icon pe-7s-cart"></i>';
// Change inner button html (with text) and Change "data-tip" attribute value
$this.html(buttonText).attr('data-tip',text);
});
});
</script>
<?php
}
The code works correctly, but there was a problem with the blogs. In blog posts, I add a standard WooCommerce product widget. I use WPBakery Page Builder, the visual editor.
When publishing or saving a post with a standard WooCommerce widget, I get a critical error:
Fatal error: Uncaught Error: Call to a member function find_product_in_cart() on null in /public_html/wp-content/themes/functions.php:703 Stack trace: #0 /public_html/wp-includes/class-wp-hook.php(292): new_products_button_text('\xD0\x92 \xD0\xBA\xD0\xBE\xD1\x80\xD0\xB7\xD0\xB8\xD0\xBD...', Object(WC_Product_Simple)) #1 /public_html/wp-includes/plugin.php(212): WP_Hook->apply_filters('\xD0\x92 \xD0\xBA\xD0\xBE\xD1\x80\xD0\xB7\xD0\xB8\xD0\xBD...', Array) #2 /public_html/wp-content/plugins/woocommerce/includes/class-wc-product-simple.php(62): apply_filters('woocommerce_pro...', '\xD0\x92 \xD0\xBA\xD0\xBE\xD1\x80\xD0\xB7\xD0\xB8\xD0\xBD...', Object(WC_Product_Simple)) #3 /public_html/wp-content/themes/cores/nasa-woo-functions.php(393): WC_Product_Simple->add_to_cart_text() #4 /public_html/wp-content/themes/cores/nasa-woo- in /public_html/wp-content/themes/functions.php on line 703
Without a product widget, the post is published without any problems.
Line 703 is part of the above code:
&& WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product->get_id() ) )
As I understand it, the cart is not initialized in the admin panel, since it is not needed here.
How can this be fixed?
I will be glad for your help!
In exceptional cases or due to the combination of plugins/themes you can indeed run into error messages.
A solution for this could be to add multiple checks before executing the next piece of code, in this way you can prevent error messages.
For example, you can replace your existing new_products_button_text callback function with:
function new_products_button_text( $text, $product ) {
if ( is_admin() ) return $text;
if ( $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() ) {
// WC Cart
if ( WC()->cart ) {
// Get cart
$cart = WC()->cart;
// If cart is NOT empty
if ( ! $cart->is_empty() ) {
// Find product in cart
if ( $cart->find_product_in_cart( $cart->generate_cart_id( $product->get_id() ) ) ) {
$text = 'Product in Cart';
}
}
}
}
return $text;
}
This can be written more compactly, but by spreading the conditions over multiple if statements, you can quickly determine where things are going wrong, if you still receive an error message.

Remove shipping row from Woocommerce admin edit order pages

How can I hide/remove shipping row from admin order page? Please help, Thanks in advance.
To hide shipping lines and details a from admin order single pages you will use the following:
add_filter( 'woocommerce_order_get_items', 'custom_order_get_items', 10, 3 );
function custom_order_get_items( $items, $order, $types ) {
if ( is_admin() && $types == array('shipping') ) {
$items = array();
}
return $items;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Maybe have a look at this?
https://wordpress.org/plugins/hide-woocommerce-product-shipping-information/
It removes the shipping info from products so maybe also from the whole site.
Add this into your function file
<?php add_action( 'init', 'hide_shipping_details' );
function hide_shipping_details() {
global $pagenow;
if( is_admin() && $pagenow == "user-edit.php") { ?>
<style> #fieldset-shipping{ display: none !important } </style>
<?php } }
you can change css as per your requirement
Try this
add_action('admin_footer', 'my_custom_script');
function my_custom_script() {
?>
<script>
jQuery(document).ready(function(){
jQuery(".wc-order-totals .label:contains('Shipping')").parent().hide();
});
</script>
<?php
}

Disable Jetpack Carousel on specific pages in WordPress

I'm trying to disable Jetpack Carousel on a specific post ID using the following code in my functions.php
function djcoh_disable_carousel( $value ) {
wp_reset_query();
if ( is_page( 614 ) ) {
$value = true; // true to disable Carousel
}
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
Here's the reference for jp_carousel_maybe_disable on GitHub
It seems that I'm unable to use is_page() within functions.php - though I thought I'd be able to by using wp_reset_query() as mentioned in the codex
What am I missing?!
The code you have is from a tutorial which is intended for running as a simple plugin. The reason your code doesn't currently work is because you are using it in the functions.php.
In it's current form your function is called as soon as it is read as part of the functions.php file. This is usually some time before the page is formed, and so you can't grab the page id with is_page{}.
Instead you should query the page and get it's id as follows:
function djcoh_disable_carousel( $value ) {
//get the global
global $post
echo "TEST PAGE ID: ".$post->ID;
//wp_reset_query();
if ( $post->ID == 614 ) {
$value = true; // true to disable Carousel
}
wp_reset_query();
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
if that doesn't work try this:
function djcoh_disable_carousel( $value ) {
//get the global
global $wp_query;
$post_ID = $wp_query->post->ID;
echo "TEST PAGE ID: ". $post_ID;
//wp_reset_query();
if ( $post_ID == 614 ) {
$value = true; // true to disable Carousel
}
wp_reset_query();
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
If none of the above work then your script is being called far too early in the process to grab the page id. So, the easiest option would be to simply place this script in it's own .php file and then upload that to the plugins root folder. Then activate it from the plugins menu.
The final option would be to create this as a filter or script and add the function call in the actual page template.
I managed this by using REQUEST_URI within a plugin file:
<?php
// No direct access
if ( ! defined( 'ABSPATH' ) ) exit;
if ( $_SERVER["REQUEST_URI"] === '/PAGE-SLUG/' ) {
add_filter( 'jp_carousel_maybe_disable', '__return_true' );
}
Change PAGE-SLUG for your slug and you are all set.
You can find info on REQUEST_URI in PHP's manuals:
'REQUEST_URI'
The URI which was given in order to access this page; for instance, '/index.html'.
It seems simplest to conditionally dequeue the Jetpack carousel script and stylesheet. The conditionals that you would typically use to control output would be available at the point in the request when the wp_footer action fires.
add_action( 'wp_footer', function() {
if ( is_page( $page ) ) {
wp_dequeue_script( 'jetpack-carousel' );
wp_dequeue_style( 'jetpack-carousel' );
}
}
Be certain to modify the is_page function to include the $page parameter or the condition will match all pages. Place the code in your theme's functions.php file and the Jetpack carousel should be disabled.

Woocommerce Creating a custom variable product type

I'm trying to create a custom variable product type from woocommerce for a booking form, which has different pricing based on a set of variables.
I've successfully added a custom product type. But how do i duplicate the same option that the variable product has for different attribute pricing. Can't seem to find any resources related with variable product types.
// add a product type
add_filter( 'product_type_selector', 'cpt_add_custom_product_type' );
function cpt_add_custom_product_type( $types ){
$types[ 'booking_product' ] = __( 'Booking Product' );
return $types;
}
add_action( 'plugins_loaded', 'cpt_create_custom_product_type' );
function cpt_create_custom_product_type(){
// declare the product class
class WC_Product_Wdm extends WC_Product_Variable {
public function __construct( $product ) {
$this->product_type = 'booking_product';
parent::__construct( $product );
// add additional functions here
}
}
}
I've managed to get part way - by adding back in the variation and attribute tabs. Next step is to add back in "Add as a Variation" option.
add_filter('woocommerce_product_data_tabs', function($tabs) {
array_push($tabs['attribute']['class'], 'show_if_variable show_if_membership');
array_push($tabs['variations']['class'], 'show_if_membership');
return $tabs;
}, 10, 1);
Dan Needham's answer is the first step you should follow and after that you should use this
function producttype_custom_js() {
if ( 'product' != get_post_type() ) :
return;
endif;
?><script type='text/javascript'>
jQuery( document ).ready( function() {
jQuery( '.enable_variation' ).addClass( 'show_if_cus_producttype' ).show();
});
</script><?php
}
add_action( 'admin_footer', 'producttype_custom_js' );
This will add the checkbox option Dan Needham has said is missing.

WordPress: how to hide toolbar in post editor?

I have a Custom Post Type (Products) in my WordPress web site.
This is a WooCommerce Product, if it's necessary to know.
I need to hide toolbar (1) into wp-editor on Add Product page.
Also I need to hide "Add media" button (2) and "Visual/Text" tabs (3).
How do I hide them?
Maybe it make sense to change this WordPress Editor to the textarea with the same value of "name" attribute with using of some hooks?
You can use function.php or plugin to manage this code.You need to put a action.
Remove media button:
function z_remove_media_controls() {
remove_action( 'media_buttons', 'media_buttons' );
}
add_action('admin_head','z_remove_media_controls');
Remove Visual tab
add_filter( 'admin_footer', 'custom_edit_page_js', 99);
function custom_edit_page_js(){
echo ' <style type="text/css">
a#content-tmce, a#content-tmce:hover, #qt_content_fullscreen{
display:none;
}
</style>';
echo ' <script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#content-tmce").attr("onclick", null);
});
</script>';
}
You can Identify post type is,
if( get_post_type() == 'product' && is_admin()) {
//do some stuff
}
I found here this elegant solution:
function wpse_199918_wp_editor_settings( $settings, $editor_id ) {
if ( $editor_id === 'content' && get_current_screen()->post_type === 'custom_post_type' ) {
$settings['tinymce'] = false;
$settings['quicktags'] = false;
$settings['media_buttons'] = false;
}
return $settings;
}
add_filter( 'wp_editor_settings', 'wpse_199918_wp_editor_settings', 10, 2 );
I have found this solution:
function hide_toolbar_TinyMCE( $in ) {
$in['toolbar1'] = '';
$in['toolbar2'] = '';
$in['toolbar'] = false;
return $in;
}
add_filter( 'tiny_mce_before_init', 'hide_toolbar_TinyMCE' );
But it hide toolbar everywhere, because this is "filter" but not "action".
How do I hide it only on Product (Custom Post Type) Add/Edit page?

Categories