1

there are some products which could only shipped to specific countires. I created an attribute to set allowed countries for each product.

In frontend the cart button should be hidden and a info message should be shown.

My try is to override the function isSalable(). This works well but Magento shows the message that the product is out of stock. But there should be a message like this: "This product is not available in XY".

class Me_Mymodule_Model_Product_Type_Simple extends Mage_Catalog_Model_Product_Type_Simple
{
    public function isSalable($product = null)
    {
        $salable = parent::isSalable($product);
        if($salable && $this->getProduct($product)->hasData('allowed_countries')) {

            $customer = Mage::getSingleton('customer/session')->getCustomer();
            $shippingAddress = Mage::getModel('customer/address')->load($customer->default_shipping);
            $allowedCountries = explode(',', $this->getProduct($product)->getData('allowed_countries'));

            if(is_array($allowedCountries) && !empty($allowedCountries[0])) {
                $salable = in_array($shippingAddress->getCountryId(), $allowedCountries) ? true : false;
            }
        }
        return $salable;
    }
}

Some ideas?

Udate:

addtocart.phtml

<?php $_product = $this->getProduct(); ?>
<?php $buttonTitle = $this->__('Add to Cart'); ?>
<?php if($_product->isSaleable()): ?>
    <div class="add-to-cart">
        <?php if(!$_product->isGrouped()): ?>
            <div class="qty-block">
                <label for="qty"><?php echo $this->__('Qty:') ?></label>
                <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
            </div>
        <?php endif; ?>
        <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
        <?php echo $this->getChildHtml('', true, true) ?>
    </div>
<?php endif; ?>
  • Could you post the content of your addtocart.phtml template please ? – Raphael at Digital Pianism Jan 18 '16 at 10:42
  • Sure. I have updated my question. – user3684098 Jan 18 '16 at 10:51
2

No need to override for this, if you take a look at Mage_Catalog_Model_Product::isSalable() you have two observers to make action on is_salable attribute :

    /**
     * Check is product available for sale
     *
     * @return bool
     */
    public function isSalable()
    {
        Mage::dispatchEvent('catalog_product_is_salable_before', array(
            'product'   => $this
        ));

        $salable = $this->isAvailable();

        $object = new Varien_Object(array(
            'product'    => $this,
            'is_salable' => $salable
        ));
        Mage::dispatchEvent('catalog_product_is_salable_after', array(
            'product'   => $this,
            'salable'   => $object
        ));
        return $object->getIsSalable();
    }

You can use one of these :
- catalog_product_is_salable_before
- catalog_product_is_salable_after


Take a look at the file app/design/frontend/base/default/template/catalog/product/view/type/default.phtml
Here you can find the text for Out of stock. You can put you message here.
Edit
Here you can fin a generic example of what you have to do :
- In you Helper file

public function getDisallowedCountries(Mage_Catalog_Model_Product $product)
{
    // put you logic here to get countries from product

    return $_countries;
}

In your phtml file:

$countries = Mage::helper('your_helper')->getDisallowedCountries($product);
echo $this->__('Out of stock in %s', $countries);
  • Thank you for your answer but this is only another way to provide the functionality of my function above right? This doesn't solve the problem? – user3684098 Jan 18 '16 at 11:43
  • Sorry I misunderstood the question, I updated my answer :) – Matthéo Geoffray Jan 18 '16 at 11:50
  • Yes but if a product is out of stock the message were correct. A solution could be to check the attribute in the template file but I want to create an extension that works without customizing files after installation, you know? That's my problem. – user3684098 Jan 18 '16 at 11:58
  • OK I understand, to avoid overriding you can create an extension where the helper format your countries. In the phtml file you only have to call this helper to show them in the message. But at least you have to have to customize the phtml file. – Matthéo Geoffray Jan 18 '16 at 12:02
  • I edit to add an example of helper you can make – Matthéo Geoffray Jan 18 '16 at 12:08

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.