I had a function that was setting category minimums and maximums. It was working until I upgraded to Woocommerce 3.0. I don't think the upgrade should have caused it to stop working.
It's throwing the following error:
Notice: qty was called incorrectly. Product properties should not be accessed directly. Backtrace: require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/alienship/page.php'), get_template_part, locate_template, load_template, require('/themes/alienship/templates/parts/content-page.php'), the_content, apply_filters('the_content'), WP_Hook->apply_filters, call_user_func_array, do_shortcode, preg_replace_callback, do_shortcode_tag, call_user_func, WC_Shortcodes::cart, WC_Shortcodes::shortcode_wrapper, call_user_func, WC_Shortcode_Cart::output, do_action('woocommerce_check_cart_items'), WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, wc_minimum_order_amount_seals, WC_Abstract_Legacy_Product->__get, wc_doing_it_wrong Please see Debugging in WordPress for more information. (This message was added in version 3.0.) in ..../wp-includes/functions.php on line 4138
It's getting stuck on: $_product_qty = $_product->qty;
When I echo $_product_qty
it has no value.
Research I've done:
I've found this resource from Woocommerce that states "each property has a get_ getter and set_"
https://github.com/woocommerce/woocommerce/wiki/Product-Data-Schema#abstract-product
and this woocommerce resource:
https://github.com/woocommerce/woocommerce/wiki/Order-and-Order-Line-Item-Data#line-item-product
as well as this stackoverflow referring to using get_id() under the answer: How to get product title in cart in new woocommerce 3.x
What I tried get it to work:
I tried:
$_product_qty = $_product->get_stock_quantity();
$_product_qty = $_product->get_qty();
$_product_qty = $_product->qty;
Here is the whole code with line with qty reference highlighted:
//check at cart if category rules min and max are met for foil and seal items
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount_seals' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount_seals' );
add_action( 'woocommerce_check_cart_items' , 'wc_minimum_order_amount_seals' );
function wc_minimum_order_amount_seals() {
// minimum order value
$foil_min = 25000;
$foil_max = 50000;
$seal_min = 10000;
$seal_max = 60000;
$_seals_cat_qty = 0;
$_foil_cat_qty = 0;
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
$products_min = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$_product_id = $_product->get_id();
//
// This is the trouble-maker:
$_product_qty = $_product->qty;
//
//
$terms = get_the_terms( $_product_id, 'product_cat' );
foreach ($terms as $term) {
if($term->term_id === 205){
$_seals_cat_qty +=$_product_qty;
}else if($term->term_id === 206){
$_foil_cat_qty +=$_product_qty;
}
}
// if your products categories are present check min/max
if(is_cart() || is_checkout()) {
if($_seals_cat_qty > 0){
if($_seals_cat_qty < $seal_min){
wc_print_notice(
sprintf( 'You must order at least %s total Seal Stickers, please add %s Gold or Silver Seal Stickers to your order.' ,
$seal_min ,
$seal_min - $_seals_cat_qty
), 'error'
);
}else if($_seals_cat_qty > $seal_max){
wc_print_notice(
sprintf( 'You must order less than %s total Seal Stickers, please remove %s Gold or Silver Seal Stickers from your order.' ,
$seal_max,
$_seals_cat_qty - $seal_max
), 'error'
);
}
}
if($_foil_cat_qty > 0){
if($_foil_cat_qty < $foil_min){
wc_print_notice(
sprintf( 'You must order at least %s total Foil Badges, please add %s White or Black Foil Badges to your order.' ,
$foil_min,
$foil_min - $_foil_cat_qty
), 'error'
);
}else if($_foil_cat_qty > $foil_max) {
wc_print_notice(
sprintf( 'You must order less than %s total Foil Badges, please remove %s White or Black Foil Badges from your order.' ,
$foil_max ,
$_foil_cat_qty - $foil_max
), 'error'
);
}
}
}
}
}
}