将产品数量、名称和结帐链接添加到 WooCommerce 添加到购物车消息

问题描述 投票:0回答:1

我不知道该怎么做,是的 - 我一直在搜索和互联网上。我确实找到了各种解决方案,但没有一个允许我有限的经验自行解决这个问题。

我需要的是这个:

“您刚刚将 QTY 产品名称添加到购物车。您的订单小计为 SUBTOTAL。想要结账还是继续购物?BUTTON | BUTTON”

因此,进一步解释一下: “您刚刚将 3 个蓝牙耳机添加到您的购物车。您的订单小计为 299 美元。想要结帐还是要继续购物?继续 | 前往结帐”

换句话说,它应该计算添加的产品数量并显示小计。右侧的“继续”按钮链接到商店页面,“结账”按钮链接到结帐页面。

现在 - 这就是我的问题开始的地方.. 在移动设备上时,整体布局有点混乱,所以我决定制作两个版本,允许文本和按钮以不同的方式对齐/放置。

这是我的代码,我确信它来自其他地方 - 但我尽了最大的努力去修改它,尽了最大的努力让它工作。

如果有人可以帮助我 - 谢谢!

add_filter( 'wc_add_to_cart_message_html', 'atcm_with_checkout_button_including_mobile_control', 10, 3 );
function atcm_with_checkout_button_including_mobile_control( $message, $qty, $count ) {

    $subtotal = wc_price(WC()->cart->subtotal);
    $count += $qty;
    
    $direct_to_shop = wc_get_shop_url();
    $direct_checkout = is_checkout() ? '#customer_details' : wc_get_checkout_url() . '#customer_details';

        if ( wp_is_mobile() && !is_checkout() ) {

        return sprintf (__( '
            <div style="font-size:20px">You just placed QTY of PRODUCT NAME in your cart.
            Your order total right now is %s. Wanna continue shopping %s or do you want to checkout %s? 
            </div>
        ' ), 
        
        $count, $subtotal, 

        '<a style="width:100%; border-left:none; margin-bottom:10%;" 
        class="shopping button alt" href="' . $direct_to_shop . '">' . __(" Continue Shopping ") . '</a>' );

        '<a style="width:100%; border-left:none; margin-top:10%;" 
        class="checkout button alt" href="' . $direct_checkout . '">' . __(" Go to Checkout ") . '</a>' );
    
        } else {

            if ( ! wp_is_mobile() && ! is_checkout() ) {
            
                return sprintf(__('
            <div style="font-size:20px">Your just placed QTY of PRODUCT NAME in your cart, brining your order total to %s. 
            Wanna continue shopping%s or do you want to checkout?%s
            </div>
            '), 
            $count, $subtotal, 
            '<a class="shopping button alt" href="' . $direct_to_shop . '">' . __(" Continue Shopping ") . '</a>' );            
            '<a class="checkout button alt" href="' . $direct_checkout . '">' . __(" Go to Checkout ") . '</a>' );
        
        }
    }
}

我搜索并尝试了不同的替代方案,尝试自己学习和修改。我就是无法完成这项工作。

php wordpress woocommerce cart storefront
1个回答
0
投票

您的代码中存在一些错误和错误,请使用以下重新访问的代码:

add_filter( 'wc_add_to_cart_message_html', 'atcm_with_checkout_button_including_mobile_control', 10, 3 );
function atcm_with_checkout_button_including_mobile_control( $message, $products, $show_qty ) {
    $subtotal     = WC()->cart->subtotal;
    $product_ids  = array_keys($products);
    $product      = wc_get_product( reset($product_ids) );
    $quantity     = $products[$product->get_id()];
    $shop_url     = esc_url( wc_get_page_permalink('shop') );
    $checkout_url = wc_get_checkout_url() . '#customer_details';

    $button_style = 'width:100%; border-left:none; margin-bottom:10%';
    $button_html  = '<a style="%s" class="shopping button alt" href="%s">%s</a>';
    $shop_btn     = sprintf( $button_html, $button_style, $shop_url, __('Continue Shopping') );
    $checkout_btn = sprintf( $button_html, $button_style, $checkout_url, __('Go to Checkout') );

    if ( ! is_checkout() ) {
        if ( wp_is_mobile() ) {
            $message = sprintf ( '<div style="font-size:20px">' . 
                __('You just placed %d of %s in your cart. Your order total right now is %s. Wanna %s or do you want %s?') .
                '</div>', $quantity, $product->get_name(), wc_price($subtotal), $shop_btn, $checkout_btn
            );
        } else {
            $message = sprintf( '<div style="font-size:20px">' . 
                __('You just placed %d of %s in your cart, bringing your order total to %s. Wanna %s or do you want %s?') .
                '</div>', $quantity, $product->get_name(), wc_price($subtotal), $shop_btn, $checkout_btn
            );
        }
    }
    return $message;
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

© www.soinside.com 2019 - 2024. All rights reserved.