I’m working on the native BlockWishlist wishlist module. The idea is to add an option in the filtering system by making it customizable by the customer. I added a “custom_order” column to the “ps_wishlist_product” table with the default value of 10000 (so that when sorting the unsorted products end up last). When the “customizable” option is enabled, arrows appear next to products to move products up and down and create a new order. The idea is to record the new order in the database with each move. Initially I wanted to do it in local storage, but if the client changes positions it can be annoying for them to lose their list.
I edited the VueJS view of ProductListContainer.vue in order to add the option and also to be able to order using the vue-draggable module. I can move my elements well. I use Axios because I understand how it works, unlike GraphQL which already seems more technical for what I want to do. So I have two functions: saveOrder and loadOrder.
- saveOrder allows you to save the position in the list in custom_order (in the database)
- loadOrder allows to display in order when user chooses “Custom” in sorting options.
My loadOrder function works well but it’s saveOrder where I have a problem.
Here is the saveOrder method in my VueJS:
async saveOrder() {
console.log('Saving order...');
const orderData = this.filteredProducts.map((product, index) => ({
productId: product.id,
customOrder: index + 1,
}));
try {
await axios.post('/index.php?fc=module&module=blockwishlist&controller=action&action=saveOrder', {
wishlistId: this.listId,
orderData: JSON.stringify(orderData),
}).then((response) => {
if (response.data.success) {
console.log('Order saved successfully!');
} else {
console.error('Failed to save order:', response.data.message);
}
});
} catch (error) {
console.error('Error saving order:', error);
}
},
On the controller side I work in /blockwishlist/controllers/front/js/action.php
public function postProcess()
{
if (false === $this->context->customer->isLogged()) {
$this->ajaxRender(
json_encode([
'success' => false,
'message' => $this->trans('You aren't logged in', [], 'Modules.Blockwishlist.Shop'),
])
);
exit;
}
$params = Tools::getValue('params');
$action = Tools::getValue('action');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['wishlistId']) && isset($_POST['orderData'])) {
// Récupérez les valeurs de 'wishlistId' et 'orderData'
$wishlistId = $_POST['wishlistId'];
$orderData = json_decode($_POST['orderData'], true);
}
}
// Here we call all methods dynamically given by the path
if ($action === 'loadOrder') {
call_user_func([$this, 'loadCustomOrder']);
exit;
}
if ($action === 'saveOrder') {
call_user_func([$this, 'saveCustomOrder'], $wishlistId, $orderData);
exit;
}
if (method_exists($this, $action . 'Action')) {
call_user_func([$this, $action . 'Action'], $params);
exit;
}
$this->ajaxRender(
json_encode([
'success' => false,
'message' => $this->trans('Unknown action', [], 'Modules.Blockwishlist.Shop'),
])
);
exit;
}
// Function to save the custom order of products
protected function saveCustomOrder($wishlistId, $orderData)
{
// Parcourir chaque élément de la commande
foreach ($orderData as $item) {
// Mettre à jour la table ps_wishlist_product avec le nouvel ordre personnalisé
Db::getInstance()->update(
'ps_wishlist_product',
['custom_order' => (int)$item['customOrder']], // Nouvelle valeur de custom_order
'id_wishlist = ' . (int)$wishlistId . ' AND id_product = ' . (int)$item['productId'] // Conditions de mise à jour
);
}
// Renvoyer une réponse JSON indiquant le succès de l'opération
$this->ajaxRender(json_encode(['success' => true]));
}
The problem is that the parameters entered in Axios do not seem to reach the controller. After various tests, the values that go into:
$wishlistId = $_POST['wishlistId'];
$orderData = json_decode($_POST['orderData'], true);
are empty (NULL). I would like to know how to get the values from Axios into my PHP controller.
Send information to ajax
Revieved data
Vincent letb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.