Pergunta

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; ?>
Foi útil?

Solução

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);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top