Php how to make something like an order basket. Shopping cart for an online store at the front or Writing modular javascript. Step-by-step instructions for creating a shopping cart

26.06.2024 external HDs

Which was intended only to show an abstract example of the implementation of a shopping cart, I did not think that it would generate so many questions and requests that poured in on the blog itself, and in my email, etc. Actually, that’s why I decided to write this plugin “jqCart”, but I want to immediately emphasize a few points:

  • Although the code is designed as a plugin, it’s a stretch to call it a plugin. And in general, in this direction, in my opinion, making a plugin completely universal is quite difficult;
  • The plugin was written in a hurry, so it’s quite crude, although it’s quite functional;
  • At the moment, the code is not documented;
  • Do I plan to improve it? Yes, but with enough free time;
  • So, for the plugin to work, the jQuery library >= 1.8 is required, which must be connected before connecting the plugin itself. It should work in all modern browsers and, in theory, even in IE8. The problem for old "donkeys" may only lie in the applied CSS properties and the version of jQuery ( Let me remind you that jQuery 2.x does not support Internet Explorer 6, 7, and 8). Data is transferred to the server using Ajax and therefore, I highly recommend using UTF-8 file encoding without BOM!

    Connection:

    Usage:

    $(function() ( "use strict"; // initializing the plugin $.jqCart(( buttons: ".add_item", // button selector, ala "Add to cart" handler: "/php/handler.php", / / path to the handler visibleLabel: false, // show/hide the label when the cart is empty (default: false) openByAdding: false, // automatically open the cart when adding a product (default: false) currency: "€", // currency : string value, mnemonics (default "$") cartLabel: ".label-place" /* selector of the element where the label will be placed, also known as the "button" for opening the cart */ )); // additional methods $. jqCart("openCart"); // open cart $.jqCart("clearCart"); // clear cart ));

    In the buttons ( "Add to Basket"), the following data attributes must be specified:

  • data-id - Product ID
  • data-title - Product name
  • data-price - Product price
  • data-img - URL of product photo ( optional)
  • All values ​​of the above data attributes take part in the formation of the cart window. You can add additional data attributes, the values ​​of which will be sent with the rest of the data to the handler. The keys in the resulting array on the server will correspond to the attribute name after “data-”, i.e. the value of the attribute "data-somevalue" will be in the array under the key "somevalue" The tag for the button and its location on the page do not matter.

    Add to Basket

    In the archive you will find an example of a handler (handler.php) with preparing and sending a letter by email. At the end of the handler, there must be a response to the client in JSON format.