#StackBounty: #magento2 #cart #magento2.2 #addtocart Why does the second product dynamically added to the cart loose it's options i…

Bounty: 50

I am dynamically adding products to the cart in Magento2 with some custom options. Every product has the same base product id with different options. RepresentProduct has been properly overridden so that all products added to the cart are separate. However with this code, the second product added will lose it’s custom options:

$magento_product = $this->productRepository->get('simple-product-1');
$params = array(
    'product' => $magento_product->getId(),
    'qty'     => intval(5),
    'options' => array(
        'cr_price' => 12.0,
        'Product' => "Test P",
        'cr_XML' => '<root></root>'
    ),
);
$this->cart->addProduct($magento_product, $params);
$params = array(
    'product' => $magento_product->getId(),
    'qty'     => intval(10),
    'options' => array(
        'cr_price' => 14.0,
        'Product' => "Test P2",
        'cr_XML' => '<root></root>'
    ),
);
$this->cart->addProduct($magento_product, $params);
$this->cart->save();

Only the first product has an entry in the quote_item_option table.

Any thoughts on why or how to fix would be appreciated.

Magento 2.2.


Get this bounty!!!

#StackBounty: #magento2 #cart #magento2.2 #addtocart Why does the second product dynamically added to the cart loose it's options i…

Bounty: 50

I am dynamically adding products to the cart in Magento2 with some custom options. Every product has the same base product id with different options. RepresentProduct has been properly overridden so that all products added to the cart are separate. However with this code, the second product added will lose it’s custom options:

$magento_product = $this->productRepository->get('simple-product-1');
$params = array(
    'product' => $magento_product->getId(),
    'qty'     => intval(5),
    'options' => array(
        'cr_price' => 12.0,
        'Product' => "Test P",
        'cr_XML' => '<root></root>'
    ),
);
$this->cart->addProduct($magento_product, $params);
$params = array(
    'product' => $magento_product->getId(),
    'qty'     => intval(10),
    'options' => array(
        'cr_price' => 14.0,
        'Product' => "Test P2",
        'cr_XML' => '<root></root>'
    ),
);
$this->cart->addProduct($magento_product, $params);
$this->cart->save();

Only the first product has an entry in the quote_item_option table.

Any thoughts on why or how to fix would be appreciated.

Magento 2.2.


Get this bounty!!!

#StackBounty: #magento2 #mini-cart #knockoutjs Magento2 Knockout js – issue on cart

Bounty: 50

I tried to reposition my cart block to a other header location.
Since then I am getting this javascript error which is annoying since I only repositioned a block.

Uncaught TypeError: Unable to process binding "foreach: function (){return { data:cart().items,as:'item'} }"
Message: Unable to process binding "foreach: function (){return $parent.getRegion($parent.getItemRenderer(item.product_type)) }"
Message: Cannot read property 'simple' of undefined

This is the block i repositioned.

  <block class="MagentoCheckoutBlockCartSidebar" name="minicart" as="minicart" after="store_language" template="cart/minicart.phtml">
                <arguments>
                    <argument name="jsLayout" xsi:type="array">
                        <item name="types" xsi:type="array"/>
                        <item name="components" xsi:type="array">
                            <item name="minicart_content" xsi:type="array">
                                <item name="component" xsi:type="string">Magento_Checkout/js/view/minicart</item>
                                <item name="config" xsi:type="array">
                                    <item name="template" xsi:type="string">Magento_Checkout/minicart/content</item>
                                </item>
                                <item name="children" xsi:type="array">
                                    <item name="subtotal.container" xsi:type="array">
                                        <item name="component" xsi:type="string">uiComponent</item>
                                        <item name="config" xsi:type="array">
                                            <item name="displayArea" xsi:type="string">subtotalContainer</item>
                                        </item>
                                        <item name="children" xsi:type="array">
                                            <item name="subtotal" xsi:type="array">
                                                <item name="component" xsi:type="string">uiComponent</item>
                                                <item name="config" xsi:type="array">
                                                    <item name="template" xsi:type="string">Magento_Checkout/minicart/subtotal</item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                    <item name="extra_info" xsi:type="array">
                                        <item name="component" xsi:type="string">uiComponent</item>
                                        <item name="config" xsi:type="array">
                                            <item name="displayArea" xsi:type="string">extraInfo</item>
                                        </item>
                                    </item>
                                    <item name="promotion" xsi:type="array">
                                        <item name="component" xsi:type="string">uiComponent</item>
                                        <item name="config" xsi:type="array">
                                            <item name="displayArea" xsi:type="string">promotion</item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </argument>
                </arguments>
            </block>


Get this bounty!!!

#StackBounty: #magento2 #magento2.2 #cache #varnish Magento 2.2 : How to programatically clear the varnish cache by cache tag

