Plugin Directory

Changeset 864479


Ignore:
Timestamp:
02/25/2014 04:42:58 AM (12 years ago)
Author:
maxrice
Message:

fix add to cart text customizations in WC 2.1

Location:
woocommerce-customizer/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • woocommerce-customizer/trunk/readme.txt

    r855936 r864479  
    44Tags: woocommerce
    55Requires at least: 3.5
    6 Tested up to: 3.8
    7 Stable tag: 1.1.1
     6Tested up to: 3.8.1
     7Stable tag: 1.2
    88License: GPLv3 or later
    99License URI: http://www.gnu.org/licenses/gpl-3.0.html
     
    5454== Changelog ==
    5555
     56= 1.2 =
     57* Fixed issues with add to cart button text customizations in WooCommerce 2.1
     58
    5659= 1.1.1 =
    5760* WooCommerce 2.1 Compatibility
  • woocommerce-customizer/trunk/woocommerce-customizer.php

    r855936 r864479  
    66 * Author: SkyVerge
    77 * Author URI: http://www.skyverge.com
    8  * Version: 1.1.1
     8 * Version: 1.2
    99 * Text Domain: wc-customizer
    1010 * Domain Path: /languages/
     
    2828    return;
    2929
     30// required compatibility class
     31require_once( 'includes/class-wc-customizer-compatibility.php' );
    3032
    3133/**
     
    7173
    7274    /** plugin version number */
    73     const VERSION = '1.1.1';
     75    const VERSION = '1.2';
    7476
    7577    /** var array the active filters */
     
    100102        }
    101103
     104        add_action( 'woocommerce_init', array( $this, 'load_customizations' ) );
     105    }
     106
     107
     108    /**
     109     * Load customizations after WC is loaded so the version can be checked
     110     *
     111     * @since 1.2
     112     */
     113    public function load_customizations() {
     114
    102115        // load filter names and values
    103116        $this->filters = get_option( 'wc_customizer_active_customizations' );
     
    107120
    108121            foreach ( $this->filters as $filter_name => $filter_value ) {
    109                 add_filter( $filter_name, array( $this, 'customize' ) );
    110         }
    111 
    112             // for use some day, in a galaxy far, far away, when PHP 5.3+ has greater WP adoption
     122
     123                // WC 2.1 changed the add to cart text filter signatures so conditionally add the new filters
     124                if ( false !== strpos( $filter_name, 'add_to_cart_text' ) && WC_Customizer_Compatibility::is_wc_version_gte_2_1() ) {
     125
     126                    if ( $filter_name == 'single_add_to_cart_text' ) {
     127
     128                        add_filter( 'woocommerce_product_single_add_to_cart_text', array( $this, 'customize_single_add_to_cart_text' ) );
     129
     130                    } else {
     131
     132                        add_filter( 'woocommerce_product_add_to_cart_text', array( $this, 'customize_add_to_cart_text' ), 10, 2 );
     133                    }
     134
     135                } else {
     136
     137                    add_filter( $filter_name, array( $this, 'customize' ) );
     138                }
     139            }
     140
     141                // for use some day, in a galaxy far, far away, when WP has greater 5.3 adoption
    113142            // add_filter( $filter_name, function() use ( $filter_value ) { return $filter_value; } );
    114143        }
     
    154183        $current_filter = current_filter();
    155184
    156         if ( isset( $this->filters[ $current_filter ] ) )
     185        if ( isset( $this->filters[ $current_filter ] ) ) {
    157186            return $this->filters[ $current_filter ];
     187        }
    158188
    159189        // no need to return a value passed in, because if a filter is set, it's designed to only return that value
     190    }
     191
     192
     193    /**
     194     * Apply the single add to cart button text customization in WC 2.1+
     195     *
     196     * The filter signature changed from `single_add_to_cart_text` to `woocommerce_product_single_add_to_cart_text`
     197     *
     198     * @since 1.2
     199     */
     200    public function customize_single_add_to_cart_text() {
     201
     202        return $this->filters['single_add_to_cart_text'];
     203    }
     204
     205
     206    /**
     207     * Apply the shop loop add to cart button text customization in WC 2.1+
     208     *
     209     * The filter signature changed from `add_to_cart_text|{type}_add_to_cart_text` to `woocommerce_product_add_to_cart_text`
     210     *
     211     * This is sort of a hack but prevents a major refactoring and maintains backwards compatibility until WC 2.1+ can
     212     * be required
     213     *
     214     * @since 1.2
     215     * @param string $text add to cart text
     216     * @param WC_Product $product product object
     217     * @return string modified add to cart text
     218     */
     219    public function customize_add_to_cart_text( $text, $product ) {
     220
     221        // out of stock add to cart text
     222        if ( isset( $this->filters['out_of_stock_add_to_cart_text'] ) && ! $product->is_in_stock() ) {
     223
     224            return $this->filters['out_of_stock_add_to_cart_text'];
     225        }
     226
     227        if ( isset( $this->filters['add_to_cart_text'] ) && $product->is_type( 'simple' ) ) {
     228
     229            // simple add to cart text
     230            return $this->filters['add_to_cart_text'];
     231
     232        } elseif ( isset( $this->filters['variable_add_to_cart_text'] ) && $product->is_type( 'variable') )  {
     233
     234            // variable add to cart text
     235            return $this->filters['variable_add_to_cart_text'];
     236
     237        } elseif ( isset( $this->filters['grouped_add_to_cart_text'] ) && $product->is_type( 'grouped' ) ) {
     238
     239            // grouped add to cart text
     240            return $this->filters['grouped_add_to_cart_text'];
     241
     242        } elseif( isset( $this->filters['external_add_to_cart_text'] ) && $product->is_type( 'external' ) ) {
     243
     244            // external add to cart text
     245            return $this->filters['external_add_to_cart_text'];
     246        }
     247
     248        return $text;
    160249    }
    161250
     
    168257     * is active.
    169258     *
    170      * @since 0.1
     259     * @since 1.0
    171260     * @param array $actions associative array of action names to anchor tags
    172261     * @return array associative array of plugin action links
     
    205294
    206295        // upgrade if installed version lower than plugin version
    207         if ( -1 === version_compare( $installed_version, self::VERSION ) )
     296        if ( -1 === version_compare( $installed_version, self::VERSION ) ) {
    208297            $this->upgrade( $installed_version );
     298        }
    209299    }
    210300
Note: See TracChangeset for help on using the changeset viewer.