• Resolved siutek

    (@siutek)


    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?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter siutek

    (@siutek)

    OK, thread to be closed.
    I made a mistake and, with the WC()->cart->add_to_cart() formula, I passed in the product ID in the argument, not the ID of the item in the cart. I changed it, everything works.

    Hi @siutek

    Thanks for reaching out!

    I’m glad you were able to find a solution to your inquiry here and thanks for sharing it with the community too! ??

    Cheers!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘$wc->cart->set_quantity() error’ is closed to new replies.