8

Can anyone explain below code? How did it work?

Is there any alternate way to do?

 <a href="#" data-post='<?php  echo $this->helper('Magento\Wishlist\Helper\Data')->getAddParams($_product) ?>' class="action towishlist" data-action="add-to-wishlist"><span>Add to Wishlist</span></a>
0

2 Answers 2

16

As you can see in widget lib/web/mage/dataPost.js

$.each(this.options.postTrigger, function (index, value) {
    events['click ' + value] = '_postDataAction';
});

On click to elements which satisfy the selectors ['a[data-post]', 'button[data-post]', 'span[data-post]'] will be created and submitted form with params, specified in data-post attribute value.

Additionally, you can specify confirmation message

if (params.data.confirmation) {
    uiConfirm({
        content: params.data.confirmationMessage,
        actions: {
            /** @inheritdoc */
            confirm: function () {
                $form.appendTo('body').hide().submit();
            }
        }
    });
} else {
    $form.appendTo('body').hide().submit();
}
4
  • 1
    This is the correct answer. Nov 17, 2017 at 16:28
  • Max as usual krosava!!
    – zhartaunik
    Dec 7, 2017 at 7:57
  • 1
    @zhartaunik feel free to upvote my answers :D
    – Max
    Dec 7, 2017 at 14:49
  • I have button with this kind of data-post and I want to disbale that button once submit event fired. Example, there is custom addtocart button, and after clicking once that button, it will be disabled and page will be redirected to my cart with that item. I have done all functionality. Just I need to prevent multiple time page submit with this data-post variable containging button.
    – Dolly
    Nov 28, 2018 at 13:43
0

If you go to the MagentoInstallationDirectory/vendor/magento/module-wishlist/Helper/Data.php You will see the getAddParams function which describes how product is added to the wishlist.

1
  • what is data-post and data-action? How it works?
    – jassi
    Nov 17, 2017 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.