Sorry guys, had to post this again as site wouldn't let me respond to comments (new account).
I have been working on some code to decrease quantity of a variable product based on the numeric values of a custom field.
This is working fine, however I have implemented a checkbox that will essentially TURN ON/OFF the feature on each product. This custom checkbox also works fine, appears in 'Product General Settings' and is storing the data fine - I have confirmed this with a VAR_DUMP() on the product page that is accurately dumping yes - (length = 3).
Although the data is storing, I can't seem to use it as a condition and when that condition is in place if ( 'yes' == $stock_weight_checkbox ) the correct decrease in quantity does not work.
I can't seem to get a VAR_DUMP() to work during this process either as it takes place during the checkout confirmation, so I can't see if any values are being displayed there at that point.
My assumptions are as follows: I am improperly calling the post_meta in this area (checkout). Perhaps this call works on the product page but I am missing something or the way I am approaching the call is incorrect.
This is a variable product that is looking for a custom field in the General Settings area during checkout, at which point it is unavailable? (Guess)
Below is the code:
// reduce stock based on 'custom_field'
add_filter( 'woocommerce_order_item_quantity', 'filter_order_item_quantity', 10, 3 );
function filter_order_item_quantity( $quantity, $order, $item )
{
$product = $item->get_product();
$term_name = $product->get_meta( 'custom_field', true );
$stock_weight_checkbox = $product->get_meta( '_stock_weight_checkbox', true );
// 'pa_weight' attribute value is "15 grams" - keep only the numbers
$quantity_grams = preg_replace('/[^0-9.]+/', '', $term_name);
// new quantity
if( 'yes' == $stock_weight_checkbox && is_numeric ( $quantity_grams ) && $quantity_grams != 0 )
$quantity *= $quantity_grams;
return $quantity;
}
function validate_attribute_weight( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
// Get custom field
$weight = get_post_meta( $variation_id, 'custom_field', true );
$stock_weight_checkbox = get_post_meta( get_the_id(),'_stock_weight_checkbox', true );
if ( 'yes' == $stock_weight_checkbox && ! empty( $weight ) ) {
// Get product object
$product = wc_get_product( $product_id );
// 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;
}
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'validate_attribute_weight', 10, 5 );
Can anyone notice something wrong? Or at the very least, let me know how I can view the VAR_DUMP() during the checkout process? (I'm used to just spitting it out on some part of my page ie: product page)