• Resolved HelgaTheViking

    (@helgatheviking)


    Hi there,

    I am the author of the WooCommerce Name Your Price plugin. I have a customer reporting that when your plugin is active, the NYP script is not working on a simple product page (not using your tables). We have confirmed that when your plugin is active the NYP javascript file is producing a 404 error.

    Specifically in the main.php file of your plugin you have this:

    
    // Name your price -- script
      if( function_exists('WC_Name_Your_Price') ){
        wp_enqueue_script( 'woocommerce-nyp', WC_Name_Your_Price()->plugin_url() . '/assets/js/name-your-price.js', array( 'jquery', 'accounting' ), WC_Name_Your_Price()->version, true );    
        
        $wc_nyp_script_params = array(
          'currency_format_num_decimals' => wc_get_price_decimals(),
          'currency_format_symbol'       => get_woocommerce_currency_symbol(),
          'currency_format_decimal_sep'  => wc_get_price_decimal_separator(),
          'currency_format_thousand_sep' => wc_get_price_thousand_separator(),
          'currency_format'              => str_replace( array( '%1$s', '%2$s' ), array( '%s', '%v' ), get_woocommerce_price_format() ), // For accounting.js.
          'annual_price_factors'         => WC_Name_Your_Price_Helpers::annual_price_factors(),
          'i18n_subscription_string'     => __( '%price / %period', 'wc_name_your_price' ),
        );  
        wp_localize_script( 'woocommerce-nyp', 'woocommerce_nyp_params', apply_filters( 'wc_nyp_script_params', $wc_nyp_script_params ) );
    
        $wcpt_nyp_error_message_templates = apply_filters(
    			'wc_nyp_error_message_templates',
    			array(
    				'invalid-product' => __( 'This is not a valid product.', 'wc_name_your_price' ),
    				'invalid'         => __( '"%%TITLE%%" could not be added to the cart. Please enter a valid, positive number.', 'wc_name_your_price' ),
    				'minimum'         => __( '"%%TITLE%%" could not be added to the cart. Please enter at least %%MINIMUM%%.', 'wc_name_your_price' ),
    				'hide_minimum'    => __( '"%%TITLE%%" could not be added to the cart. Please enter a higher amount.', 'wc_name_your_price' ),
    				'minimum_js'      => __( 'Please enter at least %%MINIMUM%%.', 'wc_name_your_price' ),
    				'hide_minimum_js' => __( 'Please enter a higher amount.', 'wc_name_your_price' ),
    				'maximum'         => __( '"%%TITLE%%" could not be added to the cart. Please enter less than or equal to %%MAXIMUM%%.', 'wc_name_your_price' ),
    				'maximum_js'      => __( 'Please enter less than or equal to %%MAXIMUM%%.', 'wc_name_your_price' ),
    				'empty'           => __( 'Please enter an amount.', 'wc_name_your_price' ),
    				'minimum-cart'    => __( '"%%TITLE%%" cannot be purchased. Please enter at least %%MINIMUM%%.', 'wc_name_your_price' ),
    				'maximum-cart'    => __( '"%%TITLE%%" cannot be purchased. Please enter less than or equal to %%MAXIMUM%%.', 'wc_name_your_price' ),
    			)
    		);
        wp_localize_script( 'woocommerce-nyp', 'wcpt_nyp_error_message_templates', $wcpt_nyp_error_message_templates );
    
      }
    

    That script URL is no longer correct. There’s no need for you to manually enqueue my script this way. I see you are doing this with other plugins (presumably also for compatibility) but this is only going to cause you maintenance headaches in the long run.

    I have registered by script with
    wp_register_script( 'woocommerce-nyp' )

    So you can enqueue it at any time after with:

    
    function wc_nyp_script_load() {
       wp_enqueue_script( 'woocommerce-nyp' );
    }
    add_action( 'wp_enqueue_scripts', 'wc_nyp_script_load', 99 );
    

    At least you will always be loading my script from my correct/current location, with all my up to date JS localizations. However, I don’t fully recommend this as its inefficient to load this script on all pages. Ideally, the script should be loaded only when necessary.

    Instead, you can print out the Name Your Price price input by calling WC_Name_Your_Price()->display->display_price_input( $product ) anywhere you wish to display the input and the script will be automatically loaded.

    For displaying multiple inputs on the same page (Which I see sometimes in other tables-like plugins) I recommend using a suffix:

    
    <code>WC_Name_Your_Price()->display->display_price_input( $product, array( 'suffix' => '-wcptl-' . $product->get_id() ) )</code>
    
    Which coupled with 
    
    

    function wc_ptl_nyp_field_suffix( $suffix, $product_id ) {
    if ( conditional_check_for_product_tables_plugin_context() ) {
    $suffix = ‘-wcptl-‘ . $product_id;
    }
    return $suffix;
    }
    add_filter( ‘wc_nyp_field_suffix’, ‘wc_ptl_nyp_field_suffix’ );
    `

    Should help NYP distinguish which price inputs go with which product.

    All that is a bit more advanced…. the most pressing concern is the script 404. It will be great for our customer if you could use WP standards for enqueuing scripts from other plugins. It should resolve the problem for our customer as well as may your plugin easier for you to support.

    Let me know if you have any questions.

    Cheers,
    -Kathy

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Name Your Price compatibility’ is closed to new replies.