• [ Moderator note: moved to Developing with WordPress. ]

    I am not able to run two functions at the same time while using a filter.

    I am trying to charge users as per checkboxes selection. But with my code, as given bellow, I am only able to process one payment (woo-commerce product) at a time.

    I am using toolset types plugin for custom fields, cred form & cred commerce plugins to charge for posting ads and woocommerce to process the payment. In my form, I have 2 addons as checkboxes that if users click when posting are charged accordingly.

    // Function for urgent-banner
    add_filter ('cred_commerce_add_product_to_cart','my_hook',10,3);
    function my_hook($product_id, $form_id, $post_id)
    
    {
    // product id is the current associated product ID
    // form_id is the cred commerce form id
    // post id is the post created or edited with the current form
    
    if (get_post_meta(get_the_ID(), 'wpcf-urgent-ad', true)) {
    $product_id = 687;
    //$product_id = 209;
    }
    
    return $product_id; 
    }
    
    //Another function for Home page slider carousel
    
    add_filter ('cred_commerce_add_product_to_cart','my_hookc',9,3);
    function my_hookc($product_id, $form_id, $post_id)
    
    {
    // product id is the current associated product ID
    // form_id is the cred commerce form id
    // post id is the post created or edited with the current form
    
    if (get_post_meta(get_the_ID(), 'wpcf-home-page-carousel', true)) {
    $product_id = 209;
    }
    
    return $product_id; 
    }
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    The two functions are running, but there can only be one end result no matter how many you run. Which ever function runs last has the final say in what product is added. It’s no different than having only one function that sets the ID to different values. Only the last value set can be returned, the interim values set previously do nothing.

    If you want to add multiple products as surcharges, each charge needs to have its own add to cart function called. Using filters only changes what product is added, it does not add more. While you could conceivable call the add to cart function from within the filter callback, but then you need to take care to not cause an infinite loop situation. Calling add to cart causes the filter callback to run, which calls add to cart, causing the filter callback to run which calls…

    An alternative is to have different products that include surcharges. Then the filter can change the base product to one with the appropriate surcharges included. But then your callback needs to know what option was checked. I think this is not available in the callback unless the entire add to cart routine is altered. I’m assuming add to cart is an Ajax process. If it’s a form submit process, the checkbox state can be determined from $_POST.

Viewing 1 replies (of 1 total)
  • The topic ‘Using WordPress add_filter hook multiple times for addons’ is closed to new replies.