А вот документация по автоматическому API REST WooCommerce: https://packagist.org/packages /automattic/woocommerce
У меня есть внешний компонент — Adverts.vue, который частично выглядит так:
Код: Выделить всё
Синхронизирай с продуктите в уебсайта
export default {
data() {
return {
selectedAdvert: null,
adverts: [], // Array to store user adverts for the current page
allAdverts: [], // Array to store the complete list of user adverts
itemsPerPage: 10,
currentPage: 1,
loading: true,
editing: false,
};
},
computed: {
totalPages() {
return Math.ceil(this.adverts.length / this.itemsPerPage);
},
paginatedAdverts() {
const start = (this.currentPage - 1) * this.itemsPerPage;
const end = start + this.itemsPerPage;
return this.adverts.slice(start, end);
},
},
enter code here
mounted() {
console.log('Mounted lifecycle hook executed.');
// Check if the access token is available in the cookie
const accessToken = this.getCookie('olx_access_token');
if (!accessToken) {
console.log('Access token not available.');
} else {
console.log('Access token available. Fetching all user adverts.');
// Access token is available, fetch all user adverts
this.fetchAllUserAdverts(accessToken);
}
},
methods: {
async syncWithWebsite() {
try {
// Fetch the user's adverts data
const accessToken = this.getCookie('olx_access_token');
await this.fetchAllUserAdverts(accessToken);
// Send the adverts data to the Laravel controller
const response = await axios.post('/syncWithWooCommerce', {
adverts: this.allAdverts,
});
if (response.data.success) {
console.log('Synchronization successful!');
// Optionally, you can show a success message to the user
} else {
console.error('Synchronization failed:', response.data.message);
// Optionally, you can show an error message to the user
}
} catch (error) {
console.error('Error during synchronization:', error);
// Optionally, you can show an error message to the user
}
},
async fetchAllUserAdverts(accessToken) {
this.loading = true;
try {
// Fetch all user adverts and store them in allAdverts
this.allAdverts = await this.fetchUserAdverts(accessToken, 0, []);
// Update the adverts array with the paginated data from allAdverts
this.updatePaginatedAdverts();
} catch (error) {
console.error('Error fetching user adverts:', error);
} finally {
this.loading = false;
}
},
fetchUserAdverts(accessToken, offset, allAdverts) {
const apiUrl = `/olx/show-adverts?access_token=${accessToken}&offset=${offset}`;
return axios
.get(apiUrl, {
headers: {
Authorization: `Bearer ${accessToken}`,
Version: '2.0',
},
})
.then((response) => {
console.log('API Response:', response.data);
const currentAdverts = response.data.data;
// Append the current adverts to the existing array
allAdverts.push(...currentAdverts);
// Check if there are more pages to fetch
const nextPageExists = currentAdverts.length === this.itemsPerPage;
if (nextPageExists) {
// Fetch the next page recursively
return this.fetchUserAdverts(accessToken, offset + this.itemsPerPage, allAdverts);
} else {
// Return all adverts when no more pages are left
return allAdverts;
}
});
},
},
Код: Выделить всё
Подробнее здесь: [url]https://stackoverflow.com/questions/78479359/problem-with-laravel-vue-js-problems-with-synchronisation-between-products-f[/url]