Tagged: 

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #640201

    Hi I have a question. I would like to create a custom table where I can add a list of product variations together with their stock quantity. My product is a cruise and the variations are the curise dates, I’d like to create a sort of tabular calendar where I have the list of the cruises for each date together with places available (stock quantity).

    I thought I could add a php code in the table element, but found it was not possible. I imagine I could hence create a new avia element (as described here http://kriesi.at/documentation/enfold/add-new-or-replace-advanced-layout-builder-elements-from-child-theme/), where I create a specific table for this purpose.

    However, tough I know hot to write in in pure php, I have some difficulties in manging it with enfold and woocommerce.

    Do you have any suggestion?

    Thank you

    Elena

    #640565

    Hi sorry, but while waiting for your answer I went thinking on about the solution and I imagine I got it! Since it may serve for others, I’d like to report here the procedure I adopted. Maybe you can also answer to a question (see at the bottom of the codes)

    First of all I created a new function on the file class-wc-product-variation.php that is found in woocommerce/includes/
    The function is more or less the modified copy of the get_formatted_name at line 741 of the file. My new function is called get_formatted_variations_name and returns a string which gives the title of the variation and the price. Here it is:

    STEP 1

    
    	/**
    	 * Get product variation name. Used within admin.
    	 *
    	 * @return string Formatted product name, including attributes and price
    	 */
    	public function get_formatted_variations_name() {
    		if ( $this->get_sku() ) {
    			$identifier = $this->get_sku();
    		} else {
    			$identifier = '';
    		}
    
    		$formatted_attributes = $this->get_formatted_variation_attributes( true );
    		$variation_name = substr($formatted_attributes, 27);
    		$extra_data           = $variation_name . ' – ' . wc_price( $this->get_price() );
    
    		return sprintf( __( '%s', 'woocommerce' ), $extra_data );
    	}
    

    STEP 2)
    I created a new avia element called “calendar’ in which I call the function stated before, together with the stock data in a loop, in order to extract all variation for a determined product. In my case the product was called “CSR Expeditions Payment” and the variations are the dates of the cruises. The $loop passes all the products, and before outputting the data I need, I filter the product of interest (in this case CSR Expeditions Payment).

     
    <?php
    if( !class_exists( 'woocommerce' ) )
    {
    	add_shortcode('csr_calendar_button', 'avia_please_install_woo');
    	return;
    }
    
    if ( !class_exists( 'avia_csr_calendar_button' ) )
    {
    	class avia_csr_calendar_button extends aviaShortcodeTemplate
    	{
    		/**
    		 * Create the config array for the shortcode button
    		 */
    		function shortcode_insert_button()
    		{
    			$this->config['name']		= __('CSR Calendar', 'avia_framework' );
    			$this->config['tab']		= __('Plugin Additions', 'avia_framework' );
    			$this->config['icon']		= AviaBuilder::$path['imagesURL']."sc-button.png";
    			$this->config['order']		= 23;
    			$this->config['target']		= 'avia-target-insert';
    			$this->config['shortcode'] 	= 'csr_calendar_button';
    			$this->config['tooltip'] 	= __('Display the "CSR Calnedar" button for the current product', 'avia_framework' );
    			$this->config['drag-level'] = 3;
    			$this->config['tinyMCE'] 	= array('disable' => "true");
    			$this->config['posttype'] 	= array('product',__('This element can only be used on single product pages','avia_framework'));
    		}
    
    		/**
    		 * Editor Element - this function defines the visual appearance of an element on the AviaBuilder Canvas
    		 * Most common usage is to define some markup in the $params['innerHtml'] which is then inserted into the drag and drop container
    		 * Less often used: $params['data'] to add data attributes, $params['class'] to modify the className
    		 *
    		 *
    		 * @param array $params this array holds the default values for $content and $args.
    		 * @return $params the return array usually holds an innerHtml key that holds item specific markup.
    		 */
    		function editor_element($params)
    		{
    			$params['innerHtml'] = "<img />config['icon']."' title='".$this->config['name']."' />";
    			$params['innerHtml'].= "<div class='avia-element-label'>".$this->config['name']."</div>";
    			$params['content'] 	 = NULL; //remove to allow content elements
    			return $params;
    		}
    
    		/**
    		 * Frontend Shortcode Handler
    		 *
    		 * @param array $atts array of attributes
    		 * @param string $content text within enclosing form of shortcode element
    		 * @param string $shortcodename the shortcode found, when == callback name
    		 * @return string $output returns the modified html string
    		 */
    		function shortcode_handler($atts, $content = "", $shortcodename = "", $meta = "")
    		{
    			$output = "";
    			$meta['el_class'];
    			
    			global $woocommerce, $product;
    			if(!is_object($woocommerce) || !is_object($woocommerce->query) || empty($product)) return;
    
    			// $product = wc_get_product();
    			
    			$output .= "<div class='av-woo-calendar-button ".$meta['el_class']."'>";
    			ob_start();?>
    			
                    <table cellspacing="0" cellpadding="2">
                        <thead>
                            <tr>
                                <th scope="col" style="text-align:left;"><?php _e('Cruise', 'woothemes'); ?></th>
                                <th scope="col" style="text-align:left;"><?php _e('Places availability', 'woothemes'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php
                        
                        $args = array(
                            'post_type'			=> 'product_variation',
                            'post_status' 		=> 'publish',
                            'posts_per_page' 	=> -1,
                            'orderby'			=> 'title',
                            'order'				=> 'DESC',
                            'meta_query' => array(
                                array(
                                    'key' 		=> '_stock',
                                    'value' 	=> array('', false, null),
                                    'compare' 	=> 'NOT IN'
                                )
                            )
                        );
                        
                        $loop = new WP_Query( $args );
                    
                        while ( $loop->have_posts() ) : $loop->the_post();
                        
                                        $product = new WC_Product_Variation( $loop->post->ID );
                                if (get_the_title( $loop->post->post_parent ) == 'CSR Expeditions Payment')
                                {		
                            ?>
                            <tr>
                                <td><?php  echo $product->get_formatted_variations_name(); ?></td>
                                <td><?php echo intval ($product->stock); ?></td>
                            </tr>
                            <?php
                                }
                        endwhile; 
                        
                        ?>
                        </tbody>
                    </table>
    		
    			<?php
    			
    			$output .= ob_get_clean();
    			$output .= "</div>";
    			
    			return $output;
    		}
    	}
    }
    

    CONSIDERATIONS

    1) It seems working by now, but I have to add it AFTER the product purchase button, otherwise the purchase button doesn’t appear… still have to figure out why

    2) I still am trying to see where I can save my new function in the child theme folde. I tried to add it to a subfolder woocommerce/includes, but it doesn’t work like that. By now I left the new function in the original woocommerce file class-wc-product-variation.php, but it is not a good solution as if I update the plugin the modifications would be lost. HERE’S MY QUESTION: do you know where I should save it??

    Thanks again
    Elena

    #640673

    Hi,

    Thank you for using Enfold.

    1.) Do you have a test page where we can see the shortcode in action? Please post the login details as well so that we can check the settings.

    2.) Please follow the instructions here: http://kriesi.at/documentation/enfold/add-new-or-replace-advanced-layout-builder-elements-from-child-theme/

    Best regards,
    Ismael

    #640734

    Hi Ismael, here I am.

    To see how the code works please refer to the following link

    As I was telling you, the csr-expeditions is the product with the variations that correspond to different date cruises. I added also gravity form to allow the possibility of adding the names of the participants if they are > 1. Just below the form I added the calendar that I described above

    These where my steps:

    1) added the new shortcodes in the child theme folder
    2) added the new wc-class to the class-wc-product-variation.php file which STILL is in the woocommerce folder

    Actually my problem is where I can save the class-wc-product-variation.php file in my child theme in order to keep it even if I update woocommerce. I had the same issue with another file of woocommerce that I modified (the form-shipping.php) and it doesn’t work if saved in my child- theme folder.
    Do you have any suggestion?

    #640754

    Sorry Ismael could you please hide the login details I sent to you? I wrongly forgot to write them as a private content

    Thank you

    #641195

    Hi,

    According to the woocommerce documentation, there’s a function called “get_formatted_name”. Why not you use that instead?

    $product->get_formatted_name();
    

    Sorry Ismael could you please hide the login details I sent to you? I wrongly forgot to write them as a private content

    We moved it in the private content field.

    Best regards,
    Ismael

    #641293

    Hi Ismael I tried to use the function get_formatted_name, but it was giving the whole bunch of data formatted ina string. I just wanted the name of the variation, and in order to avoid overriding the original function I preferred creating a new one.

    Can you suggest me wherelse I can save the file with the function so that it works, instead of leaving it in the original WC folder? I have the same problem also with one modified template of woocommerce. It doesn’t work if saved in my child theme.

    Thanks
    Elena

    #642347

    Hi,

    I’m sorry but you can’t override the woocommerce class in a child theme. What is the result of the “get_formatted_name” function? You can filter or adjust the returned value directly in the shortcode file. Example:

    $name = $product->get_formatted_name();
    $name = // do more things here;
    

    Best regards,
    Ismael

    #642463

    You are right: it was so simple! Thank you! Do you have btw any suggestion on how to override some woocommerce templates (such as e.g. the form-shipping.php) instead? I tried to save a modified copy in a woocommerce subfolder within my child theme but it doesn’t work.

    Any suggestion?

    #642856

    Hi!

    Did you create a checkout folder? Try to copy the actual folder structure. Note in the file:

    /**
    * Checkout shipping information form
    *
    * This template can be overridden by copying it to yourtheme/woocommerce/checkout/form-shipping.php.
    *
    * HOWEVER, on occasion WooCommerce will need to update template files and you (the theme developer).
    * will need to copy the new files to your theme to maintain compatibility. We try to do this.
    * as little as possible, but it does happen. When this occurs the version of the template file will.
    * be bumped and the readme will list any important changes.

    As far as I know, you can only modify the files that is included in the woocommerce > templates folder. All the files outside that directory can’t be overwritten in a child theme. You can use filters and hooks but I don’t think you need that in this situation.

    Cheers!
    Ismael

    #642869

    Hi Ismael, yes I did copy it with the same folder structure. I did it with other themes in the past and it worked perfectly. Strange, this doesn’t work with enfold. It’s a kind of bizarre problem… seems so easy but I don’t get out of it!

    #643038

    Hey!

    The theme contains no modification for the shipping template so I’m not sure why it’s not working. Please try to open a new ticket in the woocommerce support forum. I’m sure that they will be able to help you out.

    Best regards,
    Ismael

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.