0

I am using a shell script with Magento 1 to update the tier price depending on manufacturer. How can I adapt this for Magento 2

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Shell
 * @copyright   Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

require_once 'abstract.php';

class Mage_Shell_Staffelpreise extends Mage_Shell_Abstract
{
    /**
     * Run script
     *
     */
    public function run()
    {
        if ($this->getArg('run')) {
            $collection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSelect('manufacturer')
                ->addAttributeToSelect('price');

            $size = $collection->getSize();
            $i = 0;

            foreach ($collection as $product) {
                $manufacturer = $product->getAttributeText('manufacturer');

                switch ($manufacturer) {
                    case 'levis':
                    case 'boss':
                    case 'barbarossa':
                        $this->_updateTierprice($product);
                        break;
                }

                echo ++$i.'/'.$size.PHP_EOL;
            }
        } else {
            echo $this->usageHelp();
        }
    }

    protected function _updateTierprice($product)
    {
        /** @var $product Mage_Catalog_Model_Product */
        #$product->load( 'sku', $product->getSku() );
        if ($product->getId()) {
            $dbc = Mage::getSingleton('core/resource')->getConnection('core_write');
            $resource = Mage::getSingleton('core/resource');
            $table = $resource->getTableName('catalog/product').'_tier_price';
            $dbc->query("DELETE FROM $table WHERE entity_id = " . $product->getId());

            $origPrice = $product->getPrice();

            // Staffelpreis Kundengruppe 5 / 5Stk = 35% / 10Stk. = 40%
            $tierPrices[] = array(
                'website_id'  => 0,
                'cust_group'  => 5,
                'price_qty'   => 5,
                'price'       => ($origPrice * 0.65)
            );
            $tierPrices[] = array(
                'website_id'  => 0,
                'cust_group'  => 5,
                'price_qty'   => 10,
                'price'       => ($origPrice * 0.6)
            );

            // Staffelpreis Kundengruppe 6 / 5Stk = 35% / 10Stk. = 40%
            $tierPrices[] = array(
                'website_id'  => 0,
                'cust_group'  => 6,
                'price_qty'   => 5,
                'price'       => ($origPrice * 0.65)
            );
            $tierPrices[] = array(
                'website_id'  => 0,
                'cust_group'  => 6,
                'price_qty'   => 10,
                'price'       => ($origPrice * 0.6)
            );

            foreach ($tierPrices as $tierPrice) {
                $dbc->query(
                    "INSERT INTO $table (
                      `entity_id`,
                      `all_groups`,
                      `customer_group_id`,
                      `qty`,
                      `value`,
                      `website_id`
                      )
                    VALUES (
                      {$product->getId()},
                      0,
                      {$tierPrice['cust_group']},
                      {$tierPrice['price_qty']},
                      {$tierPrice['price']},
                      0
                    )"
                );
            }
        }
    }

    /**
     * Retrieve Usage Help Message
     *
     */
    public function usageHelp()
    {
        return <<<USAGE
Usage:  php -f staffelpreise.php -- [options]

  --run                         Update tier prices
  help                          This help

USAGE;
    }
}

$runner = new Mage_Shell_Staffelpreise();
$runner->run();


Your Answer

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

Browse other questions tagged or ask your own question.