Wordpress. Woocommerce. Action hook BEFORE adding to cart

后端 未结 1 1371
情歌与酒
情歌与酒 2020-12-13 10:30

What i need to do: I want to run some checks on a product before being added to the cart. More exactly: I want to compare the product i am about to

相关标签:
1条回答
  • 2020-12-13 11:00

    A bit late, but I think you are looking for add to cart validation, which is filterable. Here's an over simplified version:

    function so_validate_add_cart_item( $passed, $product_id, $quantity, $variation_id = '', $variations= '' ) {
    
        // do your validation, if not met switch $passed to false
        if ( 1 != 2 ){
            $passed = false;
            wc_add_notice( __( 'You can not do that', 'textdomain' ), 'error' );
        }
        return $passed;
    
    }
    add_filter( 'woocommerce_add_to_cart_validation', 'so_validate_add_cart_item', 10, 5 );
    
    0 讨论(0)
提交回复
热议问题