You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在WooCommerce 3中获取USPS配送所选纸箱名称并显示到cart-totals.php

Display Selected USPS Box Name in WooCommerce 3 cart-totals.php

Got it, let's tackle this—you're using the USPS shipping plugin powered by WooCommerce Services & Jetpack, and want to display the selected box name in cart-totals.php for WooCommerce 3 without enabling debug mode. Since you've already made some progress, here's how to polish it up:

Step 1: Prepare Your Template File

First, make sure you're working with a customized child theme template to avoid losing changes when WooCommerce updates:

  • Copy the original cart-totals.php file from wp-content/plugins/woocommerce/templates/cart/
  • Paste it into your child theme's woocommerce/cart/ directory (create the folders if they don't exist)

Step 2: Add the Box Name Display Code

Insert this code snippet in your custom cart-totals.php file, ideally right after the shipping totals row (look for the <tr class="shipping"> section):

<?php
// Get the currently chosen shipping method
$chosen_methods = WC()->session->get('chosen_shipping_methods');
if (!empty($chosen_methods)) {
    $chosen_method = explode(':', $chosen_methods[0]);
    $shipping_method = WC()->shipping->get_shipping_method($chosen_method[1]);

    // Verify this is the WooCommerce Services USPS method
    if (isset($shipping_method) && $shipping_method instanceof WC_Shipping_USPS) {
        $package = WC()->shipping->get_packages()[0];
        $rates = $package['rates'];
        $chosen_rate = isset($rates[$chosen_methods[0]]) ? $rates[$chosen_methods[0]] : false;

        $box_name = '';
        if ($chosen_rate) {
            // Check for box data in rate meta (compatible with most plugin versions)
            if (isset($chosen_rate->meta_data['box']['name'])) {
                $box_name = $chosen_rate->meta_data['box']['name'];
            } elseif (isset($chosen_rate->meta_data['package_details'])) {
                // Fallback for older plugin versions
                $package_details = maybe_unserialize($chosen_rate->meta_data['package_details']);
                if (isset($package_details['box_id'])) {
                    $available_boxes = $shipping_method->get_boxes();
                    $box_name = $available_boxes[$package_details['box_id']]['name'];
                }
            }
        }

        // Render the box name if we found it
        if (!empty($box_name)) {
            ?>
            <tr class="shipping-box-name">
                <th><?php esc_html_e('Selected Shipping Box', 'woocommerce'); ?></th>
                <td><?php echo esc_html($box_name); ?></td>
            </tr>
            <?php
        }
    }
}
?>

Key Notes for Troubleshooting

  • Field Variations: If the box name doesn't show up, temporarily add var_dump($chosen_rate->meta_data); right after $chosen_rate = ... to inspect the exact meta keys (you can remove this after troubleshooting—no debug mode needed).
  • Session Check: The code checks if a shipping method is selected, so it won't display anything until the user picks a USPS option.
  • Styling: You can add custom CSS to the .shipping-box-name class to match your theme's design.

内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 02:22:55