Bounty: 50

I need to switch the currency based on user IP location so I’m using this auto currency extension, it works only when I disabled the FPC otherwise it just cache the previous content and never gets changed.

I’m looking to programmatically clear the varnish cache by the cache tag of the store currency. I tried the below logic in the observer (controller_action_predispatch) but it does not work.

protected $config;
protected $purgeCache;

public function __construct(
    MagentoPageCacheModelConfig $config,
    MagentoCacheInvalidateModelPurgeCache $purgeCache
) {
    $this->config = $config;
    $this->purgeCache = $purgeCache;
}

public function execute(MagentoFrameworkEventObserver $observer)
{
    $tags = [];
    $pattern = "((^|,)%s(,|$))";
    $tags[] = sprintf($pattern, 'store');
    $this->purgeCache->sendPurgeRequest(implode('|', array_unique($tags)));
}

Thanks in advance.


Get this bounty!!!

#StackBounty: #magento2 #ui-form Magento 2 : Show Customer Attribute in ui_component Form

Bounty: 50

I’ve created ui_component form.

Where I need to show customer details, Same as Customer Edit.

But, I can able to show their data from customer_entity table.

DataProvider.php

public function getData()
{
    if (isset($this->loadedData)) {
        return $this->loadedData;
    }

    // {Vendor}{Module}ModelGridFactory 
    // Returns Customer Resource Model
    $items = $this->gridFactory->create()->getCollection();

   $items->getSelect()->join('customer_entity_text as second', 'main_table.entity_id = second.entity_id');
    //print_r($items->getData()); exit;
    foreach($items as $contact){
        $this->loadedData[$contact->getEntityId()]['contact'] = $contact->getData();
    }

    return $this->loadedData;
}

I’ve joined customer_entity_text table with my Factory in order to display status (Customer Attribute).

Now My Second attribute is file type. It’s in customer_entity_varchar, Firstly I thought that to add another join but I think it’s not the good way.

So, Is there any solution for this? I need to display both Customer Attribute in my form.

ui_component

<field name="value">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string">Status</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">contact</item>
            </item>
        </argument>
    </field>

1). Above component is working good for Status but not for Profile Image that is Image type.

<field name="value">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string">Profile Image</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="formElement" xsi:type="string">fileUploader</item>
                <item name="uploaderConfig" xsi:type="array">
                    <item name="url" xsi:type="url" path="path_controller"/>
                </item>
            </item>
        </argument>
    </field>

Even if I remove one field with same name form element it doesn’t seems to be work.

Take a look at field name that is value for Status.

If I use the same thing for image field than image component is disappeared.

Note: I’ve no idea why Magento doesn’t allow to use the name as value.

Cause I’ve joined in the collection so i am getting value as the array key.

**Question: How can I get customer attributes in this form without join in the collection ?

Also if you have other solution than most also most welcome.**


Get this bounty!!!

#StackBounty: #magento2 #grid #layered-navigation #filter #product-collection Filter on custom product collection

Bounty: 100

I’m creating a custom brand page in which are listed product with a specific brand.

I’ve created a static page with a 2 column layout, with a left sidebar in which I’m putting filters and the main column contains the products grid.

This is the code for the content widget

{{widget type="MagentoCatalogWidgetBlockProductProductsList" 
show_pager="0" 
products_count="100"
tagline="bla bla bla."  
logo="pub/media/brands/brand.png" 
template="MyVendor_MyTheme::product/widget/content/grid.phtml" 
conditions_encoded="^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,`aggregator`:`all`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`brand`,`operator`:`==`,`value`:`3847`^]^]"}}

for which I’ve overridden the Magento template grid.phtml

In the left sidebar I’ve put layered navigation

<referenceContainer name="sidebar.main">
             <block class="MagentoLayeredNavigationBlockNavigationCategory" name="catalog.leftnav" before="-" template="layer/view.phtml">
                   <block class="MagentoLayeredNavigationBlockNavigationState" name="catalog.navigation.state" as="state" />
                   <block class="MagentoLayeredNavigationBlockNavigationFilterRenderer" name="catalog.navigation.renderer" as="renderer" template="layer/filter.phtml"/>
            </block>

        </referenceContainer>

for which I’m overriding the view.phtml template.
I’m able to start with a filtered collection in the view.phtml like this

$block->getLayer()->getProductCollection()->addFieldToFilter('brand','3847')

and filters returns the correct item count inside the block.

I’ve added a:

$block->getProductCollection()->addFieldToFilter('brand','3847')

in the grid.phtml just for debugging and the collection is correct in the grid.phtml template

The problem is that the filter is not applied to the grid.phtml block.

How do I “connect” the two blocks?


Get this bounty!!!