Get product changes

后端 未结 3 1197
难免孤独
难免孤独 2021-02-14 00:33

Situation is:

Admin logs in to system and he changes product somehow.

For example:

Sets qty to 10 Adds 2 images Changes description

Is there any

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-14 01:21

    You can find the unaltered data through an object's getOrigData() method. A good time to get the object is through it's save_before event, so create an observer for the catalog_product_save_before event. The observer might contain the following:

    public function onCatalogProductSaveBefore($observer)
    {
      $product = $observer->getProduct();
      if ($product->hasDataChanges()) {
        $newValues = array_diff_assoc($product->getData(), $product->getOrigData());
        $oldValues = array_diff_assoc($product->getOrigData(), $product->getData());
        $added     = array_diff_key($product->getData(), $product->getOrigData());
        $unset     = array_diff_key($product->getOrigData(), $product->getData());
      }
    }
    

    Note that $newValues will include all of $added and existing attributes that have had their value changed. Ditto for $oldValues and $unset.

提交回复
热议问题