【问题标题】:I want to add an aler(or notification) in function我想在函数中添加警报(或通知)
【发布时间】:2019-06-11 05:42:17
【问题描述】:

我有一个功能可以检查产品是否属于同一类别,如果为真,则允许将该产品添加到购物车中。

如果产品未通过验证,我希望出现通知或警报(此产品属于不同类别)。我不知道如何实现这一点。你能给我一些建议吗?

add_filter('nav_menu_css_class', 'atg_menu_classes', 1, 3);

function is_product_the_same_cat($valid, $product_id, $quantity) {
    global $woocommerce; 
    if($woocommerce->cart->cart_contents_count == 0){ 
        return true; 
    } 
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) { 
        $_product = $values['data']; 
        $terms = get_the_terms( $_product->id, 'product_cat' ); 
        $target_terms = get_the_terms( $product_id, 'product_cat' ); 
        foreach ($terms as $term) { 
            $cat_ids[] = $term->term_id; 
        } 
        foreach ($target_terms as $term) { 
            $target_cat_ids[] = $term->term_id; 
        } 
    } 
    $same_cat = array_intersect($cat_ids, $target_cat_ids); 
    if(count($same_cat) > 0) return $valid; 
    else { 

        wp_safe_redirect(get_permalink( get_page_by_path( 'about' ) ) );
        exit();; 
    } 
} 
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_the_same_cat',10,3);

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    也许你想要这样的东西

    echo "<script>alert(\"something\")</script>"
    

    这会在网页正文中添加一个 javascript 脚本来提醒用户。

    【讨论】:

    • 不幸的是,这不是我想要的。如果上述函数中描述的条件不起作用(即在“esle”之后),我想添加一个警报。谢谢
    猜你喜欢
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    相关资源
    最近更新 更多