简体   繁体   中英

How to change magento add to cart button based on product attribute

I'm trying to change the add to cart button text based on my pre-order attribute, however it's not working quite as expected. Currently my code looks like this:

<?php if($_product->isSaleable() && $_product->getAttributeText('preorder') == 'Yes'): ?>                   
    <p><button type="button" title="<?php echo $this->__('Pre-Order') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Pre-Order') ?></span></span></button></p>
<?php elseif($_product->isSaleable() && $_product->getAttributeText('preorder') == 'No'): ?>
    <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
<?php else: ?> 
    <p class="availability out-of-stock"><span><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $this->__('Product Info') ?></a></span></p>
<?php endif; ?>

Any ideas would be greatly appreciated!

I am assuming Preorder is a Yes/No Attribute?

if so you can use this:

isSaleable() && $_product->getPreorder()):?>

You should still be able to use your method though if you ensure the following:

You also need to go into the edit attributes section and make sure you have enabled the setting to add the attribute to the collection/object when needed.

If you're on the product view page enable:

Used in Product Listing: YES

If you're on the product listing page:

 Used in Product Listing: YES 

Thanks for the ideas! I realized the issue was that I was using single quotes and not double quotes! Doh!

&& $_product->getAttributeText('preorder') == "Yes"): ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM