-6ren">
gpt4 book ai didi

wordpress - 根据选定的可用送货国家/地区更改 "no shipping available"消息

转载 作者:行者123 更新时间:2023-12-05 03:37:19 24 4
gpt4 key购买 nike

我目前正在 woocommerce/cart/cart-shipping.php 中添加以下代码,以更改不在 WooCommerce 送货国家列表中的国家/地区的无送货方式消息。

<?php

add_filter( 'woocommerce_cart_no_shipping_available_html', 'custom_no_shipping_available_html' );
add_filter( 'woocommerce_no_shipping_available_html', 'custom_no_shipping_available_html' );

function custom_no_shipping_available_html( $message ) {
$country = WC()->customer->get_shipping_country();
if ( !empty( $country ) ) {
$all_countries = WC()->countries->get_countries();
return sprintf( "Unfortunately, we don't ship to %s. Please contact our Support if you need any help.", $all_countries[ $country ] );
}
return sprintf( "Unfortunately, we don't ship to this location. Please contact our Support if you need any help.", $all_countries[ $country ] );
}
?>

在这段代码之后,cart-shipping.php 中的默认代码是:

$formatted_destination = isset($formatted_destination) ? $formatted_destination : WC()->countries->get_formatted_address($package['destination'], ', ');
$has_calculated_shipping = !empty($has_calculated_shipping);
$show_shipping_calculator = !empty($show_shipping_calculator);
$calculator_text = '';
?>
<tr class="woocommerce-shipping-totals shipping">
<th><?php echo wp_kses_post($package_name); ?></th>
<td data-title="<?php echo esc_attr($package_name); ?>">
<?php if ($available_methods) : ?>
<ul id="shipping_method" class="woocommerce-shipping-methods">
<?php foreach ($available_methods as $method) : ?>
<li>
<?php
if (1 < count($available_methods)) {
printf('<input type="radio" name="shipping_method[%1$d]" data-index="%1$d" id="shipping_method_%1$d_%2$s" value="%3$s" class="shipping_method" %4$s />', $index, esc_attr(sanitize_title($method->id)), esc_attr($method->id), checked($method->id, $chosen_method, false)); // WPCS: XSS ok.
} else {
printf('<input type="hidden" name="shipping_method[%1$d]" data-index="%1$d" id="shipping_method_%1$d_%2$s" value="%3$s" class="shipping_method" />', $index, esc_attr(sanitize_title($method->id)), esc_attr($method->id)); // WPCS: XSS ok.
}
printf('<label for="shipping_method_%1$s_%2$s">%3$s</label>', $index, esc_attr(sanitize_title($method->id)), wc_cart_totals_shipping_method_label($method)); // WPCS: XSS ok.
do_action('woocommerce_after_shipping_rate', $method, $index);
?>
</li>
<?php endforeach; ?>
</ul>
<?php if (is_cart()) : ?>
<p class="woocommerce-shipping-destination">
<?php
if ($formatted_destination) {
// Translators: $s shipping destination.
printf(esc_html__('Shipping to %s.', 'woocommerce') . ' ', '<strong>' . esc_html($formatted_destination) . '</strong>');
$calculator_text = esc_html__('Change address', 'woocommerce');
} else {
echo wp_kses_post(apply_filters('woocommerce_shipping_estimate_html', __('Shipping options will be updated during checkout.', 'woocommerce')));
}
?>
</p>
<?php endif; ?>
<?php
elseif (!$has_calculated_shipping || !$formatted_destination) :
if (is_cart() && 'no' === get_option('woocommerce_enable_shipping_calc')) {
echo wp_kses_post(apply_filters('woocommerce_shipping_not_enabled_on_cart_html', __('Shipping costs are calculated during checkout.', 'woocommerce')));
} else {
echo wp_kses_post(apply_filters('woocommerce_shipping_may_be_available_html', __('Enter your address to view shipping options.', 'woocommerce')));
}
elseif (!is_cart()) :
echo wp_kses_post(apply_filters('woocommerce_no_shipping_available_html', __('There are no shipping options available. Please ensure that your address has been entered correctly, or Please contact our Support if you need any help.', 'woocommerce')));
else :
// Translators: $s shipping destination.
echo wp_kses_post(apply_filters('woocommerce_cart_no_shipping_available_html', sprintf(esc_html__('No shipping options were found for %s.', 'woocommerce') . ' ', '<strong>' . esc_html($formatted_destination) . '</strong>')));
$calculator_text = esc_html__('Enter a different address', 'woocommerce');
endif;
?>

