0

i'm building a single page application using woocommerce and custom REST endpoints to get template parts and load them into the browser ... one endpoint is getting content-single-product.php template which loads single product details below is the code of the template

<?php
do_action( 'woocommerce_after_single_product_summary' );
?>

and here is the code that get's the product template and send back to javascript

/**
* Get Single Product
*
* @param int $product_id product id
* @return string|boolean html for single page
*/
function nh_get_product( $product_id ) {
    global $product;
    $output_html;

    ob_start();
    $product = wc_get_product($product_id);
    get_template_part('woocommerce/content-single-product');
    $output_html = ob_get_contents();
    ob_end_clean();

    return $output_html;

}


public function get_item( $request ) {
    $product_id = $request->get_param('p_id');

    if ( function_exists( 'nh_get_product' ) && $product_id ) {
      $output_html = nh_get_product( $product_id );
      if ( !empty($output_html) ) {
        $data = array( 'result' =>  $output_html );
        return new WP_REST_Response( $data, 200 );
      }
    }

    return new WP_Error( 'not-found', 'cannot find product', array( 'status' => 500 ) );
  }

the endpoint is working fine and sending back the html to the javascript, the problem is in do_action( 'woocommerce_after_single_product_summary' ); it does not trigger and does not throw any errors ... i replaced it with basic action hook that echo 'test' just for testing and still did not trigger .. im guessing do_action does not work inside ob_start();

i could just replace the do_action with direct calls to whatever the hook was doing and it will work i'm guessing ... but i just wanted to keep consistent with woocommerce templating system so i avoid any future big conflicts with the updates

EDIT: i removed the part of the HTML content in content-single-product.php that runs fine to ease in reading my question

  • 80 lines of HTML markup to help you? Please shorten your question and check the spelling. – Max Yudin Sep 30 at 13:49
  • @MaxYudin i included the code that runs the whole logic to provide enough information for anyone helping ... i could remove <section id=product> and leave do_action line only after all it's what my question is about .. just thought to put the whole template maybe it will help – Mohamed Mokhtar Sep 30 at 15:12

Your Answer

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

Browse other questions tagged or ask your own question.