2014-02-17 54 views
3

我使用Woocommerce中的add to cart消息钩来编辑文本并从某些按钮中删除某些类。看起来这个钩子现在已经在Woocommerce 2.1中被弃用了,我找不到替代品。Woocommerce中wc_add_to_cart_message钩子的替代WP

我想从“继续购物”按钮中删除“按钮”类。这个类在Woocommerce内核中定义,我希望未经编辑,以便将来进行正确更新。

我想编辑位于woocommerce /线路包括/ WC-车-的functions.php线94

$message = sprintf('<a href="%s" class="button wc-forward">%s</a> %s', $return_to, __('Continue Shopping', 'woocommerce'), $added_text); 

没有人找到这个钩子又一个适当的选择吗?提前致谢!

回答

7

这为我工作

add_filter('wc_add_to_cart_message', 'custom_add_to_cart_message'); 
function custom_add_to_cart_message() { 
    global $woocommerce; 

     $return_to = get_permalink(woocommerce_get_page_id('shop')); 
     $message = sprintf('<a href="%s" class="button wc-forwards">%s</a> %s', $return_to, __('Continue Shopping', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce')); 
    return $message; 
} 

编辑:谢谢你的校正Kaarel卡斯帕

+0

感谢这个优秀的答案:-) – michaelmcgurk

0

这可能是一个解决方案。

过滤器名已与2.1版本改为“wc_add_to_cart_message”

add_filter('wc_add_to_cart_message', 'foo'); 
function foo() { 

$product_id = $_REQUEST[ 'product_id' ]; 

if (is_array($product_id)) { 

    $titles = array(); 

    foreach ($product_id as $id) { 
     $titles[] = get_the_title($id); 
    } 

    $added_text = sprintf(__('Added &quot;%s&quot; to your cart.', 'woocommerce'), join(__('&quot; and &quot;', 'woocommerce'), array_filter(array_merge(array(join('&quot;, &quot;', array_slice($titles, 0, -1))), array_slice($titles, -1))))); 

} else { 
    $added_text = sprintf(__('&quot;%s&quot; was successfully added to your cart.', 'woocommerce'), get_the_title($product_id)); 
} 

// Output success messages 
if (get_option('woocommerce_cart_redirect_after_add') == 'yes') : 

    $return_to = apply_filters('woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url()); 

    $message = sprintf(
     '<a href="%s" class="alert-link">%s &rarr;</a> %s', 
     $return_to, __('Continue Shopping', 'woocommerce'), 
     $added_text 
    ); 

else : 

    $message = sprintf(
     '<a href="%s" class="alert-link">%s &rarr;</a> %s', 
     get_permalink(wc_get_page_id('cart')), 
     __('View Cart', 'woocommerce'), 
     $added_text); 

endif; 

return $message; 
} 

希望它能帮助:如果你有更好的方法或想法,请更换。 欢呼

+1

wc_add_to_cart_message有2个参数:消息和PRODUCT_ID。据此,最好将'add_filter'语句更改为'add_filter('wc_add_to_cart_message','foo',10,2);',将'foo'函数的签名更改为'function foo($ message,$ product_id) '并删除字符串'$ product_id = $ _REQUEST ['product_id'];'。 – rpeshkov

3

Woocommerce 2.3及更高版本,

add_filter('wc_add_to_cart_message', 'custom_add_to_cart_message'); 
    function custom_add_to_cart_message($message ){ 
    global $woocommerce; 

    $added_text = __('Product was successfully added to your Network Kit.', 'woocommerce'); 
    // Output success messages 
    if (get_option('woocommerce_cart_redirect_after_add') == 'yes') : 

     $return_to = apply_filters('woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url()); 

     $message = sprintf('<a href="%s" class="button wc-forward">%s</a> %s', $return_to, __('Continue Shopping', 'woocommerce'), $added_text); 

    else : 

     $message = sprintf('<a href="%s" class="button wc-forward">%s</a> %s', wc_get_page_permalink('cart'), __('View your Network Kit', 'woocommerce'), $added_text); 

    endif; 

    return $message; 
}