Praise be to Allah, and may Allah’s peace and blessings be upon the Messenger of Allah, his family and his companions.
In response to your question, I say:
There are two scholarly opinions regarding participating in slaughtering an animal between a person with an intention of sacrificing to draw closer to Allah and another one with an intention of benefiting from the meat:
First: It is permissible to participate in slaughtering whether slaughtering is compulsory or voluntary, and regardless of all of them intending to get closer to Allah by slaughtering or some of them just wanting the meat out of it. This is the view held by the Shafites and Hanbalites.
They deduced their opinion from a hadith transmitted by Muslim (1318) on the authority of Jabir (may Allah be pleased withhim) who said: “We set out in the state of Ihram for Hajj along, with Allah's Messenger (peace be on him). He commanded us that seven persons should join in a camel and a cow for offering sacrifice.” And in another narration it is stated that: “We joined Allah's Apostle (peace be on him) in Hajj and Umra and seven persons shared in the sacrifice of an animal.” The angle of evidence is that during pilgrimage there is compulsory sacrifice and supererogatory sacrifice. The evidence that these two types of getting closer to Allah do not have an impact on sacrifice indicates that difference of intention does not have an effect on validity of sharing in sacrifice.
Second: It is not permissible to participate in slaughtering between a person intending to get closer to Allah and another one who has a different intention. This is because only one animal will be slaughtered; therefore it is not permissible for part of it to be intended for drawing closer to Allah and the other part for not drawing closer to Allah.
What seems to be more correct is that it is not permissible to participate in sacrificing except with a person who has an intention of getting closer to Allah because the permission was given on this form of sharing in sacrifice. The difference in type of sacrifice (compulsory and supererogatory) does not have any effect because both will be intending getting closer to Allah, contrary to a case where some people will be intending to only benefit from the meat.
And Allah knows best.