In this work, the chaotic motions of relativistic electrons in X-ray free-electron lasers are investigated using an optical undulator in the presence of a magnetized ion-channel background. To miniaturize X-ray light sources, the optical undulator is a promising concept. The optical undulator provides higher optical gain than conventional magnetostatic undulators due to its micrometer wavelength. In addition, it reduces the required electron beam energy from several GeV to the multi-MeV range to produce X-ray pulses. The interaction of an optical undulator with an intense relativistic electron beam is a highly non-linear phenomenon that can lead to chaotic dynamics. At synchrotron radiation sources, the possibility of chaos control for X-ray FELs can be critical for certain classes of experimental studies. The equations of motion for a relativistic electron propagating through the optical undulator in the presence of a magnetized ion-channel can be derived from the Hamiltonian of the interaction region. Simulation results revealed that the intensity of the perturbation route from orderly behavior to chaos depends on the beam density, axial magnetic field strength, ion-channel density parameter, and pump laser undulator. Specific values of parameters were obtained for the transition from regular to chaotic paths. Bifurcation diagrams of the system were plotted to demonstrate the origin of chaos at a critical point, and Poincaré maps were created to distinguish between chaotic and orderly motions of electrons. The proposed new scheme can help to improve X-ray FELs, which have potential usages in basic sciences, medicine, and industry.