4

My requirement is here:-

I have a Game booking requirement in Magento 2, just like online movie booking(e.g- booking movie from Bookmyshow, Paytm etc). there is fix number of tickets in each slot per day.user can buy the ticket for the same day or for the future.

To achieve the same I've created a configurable product and assign associated products as slots.

for same venue and slot, ticket can be book from paytm as well as bookmyshow and many more...and ticket price may vary depending on the platform(bookmyshow,paytm etc).

so to synchronize the inventory I've chosen the configurable product concept and will assign the same associated product to each configurable product.

You can visit here for more clarification:-

https://nimb.ws/ZHiauz

My problem is that I've to manage the inventory date and slot wise.suppose for a date "dd-mm-yy" and slot 10AM-11AM(it may be same or future date,slots) someone has book 2 tickets out of 50 so now 48 tickets are left for same date and slot.

I've 3 option to achieve the same:-

1-Plugin.

2-Event.

3-Overriding.

But according to the Magento 2, a plugin is the best way to customize the things and I'm using as below:-

di.xml:-

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\CatalogInventory\Model\StockStateProvider">
        <plugin name="Wakar_Wakar_Plugin_StockStateProvider" sortOrder="1" type="Wakar\Stockcheck\Plugin\StockStateProvider"/>
    </type>
</config>

MethodName:-

public function checkQuoteItemQty(StockItemInterface $stockItem, $qty, $summaryQty, $origQty = 0)

Before Plugin:-

<?php

namespace Wakar\Stockcheck\Plugin;
class StockStateProvider
{
    public function beforeCheckQuoteItemQty(){
        echo "hey " ; exit;
    }
}

It's working fine.

But my question is that how can I use the parameter in the method beforeCheckQuoteItemQty() so that I can get the data of the original method **CheckQuoteItemQty() ??

I've also R&D for the plugin concept but still not clear on what basis/reference they are passing the parameters in the plugin methods? what're the criteria?how can we know that for beforeCheckQuoteItemQty() what parameter I need to take to hold the data here?

e.g-

I've gone through Webkul Plugin tutorial:-

https://webkul.com/blog/magento2-use-plugins/

And for the "beforeAddProduct()",they are using just like :-

public function beforeAddProduct(\Magento\Checkout\Model\Cart $subject,$productInfo,$requestInfo = null) {
            $requestInfo['qty'] = 10; // increasing quantity to 10
            return array($productInfo, $requestInfo);
        }

On what basis they are passing parameters "(\Magento\Checkout\Model\Cart $subject,$productInfo,$requestInfo = null)" how can we know what parameter we need to pass, what is the logic behind it??

After getting the data beforeCheckQuoteItemQty(), I can check the previous orders based on date and time slots and can control the quantity.

You can also suggest any alternate best solution for the same scenerio,waiting for your update.

Please help me to clear the plugin concept!!!

=================Modification==========================

How can I use before plugin for the class "Magento\CatalogInventory\Model\StockStateProvider.php" having the method "public function checkQuoteItemQty(StockItemInterface $stockItem, $qty, $summaryQty, $origQty = 0)" ??

Thanks!!!

  • I don't think I understood your question. but the parameters in the plugin are exactly what would be passed to the method. maybe check if there are other plugins on this method and work with plugin order – Philipp Sander Feb 7 at 12:19
  • thanks for the reply,I've fresh Magento 2 setup and before plugin is working fine.i want to get the original data of the method checkQuoteItemQty() using beforecheckQuoteItemQty() ,so that i can customize the thinghow can i? – wakar Ahamad Feb 7 at 13:17
  • @Bilal Usean is exactly posted what i want but still facing some problem to get the orignal method information. – wakar Ahamad Feb 7 at 13:18
2

The parameter $subject is the original object or class you are writing a plugin for, reference link

If you are writing before plugin,

Remaining parameters all are original method arguments.

If you are writing after plugin,

The second parameter should be $result, it contain the original method returns. Remaining parameters all are original method arguments.

e.g) If you want to change the below method arguments

public function setTitle($title,$name,$age)
{
    return $title.' - '.$name.' - '.$age;
}

You need to write before plugin like this

public function beforeSetTitle(\Flying\Birds\Controller\Index\Index $subject, $title, $name, $age)
{               
    $name = 'Bilal'; // change the input argument name
    return [$title,$name,$age];
}

In your case you need to write like

<?php

namespace Vendor\Module\Plugin;
use Magento\CatalogInventory\Api\Data\StockItemInterface;

class ExamplePlugin
{
    public function beforeCheckQuoteItemQty(\Magento\CatalogInventory\Model\StockStateProvider $subject, StockItemInterface $stockItem, $qty, $summaryQty, $origQty = 0)
    {
        //here your logic
    }
}   

You can find the full details about how to use those arguments here

  • thank you so much for the reply,you mean to i should write like:- public function beforeCheckQuoteItemQty(\Magento\CatalogInventory\Model\StockStateProvider $subject,StockItemInterface $stockItem, $qty, $summaryQty, $origQty = 0) ? – wakar Ahamad Feb 7 at 12:41
  • thanks again,you exactly defined "beforeCheckQuoteItemQty" same as me,now could you please let me know after "Add To Cart" in this plugin how i can get the product information like qty,options so that i can modify the things. – wakar Ahamad Feb 7 at 17:16
  • Any update please? – wakar Ahamad Feb 8 at 4:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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