• Resolved ryan0432

    (@ryan0432)


    Hello guys,

    I am not building a website for the company that has more than 5000 products.
    I wonder if there is anyway to use WooCommerce product attribute value for table ID so I don’t have to copy and paste for each entry.
    I’d like to know how I can do that. Thanks in advance.

Viewing 10 replies - 16 through 25 (of 25 total)
  • Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    sounds good! Let me know how it goes!

    Best wishes,
    Tobias

    Thread Starter ryan0432

    (@ryan0432)

    Hi @tobiasbg

    The debug function is working!
    It returns string like so:
    https://imgur.com/BSQDz3G
    It seems the issue comes from the single quotes in the returning value.
    I am still trying to find a way to strip them but no matter what I use (reg_replace, str_replace) It still throws [table “” not found /] back to me.
    It seems the returning value of $product->get_sku() already returns string that we don’t need to cast it into string again.
    Can we fix this? Cheers.

    • This reply was modified 4 years, 8 months ago by ryan0432.
    Thread Starter ryan0432

    (@ryan0432)

    Hi @tobiasbg

    I even tried to change the assignment to direct string assignment like so:
    https://imgur.com/hgMEwdS
    and it still returns [table “” not found /]
    Now we are certain that the problem comes from

    $atts['íd'] = $partNumber;
    return tablepress_get_table( $atts );

    this part.
    We are on the right track, just a few more steps to the solution.
    Please let me know what you think, cheers. ??

    Thread Starter ryan0432

    (@ryan0432)

    Hi @tobiasbg

    I also tried to implement something like this:

    add_shortcode( 'table-wc', 'champion_tablepress_woocommerce_shortcode' );
    function champion_tablepress_woocommerce_shortcode() {
    	global $product;
    	$partNumber = $product->get_sku();
    	tablepress_print_table( array( 'id' =>$partNumber) );
    }

    But it gives me this:

    “There has been a critical error on your website. Please check your site admin email inbox for instructions.”

    I will just wait for your investigation. Cheers ??

    P.S. The table prints in product page but it will always show on top of the page no matter where you put the shortcode. Then few minutes later, it returns the error message.

    • This reply was modified 4 years, 8 months ago by ryan0432.
    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    I just remembered that WordPress sometimes has problems with hyphens in Shortcodes, and then only recognizes the version without the hyphen, i.e. table instead of table-wc here. And as the used [table-wc /] Shortcode doesn’t have an ID attribute, that might lead to the error here, so we could simply replace the hyphen with an underscore. Please try again with

    add_shortcode( 'table_wc', 'champion_tablepress_woocommerce_shortcode' );
    function champion_tablepress_woocommerce_shortcode( $atts ) {
      global $product;
      $partNumber = $product->get_sku();
      $atts['id'] = (string) $partNumber;
      return tablepress_get_table( $atts );
    }

    and the Shortcode [table_wc /] on the page.

    Regards,
    Tobias

    P.S.: Regarding your attempt with tablepress_print_table(): That will not work, because Shortcodes must never print the output themselves, but return it (they basically return the replacement value of a search/replace operation).

    Thread Starter ryan0432

    (@ryan0432)

    Hi @tobiasbg

    Based on your theory, I have found a solution on it.
    But there is also a catch on it. Please bear with me for a while because it going to be long.

    First of all, it seems we don’t have to cast $product->get_sku(); into string since it is returning a string.

    Secondly, no matter I use a hyphen(-) or a underscore(_), it still plays up. The problem mainly happens on the layout. If use hyphen or underscore, the whole table will be pushed to the top. If forced it down to a different row below, it will be squeezed to the left hand side and squashed into a very narrow layout, won’t be stretched to screen width. I just get rid of the hyphen and underscore altogether. The current shortcode looks like this [tablewc /]

    After few experiments on code, I accidentally found if we get rid of the argument we parse into the function, which is $atts , it will work.

    The code block currently looks like this:

    add_shortcode( 'table-wc', 'champion_tablepress_woocommerce_shortcode' );
    function champion_tablepress_woocommerce_shortcode() {
    	global $product;
    	$partNumber = $product->get_sku();
    	$atts['id'] = $partNumber;
    	return tablepress_get_table( $atts );
    }

    This way also works, too. Because without the use of $atts['id'] it looks more legal without the argument. But this still throws critical error.

    add_shortcode( 'tablewc', 'champion_tablepress_woocommerce_shortcode' );
    function champion_tablepress_woocommerce_shortcode() {
    	global $product;
    	$partNumber = $product->get_sku();
    	return tablepress_get_table( array( 'id' =>$partNumber ) );
    }

    and it will display correctly. otherwise, the [table "" not found!] will still show.

    However, there is a catch. With this no-argument-function, each time I update the template, it gives me Fatal Error.

    Can you please investigate these points for me? I will lookup the error log and see what it throws to me. Please help me on finding out how to resolve these issues:
    1. Why add_shortcode() callback doesn’t take the argument parsed into the function we setup.
    2. The fatal error it throws when putting the shortcode in block (without the arguments parsed in)

    Many many thanks Tobia, we are very close.

    Regards

    • This reply was modified 4 years, 8 months ago by ryan0432.
    • This reply was modified 4 years, 8 months ago by ryan0432.
    Thread Starter ryan0432

    (@ryan0432)

    Hi @tobiasbg

    We have god the error code

    [24-Mar-2020 01:05:07 UTC] PHP Fatal error:  Uncaught Error: Call to a member function get_sku() on null in /home/championparts/public_html/championparts_web/wp-content/themes/Impreza-child/functions.php:11
    Stack trace:
    #0 /home/championparts/public_html/championparts_web/wp-includes/shortcodes.php(325): champion_tablepress_woocommerce_shortcode('', '', 'tablewc')
    #1 [internal function]: do_shortcode_tag(Array)
    #2 /home/championparts/public_html/championparts_web/wp-includes/shortcodes.php(199): preg_replace_callback('/\\[(\\[?)(tablew...', 'do_shortcode_ta...', '[vc_column_text...')
    #3 /home/championparts/public_html/championparts_web/wp-content/plugins/us-core/templates/elements/vc_column.php(92): do_shortcode('[vc_column_text...')
    #4 /home/championparts/public_html/championparts_web/wp-content/plugins/us-core/functions/shortcodes.php(206): require('/home/championp...')
    #5 /home/championparts/public_html/championparts_web/wp-includes/shortcodes.php(325): US_Shortcodes->__call('vc_column', Array)
    #6 [internal function]: do_shortcode_tag(Array)
    #7 /home in /home/championparts/public_html/championparts_web/wp-content/themes/Impreza-child/functions.php on line 11

    I removed the plug-in “Code-Snippet” and moved all hooks to fucntions.php so we can have a clearer view on which part is actually going wrong.

    Please help me to investigate, many thanks.

    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    ok, that error message means that the $product variable does not exist in the global variable scope. It should be a WooCommerce object, which then offers that get_sku() method. This is what we investigated above, whether the WooCommerce data is available…

    I’m not sure what you mean with the layout changes when using the hyphen or underscore, though. My guess is that the layout is shifted because of some error message that’s embedded in the page.

    I do have an idea regarding the $atts variable though: If no attributes are set, WordPress passes that as an empty string, which we can’t simply turn into an array, so that we need a cast here. Please try again with this:

    add_shortcode( 'tablewc', 'champion_tablepress_woocommerce_shortcode' );
    function champion_tablepress_woocommerce_shortcode( $atts ) {
    	$atts = (array) $atts;
    	global $product;
    	if ( ! isset( $product ) ) {
    	  return 'There is no product on this page.';
    	}
    	$partNumber = $product->get_sku();
    	$atts['id'] = $partNumber;
    	return tablepress_get_table( $atts );
    }

    This also includes a check for the $product variable, for testing.

    Regards,
    Tobias

    Thread Starter ryan0432

    (@ryan0432)

    Hi @tobiasbg

    Yeah, after you added the validation if ( !isset( $product ) ) ? to the function, it is running fine, no more critical error popping up after updating the template post.

    So I am guessing, it is one of the call-stack called this function while $product is not yet assigned a value/reference. Could that be the case?

    $atts = (array) $atts; This works as well. Now the callback can catch the argument without throwing errors.

    If no attributes are set, WordPress passes that as an empty string, which we can’t simply turn into an array.

    This is really a niche quirk only you professional WordPress developers know. lol

    Anyway, many many thanks to your help. I learned a lot from this task and also from you.

    I hope someone who are looking for the same solution as mine can find this super long post helpful.

    Regards

    Plugin Author TobiasBg

    (@tobiasbg)

    Hi,

    no problem, you are very welcome! ?? Good to hear that this helped!

    Best wishes,
    Tobias

    P.S.: In case you haven’t, please rate TablePress here in the plugin directory. Thanks!

Viewing 10 replies - 16 through 25 (of 25 total)
  • The topic ‘Is it possible using WooCommerce product attribute value for Table ID?’ is closed to new replies.