[SOLVED]Calculating a maximum width

Home – SaaS Forums Support questions WooPrice Calculator (Pro) [SOLVED]Calculating a maximum width

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #7491
    Robin Hollaar
    Participant

    Hi,

    I want to set a maximum width. The situation

    Side A = 100 mm
    Side B = 100 mm

    A+B Total = 200 mm [Max of 1500mm] I want a message to appear if the total amount is more than 1500 mm that you are not able to order because the max with = 1500 mm And that your not able to order.

    Greetz Robin.

    #7497
    Enrico
    Participant

    Hi Robin,

    You can use PHP filters to do that. Please read this article here: https://altoswebsolutions.com/blog/more-validation-using-wooprice-calculator-woocommerce

    In your case would be something like this:


    function filter_custom_wpc_add_to_cart_validation($bool, $product_id, $quantity){
    if($product_id == PRODUCT_ID){
    $sideA = $_REQUEST['aws_price_calc_ID1'];
    $sideB = $_REQUEST['aws_price_calc_ID2'];

    if(($sideA+$sideB) > 1500){
    wc_add_notice("SideA and SideB are greater then 1500 mm", "error");

    return false;
    }
    }

    return true;
    }
    add_filter('woocommerce_add_to_cart_validation', 'filter_custom_wpc_add_to_cart_validation', 20, 3);

    Where:

    PRODUCT_ID: Your product ID
    ID1: The WPC field ID of SideA
    ID2: The WPC field ID of SideB

    Follow the instructions on the article how to add this code in your website.

    Kind Regards,
    Enrico
    AT234

    #7496
    Robin Hollaar
    Participant

    Thanks enrico

    After adding the code to the function.php I now have the warning but still the product gets add to the basket.

    Is there a fix to this or am i missing something ?

    #7495
    Enrico
    Participant

    Hi Robin,

    Please send me your WP-admin and your FTP credentials to “https://altoswebsolutions.com/contact-us”, I will look into the problem directly.

    Kind Regards,
    Enrico
    AT234

    #7494
    Enrico
    Participant

    Hi Robin,

    There was a problem on the plugin that wasn’t executing your code correctly.

    I’ve installed in your website the 2.2.1 version (bugfix). You can download it from here for future reference:

    [hide]
    http://altoswebsolutions.com/beta/2.2.1_ioUyhg67HytR/woo-price-calculator.zip
    [/hide]

    Your code is working ok now.

    Kind Regards,
    Enrico
    AT234

    #7493
    Robin Hollaar
    Participant

    Hi Enrico,

    Yeah the code is working now.
    If I want to do the same validation on more product’s is its possible to add more product id’s to the code or do I need to copy paste a new code. ?

    Robin

    #7492
    Enrico
    Participant

    Hi Robin,

    If you need to apply the same validation to all the WooCommerce products:


    function filter_custom_wpc_add_to_cart_validation($bool, $product_id, $quantity){

    $sideA = $_REQUEST['aws_price_calc_ID1'];
    $sideB = $_REQUEST['aws_price_calc_ID2'];

    if(($sideA+$sideB) > 1500){
    wc_add_notice("SideA and SideB are greater then 1500 mm", "error");

    return false;
    }

    return true;
    }
    add_filter('woocommerce_add_to_cart_validation', 'filter_custom_wpc_add_to_cart_validation', 20, 3);

    If you want to apply the same validation only to some products:


    function filter_custom_wpc_add_to_cart_validation($bool, $product_id, $quantity){
    $products = array(PROD_ID1, PROD_ID2, PROD_ID3, PROD_ID4);

    if(in_array($product_id, $products)){
    $sideA = $_REQUEST['aws_price_calc_ID1'];
    $sideB = $_REQUEST['aws_price_calc_ID2'];

    if(($sideA+$sideB) > 1500){
    wc_add_notice("SideA and SideB are greater then 1500 mm", "error");

    return false;
    }
    }

    return true;
    }
    add_filter('woocommerce_add_to_cart_validation', 'filter_custom_wpc_add_to_cart_validation', 20, 3);

    If you want to check different validation values for different products you can:


    function filter_custom_wpc_add_to_cart_validation($bool, $product_id, $quantity){
    $values = array(
    PROD_ID1 => 1500,
    PROD_ID2 => 1250,
    PROD_ID3 => 1000,
    PROD_ID4 => 2000,
    );

    if(array_key_exists($product_id, $values)){
    $sideA = $_REQUEST['aws_price_calc_ID1'];
    $sideB = $_REQUEST['aws_price_calc_ID2'];

    if(($sideA+$sideB) > $values[$product_id]){
    wc_add_notice("SideA and SideB are greater then {$values[$product_id]} mm", "error");

    return false;
    }
    }

    return true;
    }
    add_filter('woocommerce_add_to_cart_validation', 'filter_custom_wpc_add_to_cart_validation', 20, 3);

    Where:

    PROD_ID1, PROD_ID2, PROD_ID3, PROD_ID4, IDn is the product ID

    Kind Regards,
    Enrico
    AT234

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.