Programming Notes

Table of Contents

Rental Booking API Guide & Integrations #

Learn How To Program In Magento 2 #

Alan Storm has the best and most thorough Magento 2 documentation, we recomend following along with your IDE doing the sample projects to really see how Magento 2 works:  http://alanstorm.com/category/magento-2/

Mage Plaza has a series with a very thorough sample module that shows most parts of the Magento 2 system: https://www.mageplaza.com/magento-2-module-development/

Our Favorite Windows Developer Environment For Magento 2 #

We have tried quite a few Windows stacks for Magento 2 development including WAMP and AMPPS but please take a look at https://wtserver.wtriple.com/ it has speed on par with a real Linux machine because it uses Nginx and PHP-CGI with multiple running processes at a time. When I compared it with my Ubuntu Nginx PHP-FPM the speed was almost the same, whereas with WAMP and AMPPS the speed usually lagged behind about 50 – 70% from the Ubuntu VM.

Where Should Customizations Be Done? #

As is the standard in Magento, NEVER in the base module (vendor/salesigniter/releaserental2), you will need to create your own module and do your customizations there, otherwise your work would be overwritten upon updating the rental module. 

Notes On Our Rental Booking Module #

How Do I? #

1) Delete a reservation for an order? #

app/code/SalesIgniter/Rental/Api/StockManagementInterface.php inject this class

then use $this->StockManagement->deleteReservationsByOrderId($orderId)

2) Retrieve quantity available for a rental product for certain dates? #

app/code/SalesIgniter/Rental/Api/StockManagementInterface.php inject this class

then call it like $this->stockManagement->getAvailableQuantity($Product, $StartDate, $EndDate)

3) Get the rental dates for an order? #

inject app/code/SalesIgniter/Rental/Helper/Calendar.php

then $this->Calendarhelper->getDatesForOrder($order)

4) Retrieve total quantity of a rental product (without taking into account reservations)? #

app/code/SalesIgniter/Rental/Api/StockManagementInterface.php inject this class

then call it like $this->stockManagement->getSirentQuantity($Product)

5) Update the quantity or rental dates for an existing order? #

app/code/SalesIgniter/Rental/Api/StockManagementInterface.php inject this class

then use $this->StockManagement->saveReservation($reservation,$data) look at the method saveFromArray phpdoc in this class to see what $data array expects

6) Do my Own Time & Date Calculations? #

In your class at the top add:

Use League\Period\Period;

Then in your code:
$period = new Period($start, $end);
$period2 = new Period($start2, $end2);
// check if overlaps
$period->overlaps($period2);
More examples and API: http://period.thephpleague.com/examples/

Rental Calendar Info #

1) How does the rental calendar widget work? #

app/code/SalesIgniter/Rental/etc/widget.xml is where it is configured in XML this calls the class:

app/code/SalesIgniter/Rental/Block/Widget/CalendarWidget.php which makes the calendar

2) What are the principles behind our datepicker? #

app/code/SalesIgniter/Rental/view/frontend/web/js/pprdatepicker.js extends via JavaScript prototype the Magento 2 jquery datepicker with various functions necessary for rentals. This is then set to “pprdatepicker” in app/code/SalesIgniter/Rental/view/frontend/requirejs-config.js

3) How does the product page show the calendar and the rental dates get passed from the calendar to the cart? #

On the customer side product page, the calendar is added via app/code/SalesIgniter/Rental/Plugin/Catalog/Template.php aroundFetchView plugin method there are lines:

if ( $subject ->getNameInLayout () === 'product.info.options.wrapper' && $this -> _helperRental-> isFrontend ()) {
  /** @var pQuery\DomNode $dom */
  $dom = pQuery:: parseStr( $html );
  return $this-> _hideStartEndCustomOptions ( $dom) ;
}

This goes to the hideStartEndCustomOptions method which hides the custom options and adds the calendar with this line: $dateFields[0]->parent->append($this->getCalendar()

The calendar html is generated from: app/code/SalesIgniter/Rental/Block/Widget/CalendarWidget.php getHtmlCalendar method

When rent is clicked, app/code/SalesIgniter/Rental/Observer/PrepareOptionsObserver.php this observer takes the dates from the buy request (buy request takes any extra data from the product page form) and saves it to the start and end date custom options.

4) How are rental prices calculated? #

app/code/SalesIgniter/Rental/Model/Product/Price.php getFinalPrice method, if we are on the product page of the customer side ajax is used but it uses this same method. Ajax can be found here: app/code/SalesIgniter/Rental/Controller/Ajax/Price.php

5) How does the customer side calendar load disabled dates (dates which are booked or set disabled by the admin)? #

app/code/SalesIgniter/Rental/view/frontend/web/js/pprdatepicker.js has an ajax call:

