在PHP中我如何使用其中一个参数是类的函数:

In magento 2.2.1 there is a function that i am trying to use that has a class as a parameter:

public function add($sku, \Magento\Catalog\Api\Data\ProductTierPriceInterface $tierPrice);

This used to just have 4 parameters that were passed to it and was used like below:

$this->tierInterface->add($sku,$groupId,$price, '1');

Now however the second parameter needs to be used in place of these old parameters where \Magento\Catalog\Api\Data\ProductTierPriceInterface $tierPrice has within the below methods that i need to use instead:

  • public function setQty($qty);
  • public function setValue($value);
  • public function setCustomerGroupId($customerGroupId);

How can i use these new methods to correctly pass the values to the function?

In Magento 2 there is a concrete implementation of ProductTierPriceInterface that you should use which is named TierPrice.

Your new code would look like this :

# Pretty sure dependency injection should be needed there.
$tierPrice = new TierPrice();
$tierPrice->setQty(1);
$tierPrice->setValue($price);
$tierPrice->setCustomerGroupId($groupId);

$this->tierInterface->add($sku, $tierPrice);