0

I am using Magento 2.3.6

I have on the Product Page an Custom HTML Select Field: Yes / No

I want the price to Change if selected Field is set to "Yes" and keep the same price if it is set to "No".

At the moment i got this Product.php Class

namespace Vendor\Name\Model;

class Product
{
    protected $objectManager;

    public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager)
    {
        $this->objectManager = $objectManager;
    }

    public function afterGetFinalPrice($product, $proceed) : float
    {
        return $proceed+10;
    }
} 

What i want is to set $proceed + 10 only if the Custom Produt Attribute is set to "Yes".

Under Vendor_Module/etc i have di.xml file

<?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\Catalog\Model\Product">
        <plugin name="change_product" type="Vendor\Name\Model\Product" sortOrder="1"/>
    </type>
</config>

Any Ideas?

How can i get .phtml Value from Frontend into PHP File before adding to Shooping Cart?

0

you can add a new attribute with yes and no value and set for product using product attribute and set yes value + 10.

0

Found the answer for my question.

Based on Renon Stewart Answer:

How to "add to cart" a product with custom input field and save it to Database?

In the addtocart.phtml view i created this block:

<select name="cloudways[isConfigured]" id="selectBlock">
        <option value="no">No</option>
        <option value="yes">Yes</option>
</select>

In the above example the passed attribute value would be the selected option of "isConfigured".

Than in The CheckoutCartProductAddAfterObserver Class i check for the attribute.

I created for it this function and check if the "key" equals "isConfigured" attribute value.

protected function isProductConfigured($post) {
    $isProductConfigured = '';
    if(is_array($post)){
        foreach($post as $key => $value){

            if($key == 'isConfigured'){
                    $isProductConfigured = $value;
            }
        }
    }
  return $isProductConfigured;
}

I call this function in the exectue(EventObserver $observer) function after

$post = $this->_request->getParam('cloudways');

if($this->isProductConfigured($post) == "yes") {

}

After that based on this answer:

Magento 2: setCustomPrice vs. setOriginalCustomPrice

I can update my Cart Price:

$price = $item->getProduct()->getFinalPrice()+10; 
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.