$.ajaxq('booked', {
                url: this.options.updateBookedUrl,
                data: dataFormSerialized,

here it is calling app/code/SalesIgniter/Rental/Controller/Ajax/Booked.php which gets all the booked dates, disabled dates, first date available, etc and returns it as a json object. You can see this ajax call if you go to inspect element > network tab in your browser.

Misc Notes On Our Rental Module #

1) How are the pricing and serial grids added to the product edit page? #

app/code/SalesIgniter/Rental/Ui/DataProvider/Product/Form/Modifier/ReservationAdvancedPricing.php

Backend Model For Saving: app/code/SalesIgniter/Rental/Model/Attribute/Backend/RentalPrice.php

app/code/SalesIgniter/Rental/Ui/DataProvider/Product/Form/Modifier/SerialNumbers.php

Backend Model For Saving: app/code/SalesIgniter/Rental/Model/Attribute/Backend/SerialNumbers.php

3) How is the jQuery UI theme set in the code? #

app/code/SalesIgniter/Rental/view/frontend/templates/widgets/calendar.phtml

‘SalesIgniter_Rental::css/datepicker/’ . $themeStyle . ‘/jquery-ui.theme.css’

4) How are qty booked and available added to the products > catalog grid? #

app/code/SalesIgniter/Rental/Ui/Component/Listing/Column/QtyAvailable.php

app/code/SalesIgniter/Rental/Ui/Component/Listing/Column/QtyBooked.php

app/code/SalesIgniter/Rental/view/adminhtml/ui_component/product_listing.xml adds the columns look at the xml:

<column name=”qty” class=”SalesIgniter\Rental\Ui\Component\Listing\Column\Qty”>…</column>

5) How are start date and end date added to the sales > orders grid?  #

app/code/SalesIgniter/Rental/view/adminhtml/ui_component/sales_order_grid.xml adds the columns

app/code/SalesIgniter/Rental/Plugin/AdditionalFields/Provider.php plugin is used to join the reservationorders table that has the start_date and end_date for the grid

6) Admin Edit Product Rental fields how does it disable/enable the setting based on the checkbox “use config settings” and add validation? #

app/code/SalesIgniter/Rental/Ui/DataProvider/Product/Form/Modifier the files in this folder correspond to the config settings, they are injected via the app/code/SalesIgniter/Rental/etc/adminhtml/di.xml file with code like:

<item name =”minPeriod” xsi:type =”string” > SalesIgniter\Rental\Ui\DataProvider\Product\Form\Modifier\MinPeriod </item>

The validation file is: app/code/SalesIgniter/Rental/view/adminhtml/web/js/form/element/rental-period.js

7) How are the rental dates added to the admin create order page? How are the dates stored in session? #

app/code/SalesIgniter/Rental/Plugin/Catalog/Template.php the lines

if ( $this ->_helperRental -> isBackendAdminOrderEdit () && $subject -> getNameInLayout() === 'items' ) {
  /** @var pQuery\DomNode $dom */
  $dom = pQuery:: parseStr( $html );
  return $this-> _appendCalendarAdmin ( $dom) ;
}

When go button is clicked there is an event bound in app/code/SalesIgniter/Rental/view/adminhtml/web/js/pprdatepicker.js

el.find(‘.sirent-go’).click(function () {

which further down uses ajax to call app/code/SalesIgniter/Rental/Controller/Adminhtml/Ajax/ChangeGlobalDates.php which updates the session dates

8) How is the rent button added? #

app/code/SalesIgniter/Rental/Plugin/Catalog/Template.php _renameButtons function

9) How do products have start and end dates tied to them?  #

Dates are saved as custom options. Rental products get added a start and end date to the custom options using: app/code/SalesIgniter/Rental/Plugin/Product/Initialization/Helper/CustomOptionsInit.php and app/code/SalesIgniter/Rental/Plugin/Product/AroundProductRepositorySave.php aroundSave plugin. This means that when you add a rental product in the admin the custom options are auto-added for you as placeholders for the start and end date.

10) How is a reservation stored in the db when an order is placed? #

app/code/SalesIgniter/Rental/Plugin/Sales/Api/OrderManagementInterface.php aroundPlace plugin is used which calls app/code/SalesIgniter/Rental/Api/StockManagementInterface.php reserveOrder method which directs to the reserveOrder method of app/code/SalesIgniter/Rental/Model/StockManagement.php

11) How are rental pricing tables rendered?  #

app/code/SalesIgniter/Rental/Plugin/Pricing/RenderPlugin.php aroundRenderAmount method checks if a product is a rental product. If it is, it calls app/code/SalesIgniter/Rental/Model/Product/PriceCalculations.php getPriceListHtml method to get the price table.