• Resolved pacificshack

    (@pacificshack)


    Hi there,
    I’ve got a function running in my cart which adds a one off fee for a Logo upload:

    add_action( 'woocommerce_before_calculate_totals', 'cp_add_custom_price' );
    	function cp_add_custom_price( $cart_object ) {
    
    		// Some query here
    
    	    global $woocommerce;
    	    foreach ( $cart_object->cart_contents as $key => $value ) {
    	        //Get the product data like below
    	        $proid = $value['product_id'];
    	        $variantid = $value['variation_id'];
    	        $price = $value['data']->price;
    
    	        //check your condition
    	        if($youcondition == true ) {
    	            //update with the new price
    	            $value['data']->price = $newprice;
    	        }
    	    }
    
    	    $excost = 99;
    		$woocommerce->cart->add_fee( 'Logo set up fee', $excost, $taxable = false, $tax_class = '' );
    
    	}

    This works well by adding a one off fee of $99 to the cart, regardless of quantity.
    The issue i have, is I want to make this conditional if an item has been previously been selected.

    I have Gravity Forms running with Product Add ons, and if Logo Upload is selected it passes through a value of $0 to the cart, and says Logo upload: yes.

    If Logo upload is yes, I want to add the fee, if it says No, I don’t want to add the fee.

    Any ideas?

    BTW: I cant just add the fee on the product page, otherwise the quantity box picks it up and multiples it!

    Have tried using get_post_meta to try and look to see if it is there and can’t seem to get it work.

    Example of product in cart:
    Testing product
    Size:
    A4

    Colour:
    Dark green

    Corners:
    No

    PVC Pockets:
    A4

    Quantity:
    1

    Logo upload:
    Yes

    https://wordpress.org/plugins/woocommerce/

Viewing 1 replies (of 1 total)
  • Thread Starter pacificshack

    (@pacificshack)

    Have found a solution for adding a one off fee to cart, depending on the post meta:

    In functions.php file

    // WooCommerce hook to add LogoUpload value to cart
    add_action( 'woocommerce_before_calculate_totals', 'cp_add_custom_price' );
    function cp_add_custom_price( $cart_object ) {
    
        global $woocommerce;
        foreach ( $cart_object->cart_contents as $key => $value ) {
    
            //$proid = $value['product_id'];
            //$variantid = $value['variation_id'];
            //$price = $value['data']->price;
            $cartItemData = $woocommerce->cart->get_item_data($value);
    
            if (preg_match("/Yes, upload logo/i", $cartItemData)) {
    		    $excost = 99;
    			$woocommerce->cart->add_fee( 'Logo set up fee', $excost, $taxable = false, $tax_class = '' );
    		}
    
       }
    }

    This adds the function to the cart. It gets the item data, and uses a php function to scan the content. If it matches a certain term, add the fee, if it doesn’t don’t add the fee.

    Done.

Viewing 1 replies (of 1 total)
  • The topic ‘One off fee added to cart’ is closed to new replies.