1

I’m tryin to insert shortcode through add_action :

add_action('woocommerce_single_product_summary', 'quotation_form', 61);
function quotation_form()
{
$produk = get_the_title();
$shortkode = sprintf(
'[zendesk_request_form size="3" group="extra-field" subject="Quotation For %s"]',
$produk
);
$shortkode = do_shortcode( $shortkode );
echo $shortkode;
}

But gettin error after the shortcode displayed:

Uncaught Error: Call to a member function get_upsells() on null in /home/dev/wp-content/themes/dummy-child/woocommerce/single-product/up-sells.php:25

The line related with error above:

if ( ! $upsells = $product->get_upsells() ) {
return;
}

Source: https://github.com/woocommerce/woocommerce/blob/master/templates/single-product/up-sells.php

So I think:

  1. The shortcode itself displaying properly but script stop executed with error above

  2. When I tried to output quotation_form function with return / echo-ing plain text or html, its working perfectly without any error

My question is: How the right way to insert shortcode in WooCommerce template? Is it possible to doing that?

Thank you

2

I solved this with dirty way.. hope someone else can provide more efficient solution, my final working code for problem above:

$shortkode = '[zendesk_request_form size="3" group="extra-field" subject="Quotation For -wkwkwk-"]';
$shortkode = do_shortcode( $shortkode );
add_action('woocommerce_single_product_summary', 'quotation_form', 61);
function quotation_form()
{
$produk = get_the_title();
global $shortkode;
$shortkode = str_replace("-wkwkwk-", $produk, $shortkode);
echo $shortkode;
}

So i move out do_shorcode outside function, then declare it as global variable. The problem is about my $produk variable will not working if declare it outside Wordpress page, so I'm using str_replace to replacing pre product title

1

Your problem is that the $product variable is not defined. I see two possible solutions:

  • If you only render the shortcode on a product page, then write global $product; at the beginning of your function.
  • Else you need to define which product to take: global $product; $product_obj = new WC_Product_Factory(); $product = $product_obj->get_product( "insert product id" );. Also write this at the beginning of the function.
  • Actually I render it in functions page, still no luck, and also have tried directly to product page (in meta.php) still not working. – Asisten Oct 24 '16 at 8:45
  • So do you think this is about scope issue? thank you for the clue, I will find out now – Asisten Oct 24 '16 at 8:49
  • I almost there... I moving out do_shortcode outside function and its work except I can grab the $product var: add_action('woocommerce_single_product_summary', 'quotation_form', 61); function quotation_form() { global $shortkode echo $shortkode; } – Asisten Oct 25 '16 at 1:16
  • Try to debug the $product variable in your quotation_form. Add codeglobal $product; var_dump($product);code. What does this variable display? – Pascal Knecht Oct 27 '16 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.