• Resolved ctuxboy

    (@ctuxboy)


    Hello,

    Try loading scripts for a certain product with enable/disable a custom checkbox on product edit page.
    So follow this nice tutorial: https://businessbloomer.com/woocommerce-display-custom-product-badge/

    I play around with this code for enable/disable scripts with a checkbox for a certain product:

    ...
    add_action( 'wp_enqueue_scripts' , 'gfp_scripts' );
     
    function gfp_scripts() {
    global $product;
      if ( get_post_meta( $product->get_id(), 'load_scripts_checkbox', true ) ) { // If checkbox is enabled load scripts
        wp_enqueue_script( 'jquery' );
        ...
      }
    }
    ... 

    But it doesn’t work.

    It gives this error:

    Fatal error: Uncaught Error: Call to a member function get_id() on string in /home/u14701p11712/domains/webkusthosting.be/public_html/dev/wp-content/plugins/google-fonts-preview/google-fonts-preview.php:68 Stack trace: #0 /home/u14701p11712/domains/webkusthosting.be/public_html/dev/wp-includes/class-wp-hook.php(286): gfp_scripts(”) #1 /home/u14701p11712/domains/webkusthosting.be/public_html/dev/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array) #2 /home/u14701p11712/domains/webkusthosting.be/public_html/dev/wp-includes/plugin.php(453): WP_Hook->do_action(Array) #3 /home/u14701p11712/domains/webkusthosting.be/public_html/dev/wp-includes/script-loader.php(1435): do_action(‘wp_enqueue_scri…’) #4 /home/u14701p11712/domains/webkusthosting.be/public_html/dev/wp-includes/class-wp-hook.php(286): wp_enqueue_scripts(”) #5 /home/u14701p11712/domains/webkusthosting.be/public_html/dev/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array) #6 /home/u14701p11712/domains/webkusthosting.be/public_ in /home/u14701p11712/domains/webkusthosting.be/public_html/dev/wp-content/plugins/google-fonts-preview/google-fonts-preview.php on line 68

    Who can help me find a solution?

Viewing 1 replies (of 1 total)
  • Thread Starter ctuxboy

    (@ctuxboy)

    Hello,

    After trying a lot of hours, found a solution:

    ...
    if ( get_post_meta( $post->ID, 'load_scripts_checkbox', true ) ) {
    ...

    That does the trick!

    Christophe

    • This reply was modified 7 years, 1 month ago by ctuxboy.
Viewing 1 replies (of 1 total)
  • The topic ‘Load scripts for a one product’ is closed to new replies.