$wc->cart->set_quantity() error | WordPress.org

[ad_1]

Hi!
I am trying to programmatically change the quantity of a particular product in the shopping cart after the buyer has met several conditions, but I get a critical error.

} elseif ( $gift_quantity < $current_gift_qt ) { $woocommerce->cart->set_quantity( $giftID, $gift_quantity );
}

After calling this code, Fatal Error appears on the page, and in the log file there is such an entry:

[26-Jun-2023 07:19:24 UTC] PHP Fatal error: Uncaught Error: Call to a member function get_tax_class() on null in /var/www/html/wp-content/plugins/woocommerce/includes/class-wc-cart-totals.php:225
Stack trace: 0 /var/www/html/wp-content/plugins/woocommerce/includes/class-wc-cart-totals.php(651): WC_Cart_Totals->get_items_from_cart() 1 /var/www/html/wp-content/plugins/woocommerce/includes/class-wc-cart-totals.php(143): WC_Cart_Totals->calculate_item_totals() 2 /var/www/html/wp-content/plugins/woocommerce/includes/class-wc-cart-totals.php(134): WC_Cart_Totals->calculate() 3 /var/www/html/wp-content/plugins/woocommerce/includes/class-wc-cart.php(1401): WC_Cart_Totals->__construct(Object(WC_Cart)) 4 /var/www/html/wp-content/plugins/woocommerce/includes/class-wc-cart.php(1360): WC_Cart->calculate_totals() 5 /var/www/html/wp-content/themes/niko-theme/functions.php(813): WC_Cart->set_quantity('499924', 2.0) 6 /var/www/html/wp-includes/class-wp-hook.php(308): add_or_remove_gift_from_cart('') 7 /var/www/html/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters('', Array) 8 /var/www/html/wp-includes/plugin.php(517): WP_Hook->do_action(Array) 9 /var/www/html/wp-content/plugins/woocommerce/includes/class-wc-cart-session.php(267): do_action('woocommerce_car…') 10 /var/www/html/wp-includes/class-wp-hook.php(308): WC_Cart_Session->set_session(Object(WC_Cart)) 11 /var/www/html/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters('', Array) 12 /var/www/html/wp-includes/plugin.php(517): WP_Hook->do_action(Array) 13 /var/www/html/wp-content/plugins/woocommerce/includes/class-wc-cart-session.php(208): do_action('woocommerce_car…', Object(WC_Cart)) 14 /var/www/html/wp-includes/class-wp-hook.php(308): WC_Cart_Session->get_cart_from_session('') 15 /var/www/html/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters(false, Array) 16 /var/www/html/wp-includes/plugin.php(517): WP_Hook->do_action(Array) 17 /var/www/html/wp-settings.php(639): do_action('wp_loaded') 18 /var/www/html/wp-config.php(101): require_once('/var/www/html/w…') 19 /var/www/html/wp-load.php(50): require_once('/var/www/html/w…') 20 /var/www/html/wp-blog-header.php(13): require_once('/var/www/html/w…') 21 /var/www/html/index.php(17): require('/var/www/html/w…') 22 {main}

Any idea?

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer