Magento2: save new order by different order status - magento

I've created a new payment gateway but I would like to save a different order status
registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_CustomPayment',
__DIR__
);
etc/config.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Store/etc/config.xsd">
<default>
<payment>
<custompayment>
<payment_action>authorize</payment_action>
<is_gateway>1</is_gateway>
<model>Vendor\CustomPayment\Model\PaymentMethod</model>
<active>1</active>
<title>Pay with Credit Card</title>
<order_status>Pending</order_status>
</custompayment>
</payment>
</default>
However, when I try to make order I doesn't see Pending status but Processing

I've solved by my self adding this variable in the Payment Model:
protected $_isInitializeNeeded = true;
this code overrides Order::STATE_PROCESSING defined in: Magento\Sales\Model\Order\Payment

Related

Magento 2 append custom attribute to product name

I would like to add 2 custom attributes after the product name EVERYWHERE in the magento 2 shop.
Like "Productname Attribute1 Attribute2"
Is this possible and how? Do i need to modify every page or is there a way to act directly on the product name rendering for the whole system?
thanks
Fot that you have to create extension. Please check my code
Create folder in app/code/Magenest
Create sub folder in app/code/Magenest/Sample
Now create registration.php with following code
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Magenest_Sample',
__DIR__
);
Create etc folder in app/code/Magenest/Sample/
Create module.xml in etc folder with following code
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magenest_Sample" setup_version="2.0.0">
</module></config>
Create folder in frontend in app/code/Magenest/Sample/etc/
Create a file di.xml file in app/code/Magenest/Sample/etc/frontend with following code
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Model\Product" type="Magenest\Sample\Model\Product"/>
</config>
Now create a Model folder in app/Magenest/Sample/
Create Product.php in Model folder with following code
<?php
namespace Magenest\Sample\Model;
class Product extends \Magento\Catalog\Model\Product
{
public function getName()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($this->_getData('entity_id'));
$myattribute = $product->getResource()->getAttribute('putyourcustomattribute')->getFrontend()->getValue($product);
$changeNamebyPreference = $this->_getData('name') . ' '. $myattribute;
return $changeNamebyPreference;
}
}

Migrating Shipping Extension for Magento 1.9 to Magento 2.2

I am trying to migrate a custom magento 1.9 extension to magento 2.2. I have done a lot of searching and can't find information on the use case I'm trying to migrate. I originally followed this tutorial for the 1.9 extension. I understand there is a tool to help port extensions, but I am trying to do this manually as I couldn't get that tool to function for me.
The custom shipping extension would run each time the shopping cart was updated to calculate a custom shipping rate. The goal is to recreate this extension in magento 2.2 such that each time the shopping cart is opened or updated it will run and calculate a shipping cost which will then propagate through the checkout process.
Below is an overview of the magento 1.9 extension. Any advice on how to translate this to magento 2.2?
/app/etc/modules/Extensions_Shipper.xml
<?xml version="1.0"?>
<config>
<modules>
<Extensions_Shipper>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Shipping />
</depends>
</Extensions_Shipper>
</modules>
/app/code/local/Extensions/Shipper/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Extensions_Shipper>
<module>0.0.1</module>
</Extensions_Shipper>
</modules>
<global>
<models>
<extensions_shipper>
<class>Extensions_Shipper_Model</class>
</extensions_shipper>
</models>
</global>
<default>
<carriers>
<extensions_shipper>
<active>1</active>
<model>extensions_shipper/carrier</model>
<title>Shipping Options</title>
<sort_order>10</sort_order>
<sallowspecific>0</sallowspecific>
</extensions_shipper>
</carriers>
</default>
/app/code/local/Extensions/Shipper/Model/Carrier.php
<?php
class Extensions_Shipper_Model_Carrier extends Mage_Shipping_Model_Carrier_Abstract implements Mage_Shipping_Model_Carrier_Interface
{
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
//Are there magento 2.2 equivalence for the following?
$addressInfo = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData();
$result = Mage::getModel('shipping/rate_result');
$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
$currentItem = Mage::getModel('catalog/product')->load($items[$itemsArray[$i]]->getProduct()->getId());
Mage::getSingleton('core/session')->addNotice('some text');
//$result = some calculations for shipping rate
return $result;
}
public function getAllowedMethods()
{
return array();
}
}
You can create an event observer on the checkout_cart_add_product_complete event to execute the logic to update the shipping costs.
namespace MyCompany\MyModule\Observer;
use Magento\Framework\Event\ObserverInterface;
class MyObserver implements ObserverInterface
{
public function __construct()
{
//Observer initialization code...
//You can use dependency injection to get any class this observer may need.
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
//Observer execution code...
}
}
Subscribe to the event in events.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="my_module_event_before">
<observer name="myObserverName" instance="MyCompany\MyModule\Observer\MyObserver" />
</event>
<event name="my_module_event_after">
<observer name="myObserverName" instance="MyCompany\MyModule\Observer\AnotherObserver" />
</event>
</config>
See Magento docs on events and observers: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/events-and-observers.html
For a list of all Magento 2.2 events, see: https://cyrillschumacher.com/magento-2.2-list-of-all-dispatched-events/

How to replace my custom template for product detail page in Magento2?

I need to replace my custom template for product detail page in magento2.
I have followed this link and updated my code but it is not working.
In my module, I have added catalog_product_view.xml and below code.
Mynamespace\Catalog\view\frontend\layout\catalog_product_view.xml
<referenceContainer name="content">
<block class="Mynamepace\Catalog\Block\Product\View\Article" name="product.view.art" template="Mynamespace_Catalog::product\view\article.phtml">
</block>
</referenceContainer>
This is not working. Can anyone suggest what I am missing here?
My Block Code:
Mynamepace\Catalog\Block\Product\View\Article.php
<?php
namespace Mynamespace\Catalog\Block\Product\View;
use Magento\Catalog\Block\Product\AbstractProduct;
class Article extends AbstractProduct
{
public function showPages()
{
return 'Article';
}
}
My phtml - Mynamespace\Catalog\view\frontend\templates\product\view\article.phtml
I am in Article
<?php
My Module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Mynamespace_Catalog" setup_version="1.0.0" schema_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
Please tell me if I have missed something Or at least tell me how to debug this code.
I see a wrong syntax on your layout file Mynamespace\Catalog\view\frontend\layout\catalog_product_view.xml
template="Mynamespace_Catalog::product\view\article.phtml"
Change it to:
template="Mynamespace_Catalog::product/view/article.phtml"

Magento custom module - unable to call block method from template

I'm working on a custom module to display CMS content. I have a custom front controller which is working as expected. I'm able to call various front actions from the controller. I am using an existing template, which is also displaying as it should. I'm also loading a layout update xml file, from which I was able to remove the product menu, which I don't need, and add a reference block for my custom block's template file.
I know the correct template override file is loading, as I'm testing with the following:
<?php echo __FILE__ . " loaded <br>"; ?>
Which is echoing the correct filename.
However, when I call my custom block method from that same template file, I get nothing.
My module namespace/module is Cmpreshn/Projects. Following is what I have so far:
Config file in
app/code/local/Cmpreshn/Projects/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Cmpreshn_Projects>
<version>0.1.0</version>
</Cmpreshn_Projects>
</modules>
<frontend>
<routers>
<projects>
<use>standard</use>
<args>
<module>Cmpreshn_Projects</module>
<frontName>education</frontName>
</args>
</projects>
</routers>
<layout>
<updates>
<projects>
<file>projects.xml</file>
</projects>
</updates>
</layout>
</frontend>
<global>
<blocks>
<projects>
<class>Projects_Block_List</class>
</projects>
</blocks>
</global>
</config>
Front controller in
app/code/local/Cmpreshn/Projects/controllers/ProjectsController.php
<?php
class Cmpreshn_Projects_ProjectsController extends Mage_Core_Controller_Front_Action {
public function indexAction(){
$this->listAction();
}
public function listAction(){
echo "list action called<br>";
/* get request and save params to object */
$this->request = Mage::app()->getRequest();
/* layout overrides for this module in app/design/frontend/default/pmc1/layout/projects.xml */
$this->loadLayout();
/* use the education template */
$this->getLayout()->getBlock("root")->setTemplate("page/pmc_education.phtml");
/* render the layout */
$this->renderLayout();
}
}
XML updates in
app/design/frontend/default/pmc1/layout/projects.xml
<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
<projects_projects_list>
<remove name="top.menu"/>
<reference name="content">
<block type="page/html" name="page" template="cmpreshn/projects/list.phtml" />
</reference>
</projects_projects_list>
</layout>
Template overrides and call to custom block in
app/design/frontend/default/pmc1/template/cmpreshn/project/list.phtml
<?php echo __FILE__ . " loaded <br>"; ?>
<?php echo $this->getProjectsList(); ?>
Last but not least, my custom block class in
app/code/local/Cmpreshn/Proejcts/Block/List.php
<?php
class Cmpreshn_Projects_Block_List extends Mage_Core_Block_Template {
public function _construct() {
parent::__construct();
echo "projects list block constructor called<br>";
} // end constructor
public function getProjectsList() {
echo "getProjectsList called <br>";
return("getProjectsList called");
}
} // end class
As I mentioned previously, I am getting the output from the first line of my list.phtml template file, but no output from my custom block method, and no indication that my block is loading (no output from block _construct() method either )
Any help is appreciated. I'm ready to pull my eyes out over this...
I just observed your code and found the following errors:
Registeration of block in registering module file (config.xml) seems wrong.
<global>
<blocks>
<projects>
<class>Cmpreshn_Projects_Block</class> <!-- Not Projects_Block_List -->
</projects>
</blocks>
</global>
The type attribute is wrong in Block element of layout file (projects.xml). You should not call page/html instead you should call projects/list.
There might be more typos. but I could found the above two only. I hope this solves your problem.
change the block type to projects/list in your projects.XML file like this
<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
<projects_projects_list>
<remove name="top.menu"/>
<reference name="content">
<block type="projects/list" name="page" template="cmpreshn/projects/list.phtml/>
</reference>
</projects_projects_list>
</layout>
you may get the output now.

