【问题标题】:Woocommerce storefront_cart_link override has no effectWoocommerce storefront_cart_link 覆盖无效
【发布时间】:2019-08-07 09:44:16
【问题描述】:

作为 Storefront 的孩子,我正在尝试为自定义 WooCommerce 主题减少一些内容。我首先替换了 storefront_header_cart 函数以删除完整的购物车列表,它按预期工作:

if ( ! function_exists( 'storefront_header_cart' ) ) {
    function storefront_header_cart() {
        if ( storefront_is_woocommerce_activated() ) {
            if ( is_cart() ) {
                $class = 'current-menu-item';
            } else {
                $class = '';
            }
            ?>
        <ul id="site-header-cart" class="site-header-cart menu">
            <li class="<?php echo esc_attr( $class ); ?>">
                <?php storefront_cart_link(); ?>
            </li>
        </ul>
            <?php
        }
    }
}

然后我想更改内容链接本身的文本。我做了完全相同的事情来覆盖默认的店面行为......

if ( ! function_exists( 'storefront_cart_link' ) ) {
    function storefront_cart_link() {
        ?>
            <a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'storefront' ); ?>">
                <?php /* translators: %d: number of items in cart */ ?>
                <span class="count"><?php echo wp_kses_data( sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'storefront' ), WC()->cart->get_cart_contents_count() ) ); ?></span>
            </a>
        <?php
    }
}

...但它没有做任何可辨别的事情。显示完整的原始链接:

<a class="cart-contents" href="http://localhost/cart/" title="View your shopping cart">
    <span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>0.00</span> <span class="count">0 items</span>
</a>

为什么会有不一致的行为?

  • WordPress:5.1.1
  • WooCommerce:3.5.6
  • 店面:2.4.5

【问题讨论】:

  • @LoicTheAztec 我最初是这么想的,但我没有看到任何在页面加载时发出的异步调用可以解释这一点。我会更深入地挖掘,但没有调用获取片段,我不确定这是怎么发生的。
  • @LoicTheAztec 我认为这解决了我在更新购物车后会遇到的未来问题,但目前在页面加载时我找不到任何异步调用来获取片段。此外,按照建议进行更新不会更改页面加载时的链接。稍后我将用几条信息更新 OP,这可能会有所帮助。
  • @LoicTheAztec 我收回了……它正在工作。可能是与此相结合的缓存问题。请张贴作为答案。

标签: ajax wordpress woocommerce cart storefront


【解决方案1】:

这似乎与 Storefront 标题购物车计数上的 ajaxified 购物车片段有关。

  1. 首先完全清空购物车并检查它是否与缓存无关。
  2. ajaxifiedcartfragments上涉及的钩子是woocommerce_add_to_cart_fragments,所以你应该尝试在Storefront源代码中找到一些相关的钩子函数代码。

查看有关 Ajax 的有关标题购物车计数的相关主题:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 2016-08-28
    • 2016-10-20
    • 2014-02-04
    • 2017-05-06
    相关资源
    最近更新 更多