Skip to main content
add notes on tracing
Source Link

Additional notes on intercepting the process: When you var_dump to track, it is better var_dump($_REQUEST) also. If you have options then need to confirm it is passed from the form to php. This could help to trace and confirm. If the parameter does not exist in $_REQUEST, most likely it is not in the form that is being submitted.

Additional notes on intercepting the process: When you var_dump to track, it is better var_dump($_REQUEST) also. If you have options then need to confirm it is passed from the form to php. This could help to trace and confirm. If the parameter does not exist in $_REQUEST, most likely it is not in the form that is being submitted.

Source Link

There are some points in your code might need to review practically.

Here is something you might be interested to update and a method for you to var_dump the process.

function validate_attribute_weight( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {

    // Get custom field

    // 1. I am not sure if you directly get the post meta using WP builtin return expected result or not since I can see you use $product->get_meta() before
    // $weight = get_post_meta( $variation_id, 'custom_field', true );
    // $stock_weight_checkbox = get_post_meta( get_the_id(),'_stock_weight_checkbox', true );

    // you may try to change to
    // current product from the filter, it is better than get_the_id() because if filter provides to you, more directly and bullet-proof
    $product           = wc_get_product( $product_id );

    // with $product fetched, you may use the same code previously which ensure consistency 
    $stock_weight_checkbox = $product->get_meta( '_stock_weight_checkbox', true );

    if ( 'yes' == $stock_weight_checkbox && ! empty( $weight ) ) {
        // Get product object
        // $product = wc_get_product( $product_id ); // since already get, no need to do so

        // Get current product stock
        $product_stock = $product->get_stock_quantity();

        // ( Weight * quantity ) > product stock
        if( ( ( $weight * $quantity ) > $product_stock ) ) {
            wc_add_notice( sprintf( 'Sorry, you cannot add <strong>' . $weight .'</strong> of <strong>%1$s</strong> to the cart because there are only <strong>%2$sg</strong> left in our inventory. Please choose a lesser amount. We hope to have more in stock shortly.', $product->get_name(), $product_stock ), 'error' );
            $passed = false;
        }
    }

    // what you concern is the testing logic, if you want to intercept, you may add exit()
    // var_dump() here
    exit();

    return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'validate_attribute_weight', 10, 5 );

Because the WordPress ajax process is like:

  • you click the add to cart
  • it pass the data by JS ajax with URL and then return with something, it will fire the action to the php to process <== this is the point that you can intercept, when the php exit here, anything during the process is in the middle and thus you could see the var_dump();
  • after process completed, the php return result (false/true) and return something to the page