<?php if ($show_package_details) : ?>
<?php echo '<p class="woocommerce-shipping-contents"><small>' . esc_html($package_details) . '</small></p>'; ?>
<?php endif; ?>

<?php if ($show_shipping_calculator) : ?>
<?php woocommerce_shipping_calculator($calculator_text); ?>
<?php endif; ?>
</td>
</tr>

但问题是我必须填写所有帐单地址字段,包括电话和邮政编码,才能收到不支持所选国家/地区的消息。

我认为发生这种情况是因为默认情况下 WooCommerce 正在检查同一 PHP 文件中设置的 $formatted_destination 变量。

我的问题是一旦选择了国家并且它不属于运送国家的一部分,我如何才能以直接显示的方式显示消息?

最佳答案

要回答您的问题,请先进行一些解释。我从 templates/cart/cart-shipping.php 复制了一段代码第 66 - 72 行@version 3.6.0

elseif ( ! is_cart() ) :
echo wp_kses_post( apply_filters( 'woocommerce_no_shipping_available_html', __( 'There are no shipping options available. Please ensure that your address has been entered correctly, or contact us if you need any help.', 'woocommerce' ) ) );
else :
// Translators: $s shipping destination.
echo wp_kses_post( apply_filters( 'woocommerce_cart_no_shipping_available_html', sprintf( esc_html__( 'No shipping options were found for %s.', 'woocommerce' ) . ' ', '<strong>' . esc_html( $formatted_destination ) . '</strong>' ) ) );
$calculator_text = esc_html__( 'Enter a different address', 'woocommerce' );
endif;

如您所见,它包含 2 个过滤器 Hook ,即:

  • woocommerce_no_shipping_available_html
  • woocommerce_cart_no_shipping_available_html

虽然第二个钩子(Hook)在消息中显示了 $formatted_destination,但第一个钩子(Hook)没有。


所以要回答你的问题,你可以像这样使用钩子(Hook):

显示在购物车页面上的消息

function filter_woocommerce_cart_no_shipping_available_html( $sprintf ) {
// Get packages
$packages = WC()->shipping()->get_packages();

// Loop through
foreach ( $packages as $i => $package ) {
$formatted_destination = WC()->countries->get_formatted_address( $package['destination'], ', ' );
}

// Message
$sprintf = sprintf( esc_html__( 'Unfortunately, we don\'t ship to %s. Please contact our Support if you need any help.', 'woocommerce' ) . ' ', '<strong>' . esc_html( $formatted_destination ) . '</strong>' );

return $sprintf;
}
add_filter( 'woocommerce_cart_no_shipping_available_html', 'filter_woocommerce_cart_no_shipping_available_html', 10, 1 );

显示在结帐页面上的消息

function filter_woocommerce_no_shipping_available_html( $html ) {
// Message
$html = __( 'My new message', 'woocommerce' )

return $html;
}
add_filter( 'woocommerce_no_shipping_available_html', 'filter_woocommerce_no_shipping_available_html', 10, 1 );

对于这两个答案,无需修改/覆盖模板文件,代码进入事件子主题(或事件主题)的 functions.php 文件。

关于wordpress - 根据选定的可用送货国家/地区更改 "no shipping available"消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69368716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com