PrestaShop 1.6 How to add a discount to order? - plugins

I'm writing a module for PRESTASHOP 1.6. One of the task of this module is to give a customer DISCOUNT for whole order in the CART and display it. The question is which method should I use to get total value of all products in the cart and then add a single DISCOUNT for whole order?

Check this code to create a new cart rule:
$cart_rule = new CartRule();
$cart_rule->id_customer = $this->context->cart->id_customer;
$cart_rule->name = array(
Configuration::get('PS_LANG_DEFAULT') => $this->l('CartRule title')
);
$cart_rule->date_from = date('Y-m-d H:i:s', time());
$cart_rule->date_to = date('Y-m-d H:i:s', time() + 24 * 3600);
$cart_rule->quantity = 1;
$cart_rule->quantity_per_user = 1;
$cart_rule->minimum_amount_currency = $this->context->cart->id_currency;
$cart_rule->reduction_currency = $this->context->cart->id_currency;
$cart_rule->free_shipping = true;
$cart_rule->reduction_amount = 50; #discount value
$cart_rule->active = 1;
$cart_rule->add();
// Add cart rule to cart and in order
$values = array(
'tax_incl' => $cart_rule->getContextualValue(true),
'tax_excl' => $cart_rule->getContextualValue(false)
);
$this->context->cart->addCartRule($cart_rule->id, $cart_rule->name[Configuration::get('PS_LANG_DEFAULT')], $values);

Related

Get custom stock product quantity

I use this code to retrieve product quantity by given id
$stockItem = $objectManager->get('\Magento\CatalogInventory\Model\Stock\StockItemRepository');
$productId = 10858;
$productStock = $stockItem->get($productId);
$productStock->getData();
This code work and return this results
array (
'item_id' => '10858',
'product_id' => '10962',
'stock_id' => '1',
'qty' => '0.0000',
'min_qty' => '0.0000',
...
)
But I need to retrieve information by stock_id = 2 instead of default stock (id: 1)
There's a way to do this, using this code?
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productId = 1;
$StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
$product_qty = $StockState->getStockQty($productId);

Show some data on customer emails related to Woocommerce product table plugin

In Woocommerce I am using Woocommerce product table plugin and I would like to show some data on customer order email notification, but I am stuck on getting this data.
The code I am using:
add_action( 'woocommerce_email_after_order_table', 'custom_email_after_order_table', 10, 4 );
function custom_email_after_order_table( $order, $sent_to_admin, $plain_text, $email ) {
$product_names = array();
$product_test = array();
// Loop thougth order items
foreach( $order->get_items() as $item_id => $item ){
//$vendor_id = get_post_field( 'post_author', $line_item->get_product_id());
// $vendor = get_userdata( $vendor_id );
$product = $item->get_product();
$product_id = $item->get_product_id(); // Get the product ID
// Set each product name in an array
$product_names[] = $item->get_name(); // Get the product NAME
$prduct_test[]=$product->get_name();//testing
}
// Displaying
echo '<p><strong>'.__('Product names', 'woocommerce').':</strong> <br>'.implode( ', ', $product_names ).'</p>';
echo '<p><strong>'.__('Product test', 'woocommerce').':</strong> <br>'.implode( ', ', $prduct_test ).'</p>';
}
The data that I want:

How to load all orders from a specific customer in Magento 2

How can we load all orders for a specified customer id in Magento2? This is how we did it in Magento1.
$orderCollection = Mage::getModel('sales/order')->getCollection();
$lastyear = date('Y-m-d', strtotime("-1 year"));
$orders = $orderCollection->addAttributeToFilter("customer_id", 1234567890)->addAttributeToFilter('state', 'complete')->addAttributeToFilter('created_at', array('gteq' => $lastyear));
Hello mike you can get all orders of specific customer in this way
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$lastyear = date('Y-m-d', strtotime("-1 year"));
$order = $objectManager->create('Magento\Sales\Model\Order')->getCollection()->addAttributeToFilter('customer_id', $customer_id)->addAttributeToFilter('state', 'complete')->addAttributeToFilter('created_at', ['gteq' => $lastyear]);

How to set custmer id for an quote in magento?

I am adding product into cart and tried to map customer id,email to that quote
using the below code
$product_id = 123;
$qty = 1;
$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$superAttributeArray = array('151' => '3');
$params = array(
'product' => $product_id,
'qty' => $qty,
'super_attribute' => $superAttributeArray
);
$cart->addProduct($product, $params);
$cart->save();
$currenQuoteId = Mage::getSingleton('checkout/session')->getQuoteId();
$store = Mage::getSingleton('core/store')->load(Mage::app()->getStore()->getId());
$quote = Mage::getModel('sales/quote')->setStore($store)->load($currenQuoteId);
$quote->setCustomerId('1')->setCustomerEmail('test#gmail.com')->setCustomerFirstname('firstname')->setCustomerLastname('lastname');
$quote->save();
When I try to set customerid,email,fname,lname am getting error as "Mage registry key "controller" already exists".
Can anyone help me in fixing this issue?
Something like this might work
$customerObj = Mage::getModel('customer/customer')->load($customerId);
$quoteObj = Mage::getModel('sales/quote')->assignCustomer($customerObj);
$storeId = Mage::app()->getStore()->getId();
$quoteObj->setStore(Mage::getSingleton('core/store')->load($storeId);
$productObj = Mage::getModel('catalog/product')->load($productId);
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($productObj);
$quoteItem->setQuote($quoteObj);
$quoteItem->setQty('1');
$quoteItem->setStoreId($storeId);
$quoteObj->addItem($quoteItem);
$quoteObj->setStoreId($storeId);
$quoteObj->collectTotals();
$quoteObj->save();

Remove export option for particular table in zf-datagrid

I want to remove out the datagrid export option ('pdf', 'csv','excel','word' etc..) for some
specific table in the datagrid of zend framework . Its now by default coming for every
table attached to the project , How can i do this ? Thanks . posting the code of the datagrid
helper below
public function dataGrid($sql){
$this->view->headLink()->appendStylesheet($this->view->baseUrl('grid/styles/styles.css'));
$this->view->setEncoding('ISO-8859-1');
$config = new Zend_Config_Ini(APPLICATION_PATH. '/configs/grid.ini', 'production');
$grid = Bvb_Grid::factory('Table', $config, 'grid');
$grid->setExport(array());
$backendName = Zend_Cache::$standardBackends[0];
$frontendName = Zend_Cache::$standardFrontends[0];
$frontendOptions = array('automatic_serialization'=> true);
$backendOptions = array('auto_refresh_fast_cache'=> true);
$cache = Zend_Cache::factory($frontendName,
$backendName,$frontendOptions,$backendOptions);
$grid->setExport(array('pdf', 'csv','excel','word'));
$grid->addFiltersRenderDir(APPLICATION_PATH.'/views/helpers/Filters/', 'Filters');
$grid->setPaginationInterval(array(100 =>100, 200 => 200, 500 => 500, 1000 => 1000));
$grid->setUseKeyEventsOnFilters(true);
$grid->query($sql);
return $grid;
}
$grid->setGridColumns($grid_columns);
$grid->setExport(array());
$gd = $grid->deploy();
You can set the export option into null by passing a null array to the setExport function