running your own custom javascript functions on cart refresh

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #5940
    Olly
    Admin & Mod

      as of v2.11.5.1
      if you would like to have your own additional javascript functions running after every cart refresh , you can do the following

      example:
      create a directory called ‘wppizza-custom-js’ in your plugins directory.
      add 2 files in said directory called ‘wppizza-custom-js.php’ and ‘custom.js’
      add the following code to wppizza-custom-js.php:

      
      <?php
      /*
      Plugin Name: WPPizza - Custom Javascript on Cart Refresh
      Description:  WPPizza - Custom Javascript on Cart Refresh
      Author: ollybach
      Plugin URI: http://www.wp-pizza.com
      Author URI: http://www.wp-pizza.com
      Version: 0.1
      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
      ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
      WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
      DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
      ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
      (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
      LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
      ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
      SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      =================
      NOTE: there are probably other ways to do the below too, but  - at the moment of writing at least - this seems to be the simplest....
      =================
      */
      if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
      add_action( 'plugins_loaded', 'myprefix_wppizza_extend_custom_js');
      function myprefix_wppizza_extend_custom_js(){
      if (!class_exists( 'WPPIZZA' )) {return;}
      class MYPREFIX_WPPIZZA_EXTEND_CUSTOM_JS extends WPPIZZA{
      /*****************************************************
      *
      *	[construct]
      *
      ******************************************************/
      function __construct() {
      parent::__construct();
      /******add js functions name to run after cart refresh*************/
      add_filter('wppizza_filter_js_cart_refresh_functions', array( $this, 'myprefix_wppizza_custom_js_cart_refresh_functions'),10,1);
      /***enqueue frontend scripts and styles***/
      add_action('wp_enqueue_scripts', array( $this, 'myprefix_wppizza_register_custom_js'));
      }
      /*******************************************************
      *
      *	[js function to call on cart refresh (in this example wppizzaCustomJs )]
      *
      ******************************************************/
      function myprefix_wppizza_custom_js_cart_refresh_functions($array){
      $array[]='myprefixWppizzaCustomJs';
      return $array;
      }
      /*******************************************************
      *
      *	[include custom js ]
      *
      ******************************************************/
      function myprefix_wppizza_register_custom_js(){
      wp_register_script($this->pluginSlug.'-myprefix-customjs', plugins_url( 'custom.js', __FILE__ ), array($this->pluginSlug), $this->pluginVersion ,$this->pluginOptions['plugin_data']['js_in_footer']);
      wp_enqueue_script($this->pluginSlug.'-myprefix-customjs');
      }
      }
      $MYPREFIX_WPPIZZA_EXTEND_CUSTOM_JS = new MYPREFIX_WPPIZZA_EXTEND_CUSTOM_JS();
      }
      ?>
      

      and in custom.js add something like this for example:

      
      var myprefixWppizzaCustomJs=function(){};
      jQuery(document).ready(function($){
      myprefixWppizzaCustomJs=function(cart){
      /**do stuff*/
      console.log('do stuff');
      console.log(cart);// cart will return all sorts of cart vars
      }
      });
      

      the above should get you going i would have thought

    Viewing 1 post (of 1 total)
    • The topic ‘running your own custom javascript functions on cart refresh’ is closed to new replies.