I am trying to preselect the shipping country based on the user's country code.
I have the code already needed to get the country code which is in my model.
I have tried using the code in accepted answer to this question. It uses an after
plugin but I can't get it to work. It still defaults to United States even though I 'm telling it to use use GB.
I have tried logging out in the plugin but get nothing in the logs so I don't think the plugin is getting called.
Here is the code:
di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Block\Cart\LayoutProcessor">
<plugin name="asg_country_list"
type="Asg\GeoIp\Plugin\Checkout\Block\Cart\LayoutProcessor" sortOrder="1"/>
</type>
</config>
LayoutProcessor.php
<?php
namespace Asg\GeoIp\Plugin\Checkout\Block\Cart;
class LayoutProcessor
{
public function beforeProcess(
\Magento\Checkout\Block\Cart\LayoutProcessor $subject,
$jsLayout
) {
$path = '/var/www/magento.local/var/log/test.log';
file_put_contents($path, 'this is a test');
}
public function afterProcess(
\Magento\Checkout\Block\Cart\LayoutProcessor $subject,
$jsLayout
) {
$selectedCountry = 'GB';
$this->_logger->info($selectedCountry);
if (isset($jsLayout['components']['checkoutProvider']['dictionaries'])) {
foreach ($jsLayout['components']['checkoutProvider']['dictionaries']['country_id'] as &$country) {
if ($country['value'] == $selectedCountry) {
$country['is_default'] = 1;
} else {
if (isset($country['is_default'])) {
unset($country['is_default']);
}
}
}
}
if (isset($jsLayout['components']['block-summary']['children']['block-shipping']['children']['address-fieldsets'])) {
$jsLayout['components']['block-summary']['children']['block-shipping']['children']['address-fieldsets']['children']['country_id']['value'] = $selectedCountry;
}
return $jsLayout;
}
}
I'm using Magento 2.3.4. Could the plugin be calling the wrong module since the original question was Magento 2.2?