Woocommerce order_review custom template on cart items update

Viewed 287

I have develop a custom template for woocommerce checkout page and added all templates that I need to change specifically order_review.php in plugin/woocommerce/checkout/order_review.php and on order page it works perfect.

From Order page I can remove some products or add products through ajax and here is my ajax code.

ob_start();
woocommerce_order_review();
$woocommerce_order_review = ob_get_clean();

$response = array(
    'cart_total'    => WC()->cart->total,
    'cart_item_key' => $new_key,
    'fragments'     => apply_filters(
        'woocommerce_update_order_review_fragments',
        array(
            '.woocommerce-checkout-review-order-table' => $woocommerce_order_review,
        )
    ),
);

if ( ! empty( $data ) ) {
    $response['cartflows_data'] = $data;
}

return $response;

And the woocommerce_order_review(); loads woocommerce default template instead of template from my plugin.

2 Answers

You have to override WooCommerce Template woocommerce/templates/checkout/review-order.php and Put this file in your theme folder themes/yourthemename/woocommerce/checkout/review-order.php

WooCommerce does not look in plugin folders for templates by default. You need to use the filter woocommerce_locate_template in your plugin to tell WooCommerce to look in your plugin folder for templates. The solution is outlined by SkyVerge in this blog post: https://www.skyverge.com/blog/override-woocommerce-template-file-within-a-plugin/

I am including the solution here in case the post is every removed for some reason:

function myplugin_plugin_path() {

  // gets the absolute path to this plugin directory

  return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );



function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
  global $woocommerce;

  $_template = $template;

  if ( ! $template_path ) $template_path = $woocommerce->template_url;

  $plugin_path  = myplugin_plugin_path() . '/woocommerce/';

  // Look within passed path within the theme - this is priority
  $template = locate_template(

    array(
      $template_path . $template_name,
      $template_name
    )
  );

  // Modification: Get the template from this plugin, if it exists
  if ( ! $template && file_exists( $plugin_path . $template_name ) )
    $template = $plugin_path . $template_name;

  // Use default template
  if ( ! $template )
    $template = $_template;

  // Return what we found
  return $template;
}
Related