【问题标题】:Subtracting cart subtotal to cart total error issue In WooCommerce在 WooCommerce 中将购物车小计减去购物车总错误问题
【发布时间】:2021-01-13 14:17:46
【问题描述】:

根据我上一个问题 Add WooCommerce cart link + total as last menu item in wp_nav_menu 的答案代码,我现在尝试将运费作为附加参数添加到链接中。

我需要一个 if 语句,确保购物车不为空。然后我认为最简单的方法是进行简单的计算,然后得到运输总额(订单总额减去小计)。

问题是;我收到两条通知,上面写着警告:A non-numeric value encountered。警告指的是这一行:

$shipping_total = wc_price($order_total - $order_subtotal);

这是代码:

add_filter( 'wp_nav_menu_header-menu_items', 'minicart_link_with_product_count_subtotal_and_shipping', 10, 2 );
function minicart_link_with_product_count_subtotal_and_shipping( $items, $args ) {

    $order_total = wc_price(WC()->cart->get_total());

    $order_subtotal = wc_price(WC()->cart->get_subtotal());

    $shipping_total = wc_price($order_total - $order_subtotal);

    // cart url
    $link_url = wc_get_cart_url();   

    // icon, product count, subtotal and shipping (based on if statement)
    if (WC()->cart->is_empty()){

        $link_text = sprintf( __( '<i class="shopping-cart"></i> (0)', 'woocommerce' ));

    } else {

        $link_text = sprintf( __( '<i class="shopping-cart"></i> %d - %s (%s)', 'woocommerce' ), WC()->cart->cart_contents_count, wc_price(WC()->cart->get_subtotal()), $shipping_total);

    }

    // link
    $minicart_link = '<a title="View my cart" class="wfminicart" href="' . $link_url . '">' . $link_text . '</a>';

    // return the link as last menu item
    return $items . $minicart_link;
}

【问题讨论】:

    标签: php wordpress woocommerce cart numeric


    【解决方案1】:

    您需要从前 2 个变量中删除 wc_price() 格式化函数,并将 WC()-&gt;cart-&gt;get_total() 替换为 WC()-&gt;cart-&gt;total 以避免此问题。

    所以你的代码将是:

    add_filter( 'wp_nav_menu_header-menu_items', 'minicart_link_with_product_count_subtotal_and_shipping', 10, 2 );
    function minicart_link_with_product_count_subtotal_and_shipping( $items, $args ) {
        $order_total    = WC()->cart->total; // <= Here non formatted total
    
        $order_subtotal = WC()->cart->get_subtotal();
    
        $shipping_total = wc_price($order_total - $order_subtotal);
    
        // cart url
        $link_url = wc_get_cart_url();   
    
        // icon, product count, subtotal and shipping (based on if statement)
        if (WC()->cart->is_empty()){
    
            $link_text = sprintf( __( '<i class="shopping-cart"></i> (0)', 'woocommerce' ));
    
        } else {
    
            $link_text = sprintf( __( '<i class="shopping-cart"></i> %d - %s (%s)', 'woocommerce' ), WC()->cart->cart_contents_count, wc_price(WC()->cart->get_subtotal()), $shipping_total);
    
        }
    
        // link
        $minicart_link = '<a title="View my cart" class="wfminicart" href="' . $link_url . '">' . $link_text . '</a>';
    
        // return the link as last menu item
        return $items . $minicart_link;
    }
    

    它应该可以正常工作。

    【讨论】:

    • 嗨 LoicTheAztec,是的,修复了其中一个通知,但仍然存在。
    • 有两个通知,它们都是一样的。 Warning: A non-numeric value encountered。使用您的代码,一个被删除,一个被保留。它仍然引用同一行:$shipping_total = wc_price($order_total - $order_subtotal);
    • 添加return并将其更改为return $items . $minicart_link;时确实有效!谢谢!
    • @HaroldAldersen Oups!我的错......我在上次编辑时忘记替换它,
    猜你喜欢
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多