Forum Replies Created

Viewing 15 replies - 346 through 360 (of 1,795 total)
  • @rikatredcell I’m glad you got it sorted.

    It is strange that they’re using different tags/CSS for the Blocks than for the shortcodes. You could point it out as an issue on their GitHub Page.

    @roeljonker I’m glad that works for you!

    I don’t know if the WooCommerce developers would consider this a bug. I think it falls into the category of a “feature.” The code looks like they intended to leave “In Stock” out.

    It seems like adding an option in the Settings or in Appearance -> Customize -> WooCommerce to display the “In Stock” would be a nice thing to have. There is a WooCommerce Feature Request Forum that I believe is still active where you could suggest it, or maybe on their GitHub Page as an enhancement. If you do, please post the link here and I will vote for it. 🙂

    I may be misunderstanding what you’re trying to accomplish, but I think WooCommerce will do this for you out-of-the box using variable products with the relevant attributes.

    For example, you’d create two attributes, quantity and swatches. The quantity attribute would have values “1|2|3”. The swatches would have the values you want for them, say “red|yellow|green|blue”. Next, you’d create variations combining the quantities and swatches that correspond. When you’re done, the customer’s choice of quantity will only show the swatch values that you paired with it.

    There is one “gotcha” you have to be cautious with. By default, the variations threshold is 30. If the number of variations is less than the threshold, WooCommerce uses AJAX to check the available variations on the fly, and the user interface will work the way you want it.and the customer is only able to select available variations. If the number of variations is greater than the threshold, AJAX isn’t used, and the customer may get the “unavailable variation” message.

    If you have more than 30 variations for a product, you can increase the threshold by adding the following code to your child theme’s functions.php, replacing the “150” with whatever you need, but keeping in mind the higher the number, the more resource-intensive the AJAX call:

    
    add_filter( 'woocommerce_ajax_variation_threshold', 'wc_ajax_variation_threshold' );
    function wc_ajax_variation_threshold() {
        return 150;
    }
    

    Interesting. It looks like it might be the difference with using Blocks.

    I tried using a products shortcode, and I got the unordered list layout like you have on your Shop page. You might give that a try on your Home page instead of Blocks. There are shortcodes to reproduce each section you currently have on your Home page.

    I have to say, your Home page doesn’t look bad to me at all, though you need some images for your categories to spice it up. 🙂

    You could add the following to Appearance -> Customize -> Additional CSS to get rid of the underlines on the “Select Options” buttons to make it look more like the Shop page if you decide to go with the Blocks:

    
    .hentry .entry-content a:not(.button):not(.components-button) {
    	text-decoration: none;
    }
    

    I don’t know when this happened either, but the culprit appears to be in the WooCommerce function get_availability_text():

    
    protected function get_availability_text() {
            if ( ! $this->is_in_stock() ) {
                $availability = __( 'Out of stock', 'woocommerce' );
            } elseif ( $this->managing_stock() && $this->is_on_backorder( 1 ) ) {
                $availability = $this->backorders_require_notification() ? __( 'Available on backorder', 'woocommerce' ) : '';
            } elseif ( $this->managing_stock() ) {
                $availability = wc_format_stock_for_display( $this );
            } else {
                $availability = '';
            }
            return apply_filters( 'woocommerce_get_availability_text', $availability, $this );
        }
    

    Notice how the “else” statement returns an empty string if the product is in stock and not under inventory control.

    Give the following a try and see if it works for you. I didn’t test it with inventory control, but the only thing I changed was to substitute “In Stock” for an availability with an empty string:

    
    add_filter('woocommerce_get_availability', 'custom_get_availability', 1, 2);
    function custom_get_availability($availability, $product) {
      if ($availability['availability'] == '') {
        $availability['availability'] = __('In Stock', 'woocommerce');
      }
      return $availability;
    }
    

    I figured it out! I was using the wrong slug for the parent menu. I found the answer using the code contributed by Aurovrata Venet on the remove_submenu_page in the Codex to find the correct slug:

    
    global $submenu;
    error_log(print_r($submenu, true));
    

    With that information, the code to hide the “Reports” submenu is:

    
    function custom_menu_page_removing() {
      remove_submenu_page('woocommerce', 'wc-reports');
    }
    add_action('admin_menu', 'custom_menu_page_removing', 110);
    

    That removed the “Reports” submenu for me with and without the new Analytics disabled.

    According to this post, if your goal is to reduce resource usage, it doesn’t sound like it’s necessary unless you’re worried about users clicking the link; if you don’t click the link, it doesn’t use any resources.

    If you want to hide the submenu item, it seems like according to this article, you could use the following, but I couldn’t get it to work:

    
    function custom_menu_page_removing() {
      remove_submenu_page('admin.php', 'wc-reports');
    }
    add_action('admin_menu', 'custom_menu_page_removing', 110);
    

    You might post a new thread specifically about removing the WooCommerce “Reports” submenu and get more responses. If you do, please link to it here. I am curious why that code doesn’t work. It may be firing before the WooCommerce menus are registered.

    With variable products too, it would just use an array for the post_type:

    
    add_shortcode( 'scheduled_sale_products', 'scheduled_sale_products_shortcode' );
    	function scheduled_sale_products_shortcode($atts) {
    		extract( shortcode_atts(array('limit' => '', 'columns'=>'' ), $atts));
    		$columns = ! empty($atts['columns'] ) ? $atts['columns'] : 3;
    		
    		//echo 'Products on sale: ' . implode( ',', wc_get_product_ids_on_sale() ) . '<br>';
    		//echo 'From time = ' . current_time('timestamp', 1) . '<br>';
    		
    		$args = array(
    			'post_type'=>array('product', 'product_variation'),
    			'post_status' =>'publish', 
    			'post_in'=>implode( ',', wc_get_product_ids_on_sale() ), 
    			'posts_per_page'=>$atts['limit'],
    			'meta_query'=>array(
    				'relation'=>'AND',
    				array(
    					'key' =>'_sale_price_dates_from',
    					'value' =>current_time( 'timestamp', 1 ),
    					'compare'=>'>',		
    				),
    				array(
    					'key'=>'_sale_price_dates_to',
    					'value'=>current_time( 'timestamp', 1 ),
    					'compare'=>'>',		
    				),
    			),
    		);
    	
    		$loop = new WP_Query( $args );
    	
    		if ( $loop->have_posts() ) {
    			?>
    			<ul class ="products columns-<?php echo $columns; ?>">
    			<?php
    				while ($loop->have_posts()) : $loop->the_post();
    					woocommerce_get_template_part( 'content', 'product' );
    				endwhile;
    			?>
    			</ul>
    			<?php
    			}
    			else
    				echo __('No products found');
    		
    			wp_reset_postdata();
    	}
    

    The key to which sale items are shown is in the two operators in the meta_Query:

    
    array(
      'key' =>'_sale_price_dates_from',
      'value' =>current_time( 'timestamp', 1 ),
      'compare'=>'>',		
    				),
    				array(
      'key'=>'_sale_price_dates_to',
      'value'=>current_time( 'timestamp', 1 ),
      'compare'=>'>',		
    ),
    

    In the above code, it’s showing products with a “sale_price_dates_from” time that is greater than the current time, so it will only show future sales.

    If you want to show products that are currently on sale and haven’t had their “sale_price_dates_to” time come around yet, you would change the operator like this:

    
    array(
      'key' =>'_sale_price_dates_from',
      'value' =>current_time( 'timestamp', 1 ),
      'compare'=>'<',		
    				),
    				array(
      'key'=>'_sale_price_dates_to',
      'value'=>current_time( 'timestamp', 1 ),
      'compare'=>'>',		
    ),
    

    If you’re showing products that are currently on sale, why not just use the standard products short code or a block?

    Give this a try:

    
    add_shortcode( 'scheduled_sale_products', 'scheduled_sale_products_shortcode' );
    	function scheduled_sale_products_shortcode($atts) {
    		extract( shortcode_atts(array('limit' => '', 'columns'=>'' ), $atts));
    		$columns = ! empty($atts['columns'] ) ? $atts['columns'] : 3;
    		
    		//echo 'Products on sale: ' . implode( ',', wc_get_product_ids_on_sale() ) . '<br>';
    		//echo 'From time = ' . current_time('timestamp', 1) . '<br>';
    		
    		$args = array(
    			'post_type'=>'product',
    			'post_status' =>'publish', 
    			'post_in'=>implode( ',', wc_get_product_ids_on_sale() ), 
    			'posts_per_page'=>$atts['limit'],
    			'meta_query'=>array(
    				'relation'=>'AND',
    				array(
    					'key' =>'_sale_price_dates_from',
    					'value' =>current_time( 'timestamp', 1 ),
    					'compare'=>'>',		
    				),
    				array(
    					'key'=>'_sale_price_dates_to',
    					'value'=>current_time( 'timestamp', 1 ),
    					'compare'=>'>',		
    				),
    			),
    		);
    	
    		$loop = new WP_Query( $args );
    	
    		if ( $loop->have_posts() ) {
    			?>
    			<ul class ="products columns-<?php echo $columns; ?>">
    			<?php
    				while ($loop->have_posts()) : $loop->the_post();
    					woocommerce_get_template_part( 'content', 'product' );
    				endwhile;
    			?>
    			</ul>
    			<?php
    			}
    			else
    				echo __('No products found');
    		
    			wp_reset_postdata();
    	}
    

    The shortcode will now be:

    
    [scheduled_sale_products limit=12 columns=3]
    

    There were a couple of problems with the original code. One was a typo in 'post__in' which had an extra underscore in the original code and was causing a MySQL error, and the other was with the handling of the operators in the times. I also removed some code that wasn’t used. It’s working for me to display a simple product that is scheduled for being on sale in the future. That code doesn’t work on variable products for me. I also added a parameter to the query to display only published products. On my test site using Twenty Sixteen, the layout is obviously going to need to be tweaked, but hey, it works now. 🙂

    I don’t understand how this is going to help you, because it doesn’t show the date the product is coming on sale, but maybe that’s what you want?

    • This reply was modified 6 years, 2 months ago by linux4me2.

    Now you have me curious. I’ll give this a try and see what I can come up with.

    It’s not my code, but just a quick look at the original code that @yazdaniwp gave you makes me wonder if the problem might be with the operator in this section:

    
    array(
      'key'     => '_sale_price_dates_to',
      'value'   => current_time( 'timestamp', 1 ),
      'compare' => '<',		
      ),
    

    I’d try changing it to greater than the current time to see if you get the expected results.

    The Reports part pre-dates WooCommerce 4.0, doesn’t it? I was thinking you wanted to get rid of the new Analytics, Dashboard, javascript Admin, and all the new stuff they’ve added with v. 4.0, including the database tables for them described in the readme for the plugin. I don’t think the old Reports will hurt you much unless you use it.

    I think to call that function, you’d have to make the WooCommerce functions global. You might try the current, wc version, which shouldn’t require a global:

    
    'post__in' => implode( ',', wc_get_product_ids_on_sale() )
    

    I understand completely. I always go the route of a little code rather than a plugin if possible; however, if you take a look at this plugin, it’s only a few lines of code in one file plus the readme. Total disk space it will take is 2.7 KB. I don’t think it will be any more load on your server than a code snippet.

Viewing 15 replies - 346 through 360 (of 1,795 total)