简体   繁体   English

在Woocommerce 3中自定义添加到购物车消息

[英]Customizing add to cart message in Woocommerce 3

I added a link to the cart page inside the message that pops up when a product is successfully added to the cart in Woocommerce. 我在商品成功添加到Woocommerce中的购物车时弹出的消息中添加了购物车页面的链接。 This however does nothing when it's clicked. 但是,单击它不会执行任何操作。 The inspector does show the correct working link. 检查员确实显示正确的工作链接。

// Add cart page URL
        global $woocommerce;
        $cart_url = $woocommerce->cart->get_cart_url();

        $returnArray['messages']['success'] = sprintf(__('Products successfully added to cart! <a href="%s">View cart</a>','woocommerce'), $cart_url);

I think I should note that this part of the code is used twice in the page: 我想我应该注意,代码的这一部分在页面中使用了两次:

global $woocommerce;
$cart_url = $woocommerce->cart->get_cart_url();

I understand that using the same variable twice could cause issues but in this case they should do the same thing anyway but could this be the cause? 我了解两次使用相同的变量可能会导致问题,但是在这种情况下,它们仍然应该执行相同的操作,但这可能是原因吗?

EDIT: changed the method for getting the cart page URL 编辑:更改了获取购物车页面URL的方法

$returnArray['messages']['success'] = sprintf( __('Products successfully added to cart! <a href="%s">View cart</a>','sf-european-fasteners'), wc_get_cart_url() );

Your code is outdated and deprecated: 您的代码已过时和过时:

  • $global $woocommerce; and $woocommerce is replaced with WC() so for cart: WC()->cart . $woocommerce替换为WC()以便购物车: WC()->cart
  • The WC_Cart method get_cart_url() is deprecated and replaced by wc_get_cart_url() function. WC_Cart方法get_cart_url()已被弃用,并由wc_get_cart_url()函数代替。

To change or override added-to-cart message, you can use the following dedicated filter hook: 要更改或覆盖添加到购物车的消息,可以使用以下专用过滤器挂钩:

add_filter ( 'wc_add_to_cart_message_html', 'custom_add_to_cart_message', 10, 2 );
function custom_add_to_cart_message( $message, $products ) {
    $message = sprintf( __('Products successfully added to cart! <a href="%s">View cart</a>','woocommerce'), wc_get_cart_url() );

    return $message;
}

Code goes in function.php file of your active child theme (or active theme). 代码进入您的活动子主题(或活动主题)的function.php文件中。 It should better work. 它应该更好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM