Magento You have conditions if a customer buys certain products

I would like to show the terms and conditions (agreement.phtml) only if the customer purchases certain products. For example, some of our products require a prescription. The customer buys one of them, we would like the conditions to appear.

I created an attribute in the backend: prescription_required

The psuedo code is as follows:

Loop through products in the basket if prescription_required == yes set flag to yes end if end loop 

if the flag is set to yes, go to the code in the .phtml conventions file

I think the main problem I am facing is getting the product information in the .phmtl conventions and scrolling through the cart.

I tried

 <?php $_product = Mage::getModel('catalog/product')->load($this->getData('product_id')); echo $_product->getName(); ?> 

but nothing comes back.

+4
source share
2 answers

EDIT:

I made an extension that does exactly what you are trying to achieve and published it through Magento Connect. Here is the link: http://www.magentocommerce.com/magento-connect/catalog/product/view/id/14603 . You can download it or follow these steps. A link to the source code of the extension is at the bottom of the answer.

END OF EDITING

These are the steps you must follow:

  • Create a new product attribute responsible for displaying (or not) Terms of use
  • Rewrite the block responsible for displaying these conditions on the verification page
  • Rewrite the assistant responsible for checking whether all the necessary conditions and conditions of the checkboxes are clicked by users

Assumptions and Purpose:

I assume that you do not want to create something more complex than what you really need. I will explain to you how to make an extension that will lead to the appearance of ALL included terms and conditions if there is at least one product in the basket that requires conditions / conditions, or NONE of them - if there is no product in the basket that requires this.

I also assume that we want to do everything right and leave the main files alone. Of course, it would be easier to just change the basic functionality, but this approach will really cause problems in the future. That is why we are creating a new module.

Further, I assume that this functionality is necessary in all stores, if there are more.

One last thing: I tried my approach on Magento Community Edition 1.7. It does not guarantee work with other versions, but I think that it should work on 1.6 and 1.5. You can try it if you are working on one of these versions. I am not sure if this will work in older versions.

Create a new attribute

This is beyond the scope of the question, but it would be better if:

  • the attribute was the Yes / No parameter because we donโ€™t need anything else
  • the attribute was a system attribute, so we do not need to add it to each attribute group

Of course, depending on your needs, you can create the attribute in another way.

In my case, the conditional_agreements code was assigned to the product attribute. This code is used in the code snippets below.

Block rewriting

The block we need to rewrite is Mage_Checkout_Block_Agreements . It has only one function - getAgreements() .

First of all, you need to make correspondence. My package is called bartoszgorski , and my module is conditionalagreements , so my code is in config.xml :

 <config> <!-- other code --> <global> <!-- other code --> <blocks> <checkout> <rewrite> <agreements>Bartoszgorski_Conditionalagreements_Block_Agreements</agreements> </rewrite> </checkout> </blocks> <!-- other code --> </global> </config> 

In the file itself, I just used a helper (explained later) to check if we want to display any conditions. My code is as follows:

 public function getAgreements() { if(Mage::helper('conditionalagreements')->checkIfQuoteRequiresAgreements() == true) { return parent::getAgreements(); } else { return array(); } } 

So, if there is something to show, we can display it. Otherwise, function returns an empty array conventions.

Rewriting Assistant:

helper we have to rewrite Mage_Checkout_Helper_Data . If helper not been rewritten, Magento will not display any conditions / conditions, but will still require users to check their flags - and this, of course, will be impossible. So my code in config.xml is:

 <config> <!-- other code --> <global> <!-- other code --> <helpers> <conditionalagreements> <class>Bartoszgorski_Conditionalagreements_Helper</class> </conditionalagreements> <checkout> <rewrite> <data>Bartoszgorski_Conditionalagreements_Helper_Data</data> </rewrite> </checkout> </helpers> <!-- other code --> </global> </config> 

In helper there is a function getRequiredAgreementIds() , which is responsible for obtaining all the conditions and conditions that must be agreed upon. We want to do the same, but only if there is at least one product in the trolley. Therefore, I suggest creating another helper function ( checkIfQuoteRequiresAgreements() in my case), which checks such products and uses it in getRequiredAgreementIds() (and in block , as shown above). So, rewriting helper should have the following functions:

 public function getRequiredAgreementIds() { if($this->checkIfQuoteRequiresAgreements() == true) { if (is_null($this->_agreements)) { if (!Mage::getStoreConfigFlag('checkout/options/enable_agreements')) { $this->_agreements = array(); } else { $this->_agreements = Mage::getModel('checkout/agreement')->getCollection() ->addStoreFilter(Mage::app()->getStore()->getId()) ->addFieldToFilter('is_active', 1) ->getAllIds(); } } return $this->_agreements; } else { return array(); } } public function checkIfQuoteRequiresAgreements() { $showAgreements = false; $quote = Mage::getSingleton('checkout/session')->getQuote(); foreach ($quote->getAllVisibleItems() as $quoteItem) { $product = Mage::getModel('catalog/product')->load($quoteItem->getProductId()); if($product->getConditionalAgreements() == 1) { $showAgreements = true; break; } } return $showAgreements; } 

What is it. After that, you should have what you want. Just remember that if you have several items in terms and conditions, you can only display them or not. Of course, you can develop something more based on my code snippets - it is up to you.

Here is my fully working module:

https://github.com/bgorski/conditionalagreements

You can view what I did there, or simply download it and use it on your website.

Hope this helps. Any feedback would be most appreciated.

+7
source

Assuming the parameter is a text field, you can do it like this:

 // get your cart object $quote = Mage::getSingleton('checkout/session')->getQuote(); foreach ($quote->getItems() as $item) { $productId = $item->getProductId(); $product = Mage::getModel('catalog/product')->load($productId); $prescriptionRequired = $product->getAttributeText('prescription_required'); // code for subscription handling goes here } 

Let me know if this worked ...

Hello

0
source

Source: https://habr.com/ru/post/1435959/


All Articles