0

I really need some help here. I am trying to get the value of a custom field so I can compare it to the amount in stock, and display a message depending on the returned value.

The problem? No matter what I try I keep being met with boolean false - string length 0 or Uncaught Error: Call to a member function get_meta() on boolean

Please for the love of god, how can I get the value of this custom field that is stored in the meta information of that product's variation?

I've been trying for 2 days without luck! I will love you forever!

// check out of stock using 'custom_field' value
    add_filter( 'woocommerce_add_to_cart_validation', 'woocommerce_validate_attribute_weight', 10,3);
    function woocommerce_validate_attribute_weight($variation_id, $variations, $product_id) {

    $grams = get_post_meta( $variation_id, 'custom_field', true );

    // get product id
    if (isset($_REQUEST["add-to-cart"])) {
        $productid = (int)$_REQUEST["add-to-cart"];
    } else {
        $productid = null;
    }

    // get quantity
    if (isset($_REQUEST["quantity"])) {
        $quantity = (int)$_REQUEST["quantity"];
    } else {
        $quantity = 1;
    }

// get weight of selected variation
    if (isset($_REQUEST["custom_field"])) {
        $weight = preg_replace('/[^0-9.]+/', '', $_REQUEST["custom_field"]);
    } else {
        $weight = null;
    }

    // comparing stock
    if($productid && $weight)
    {
        $product = wc_get_product($productid);
        $productstock = (int)$product->get_stock_quantity();

        if(($weight * $quantity) > $productstock)
        {
            wc_add_notice( sprintf( 'You cannot add that amount of "%1$s" to the cart because there is not enough stock (%2$s remaining).', $product->get_title(), $productstock ), 'error' );
            return;
        }
    }
    var_dump($grams);
    return true;
}
  • I would like to clarify something before answering your questions. How did you add the custom field? Where did you do it? (GUI? or Code: Filter? Action?). – simongcc Apr 6 '20 at 5:46
  • It was added via action ( woocommerce_variation_options_pricing ) and stored via filter ( woocommerce_available_variation ) I can successfully pull the product using $product = wc_get_product( $product_id ); and a var_dump() of $grams = $product->get_available_variations(); shows the custom_field as a string in an array.... so the data is there. I just don't know how to successfully pull it and use it. – Bruce Banner Apr 6 '20 at 6:21
  • I have updated the comment to the validity with better explanation. It should be your own test logic result. The $validity from the filter is for your reference or prior checking. – simongcc Apr 6 '20 at 8:48
  • I have added also how to get the meta of variations. If it is a variation, after user select it and add to the cart. It will be available in $variations as an array in the filter. You could then use for manipulations such as comparison. – simongcc Apr 6 '20 at 8:58
0

I think you have overlooked the use of the filter.

Your use of filter seems missing arguments so that when it pass to your function, it is cascaded. Because there are total 6 parameters from the filter. Your arguments will become something like:

  • $variation_id => becomes a true/false result of validation
  • $variations => becomes $product_id
  • $product_id => becomes $quantity

I have created a simple example to test using the default installation of Woocommerce and sample products. I added a few colors for the album and then add it to the cart.

The woocommerce_add_to_cart_validation could take a maximum of 6 variables when it is defined. And the first one is the validation result supposed to be returned.

  1. The first one is the value for filter which is supposed to be your own validation and passing the true/false result to it. Additional parameters for passing to the callback function starting from 2nd. You may refer to WordPress apply_filters() for details.

  2. When add to cart, simple product will pass first 2 arguments. (product ID and quantity)

  3. If there is product variation, the 4th and 5th argument will be passed to this filter. (variation ID and variation details)

  4. The last one is passed when cart item data is being populated from the cart.

So the first 2 arguments is available for all situations. To make use of the filter with checking the remaining conditioned arguments. A default value could be used to pass to the filter. So that the filter function is applicable to all situations.

add_filter( 'woocommerce_add_to_cart_validation', 'q363322_woocommerce_validate_attribute_weight', 10, 6);

// with default value set, it will not break the code if there is not enough arguments
function q363322_woocommerce_validate_attribute_weight( $validity, $product_id, $quantity, $variation_id = 0, $variations = null, $cart_item_data = null  ) {
    // quantity and product_id is already provided in the filter
    var_dump($product_id); 
    var_dump($quantity); 

    // you may try var_dump this to see if there is any variations for the product and fetch according to your needs
    var_dump($variations); // you can see variations here if the product have any

    // with $variations, you may do your test

    // in my test, it is a color, after select blue, the var_dump output is
    // array(1) { ["attribute_pa_color"]=> string(4) "blue" }
    // to get the attribute
    // $color = $variations['attribute_pa_color']; // which is 'blue' in this case
    // I think it is the "custom field" you mean since all variation selected by the user are passed here. Most likely it is what you are looking for

    // should be your checking mechanism and result
    return true; // true to continue and false to stop
}

Hope it helps.

Edited: added arguments explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.