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

    (@chillifish)

    The discount is being applied on the item line in the cart, but the subtotal and total are not being calculated and applied. I have a custom function that seems to be interrupting it, but not sure why or where.

    // add unpaid entries to cart
    add_action( 'pre_get_posts', 'add_entries_to_cart' );
    
    function add_entries_to_cart() {
    	global $woocommerce;
    
    	// Check we are logged in
    	if ( is_user_logged_in() ) {
    
    		// Check if we are in admin area
    		if ( ! is_admin() ) {
    			$user = wp_get_current_user();
    			$entries = get_user_meta($user->ID, 'entry_count', true);
    			$entries_paid = get_user_meta($user->ID, 'paid_entries', true);
    
    			$product_id = 229;
    			$quantity = $entries - $entries_paid;
    
    			$found = false;
    			//check if product already in cart, get id
    			if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
    				foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    					$_product = $values['data'];
    					if ( $_product->id == $product_id ) {
    						// correct quantity
    						// Get it's unique ID within the Cart
    						$prod_unique_id = $woocommerce->cart->generate_cart_id( $product_id );
    						$woocommerce->cart->set_quantity( $prod_unique_id, $quantity );
    						$found = true;
    					}
    				}
    				// if product not found, add it
    				if ( ! $found ) {
    					$woocommerce->cart->add_to_cart( $product_id, $quantity );
    				}
    			}
    			// no product in cart so add it
    			else {
    				// add to cart
    				$woocommerce->cart->add_to_cart( $product_id, $quantity );
    			}
    		}
    	}
    }
    Thread Starter Chillifish

    (@chillifish)

    It’s related to these two lines:

    $prod_unique_id = $woocommerce->cart->generate_cart_id( $product_id );
    $woocommerce->cart->set_quantity( $prod_unique_id, $quantity );

    If I remove them it works, but people can change the quantity of items in their cart, which I don’t want.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Discount not applied’ is closed to new replies.