【问题标题】:Woocommerce order_review custom template on cart items updateWoocommerce order_review 购物车项目更新的自定义模板
【发布时间】:2021-07-14 23:14:11
【问题描述】:

我已经为 woocommerce 结帐页面开发了一个自定义模板,并在plugin/woocommerce/checkout/order_review.php 中添加了我需要专门更改order_review.php 的所有模板,并且在订单页面上它工作得很好。

从订单页面我可以删除一些产品或通过 ajax 添加产品,这是我的 ajax 代码。

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;

woocommerce_order_review(); 加载 woocommerce 默认模板,而不是我插件中的模板。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    默认情况下,WooCommerce 不会在插件文件夹中查找模板。您需要在插件中使用过滤器 woocommerce_locate_template 来告诉 WooCommerce 在插件文件夹中查找模板。 SkyVerge 在这篇博文中概述了该解决方案:https://www.skyverge.com/blog/override-woocommerce-template-file-within-a-plugin/

    我在这里包括解决方案,以防帖子因某种原因被删除:

    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;
    }
    

    【讨论】:

      【解决方案2】:

      您必须覆盖 WooCommerce 模板 woocommerce/templates/checkout/review-order.php 并将此文件放入您的主题文件夹主题/yourthemename/woocommerce/checkout/review-order.php

      【讨论】:

      • 我已经使用我的插件 plugins/myplugin/woocommerce/checkout/review-order.php 完成了这项工作
      猜你喜欢
      • 2017-01-20
      • 2016-12-16
      • 2019-08-07
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 2018-01-09
      • 2018-08-29
      • 2011-04-28
      相关资源
      最近更新 更多