magento link to cms page in product attribute not working

I'm trying to add an 'a href' / link in an Attribute Field for a product.
However, the methods that I'm using are not working - although they work in CMS page content. When I view the product, the attribute with the link is displayed, but the actual URL does not seem to be generated correctly (404 error)
I've tried the following:
1. Test link 1
2. Test link 2
3. Test link 3
What am I doing wrong?
Your help is appreciated in advance
Thank you!
Magento EAV attributes values will not be parsed by PHP on their own. For display to the user, they are rendered through a frontend model. See eav_attribute table for examples.
Based on the "we do not want to display the entire url, just a text link" comment, you need an attribute with a custom frontend model. I'm guessing that it was added via the Admin Panel, which won't allow to add custom frontend models. Whereas adding the frontend model requires a script, I'd recommend adding the attribute via script in the first place.
To install this attribute properly , Magento needs to execute a setup script, which is a Magento term for (usually) PHP code which is executed exactly once with the ability to manipulate the database. Running these presupposes a module exists:
app/etc/modules/Your_Module.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Your_Module>
<active>true</active>
<codePool>local</codePool>
</Your_Module>
</modules>
</config>
app/code/local/Your/Module/etc/config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Your_Module>
<version>1.0.0.0</version>
</Your_Module>
</modules>
<global>
<models>
<your_module>
<class>Your_Module_Model</class>
</your_module>
</models>
<resources>
<your_module_setup>
<setup>
<module>Your_Module</module>
</setup>
</your_module_setup>
</resources>
</global>
</config>
app/code/local/Your/Module/sql/your_module_setup/install-1.0.0.0.php:
<?php
$installer = Mage::getResourceModel('catalog/setup','catalog_setup');
/* #var $installer Mage_Catalog_Model_Resource_Setup */
$installer->startSteup();
$installer->addAttribute(
'catalog_product',
'unique_attr_code',
array(
'label' => 'Link to Product',
'required' => 'false', //or true if appropriate
'group' => 'General', //Adds to all sets
'frontend' => 'your_module/frontend_url'
)
);
$installer->endSetup();
app/code/local/Your/Module/Model/Frontend/Url.php:
class Your_Module_Model_Frontend_Url
extends Mage_Eav_Model_Entity_Attribute_Frontend_Abstract
{
public function getUrl($object)
{
$url = false;
if ($path = $object->getData($this->getAttribute()->getAttributeCode())) {
$url = Mage::getUrl('path');
}
return $url;
}
}

Resources