【问题标题】:"Please specify the product's option(s)" in Magento 1.9.2Magento 1.9.2 中的“请指定产品的选项”
【发布时间】:2017-04-16 10:14:46
【问题描述】:

我有一个 Magento 1.9 服务器并且产品已设置,简单产品的添加到购物车工作顺利且正常,但是当我尝试添加可配置产品时,此消息返回。

请指定产品的选项。

现在我正在跟踪代码并对其进行处理,直到我到达这里。

app/code/core/Mage/Checkout/controllers/CartController

在此文件中存在此添加产品的代码:

/**
 * Add product to shopping cart action
 *
 * @return Mage_Core_Controller_Varien_Action
 * @throws Exception
 */
public function addAction()
{
    if (!$this->_validateFormKey()) {
        $this->_goBack();
        return;
    }
    $cart   = $this->_getCart();
    $params = $this->getRequest()->getParams();

    try {
        if (isset($params['qty'])) {
            $filter = new Zend_Filter_LocalizedToNormalized(
                array('locale' => Mage::app()->getLocale()->getLocaleCode())
            );
            $params['qty'] = $filter->filter($params['qty']);


        }

        $product = $this->_initProduct();
        $related = $this->getRequest()->getParam('related_product');


        /**
         * Check product availability
         */
        if (!$product) {
            $this->_goBack();
            return;
        }
        echo 'here';
        ?>
            <pre>
                <?php print_r($params); ?>
            </pre>
        <?php
            exit;
        ?>
        <?php

        $cart->addProduct($product, $params);
        if (!empty($related)) {
            $cart->addProductsByIds(explode(',', $related));
        }

        $cart->save();

        $this->_getSession()->setCartWasUpdated(true);

        /**
         * @todo remove wishlist observer processAddToCart
         */
        Mage::dispatchEvent('checkout_cart_add_product_complete',
            array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
        );

        if (!$this->_getSession()->getNoCartRedirect(true)) {
            if (!$cart->getQuote()->getHasError()) {
                $message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->escapeHtml($product->getName()));
                $this->_getSession()->addSuccess($message);
            }
            $this->_goBack();
        }
    } catch (Mage_Core_Exception $e) {
        if ($this->_getSession()->getUseNotice(true)) {
            $this->_getSession()->addNotice(Mage::helper('core')->escapeHtml($e->getMessage()));
        } else {
            $messages = array_unique(explode("\n", $e->getMessage()));
            foreach ($messages as $message) {
                $this->_getSession()->addError(Mage::helper('core')->escapeHtml($message));
            }
        }

        $url = $this->_getSession()->getRedirectUrl(true);
        if ($url) {
            $this->getResponse()->setRedirect($url);
        } else {
            $this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
        }
    } catch (Exception $e) {
        $this->_getSession()->addException($e, $this->__('Cannot add the item to shopping cart.'));
        Mage::logException($e);
        $this->_goBack();
    }
}

在这段代码中,我使用这个简单的代码来实现我在哪里以及我拥有什么数据。

echo 'here';
?>
    <pre>
        <?php print_r($params); ?>
    </pre>
<?php
    exit;
?>
<?php

这个简单的代码返回我的可配置产品的参数。

Array
(
    [uenc] => aHR0cDovL3N0YWdpbmcudmF0dHJlbmEuY29tL2luZGV4LnBocC9nZW5lcmFsLWplYW5zMTMuaHRtbA,,
    [product] => 65
    [form_key] => qIGyp6YDp7kQjibA
    [related_product] => 
    [super_attribute] => Array
        (
            [168] => M
        )

    [qty] => 1
)

这意味着选择了所有属性,但是当我到达这个简单的代码之后

$cart->addProduct($product, $params);

它假设去这个文件

app/code/core/Mage/Checkout/Model/Cart.php

关于这个功能

/**
 * Add product to shopping cart (quote)
 *
 * @param   int|Mage_Catalog_Model_Product $productInfo
 * @param   mixed $requestInfo
 * @return  Mage_Checkout_Model_Cart
 */
public function addProduct($productInfo, $requestInfo=null)
{
    $product = $this->_getProduct($productInfo);
    $request = $this->_getProductRequest($requestInfo);

    $productId = $product->getId();

    if ($product->getStockItem()) {
        $minimumQty = $product->getStockItem()->getMinSaleQty();
        //If product was not found in cart and there is set minimal qty for it
        if ($minimumQty && $minimumQty > 0 && $request->getQty() < $minimumQty
            && !$this->getQuote()->hasProductId($productId)
        ){
            $request->setQty($minimumQty);
        }
    }

    if ($productId) {
        try {
            $result = $this->getQuote()->addProduct($product, $request);
        } catch (Mage_Core_Exception $e) {
            $this->getCheckoutSession()->setUseNotice(false);
            $result = $e->getMessage();
        }
        /**
         * String we can get if prepare process has error
         */
        if (is_string($result)) {
            $redirectUrl = ($product->hasOptionsValidationFail())
                ? $product->getUrlModel()->getUrl(
                    $product,
                    array('_query' => array('startcustomization' => 1))
                )
                : $product->getProductUrl();
            $this->getCheckoutSession()->setRedirectUrl($redirectUrl);
            if ($this->getCheckoutSession()->getUseNotice() === null) {
                $this->getCheckoutSession()->setUseNotice(true);
            }
            Mage::throwException($result);
        }
    } else {
        Mage::throwException(Mage::helper('checkout')->__('The product does not exist.'));
    }

    Mage::dispatchEvent('checkout_cart_product_add_after', array('quote_item' => $result, 'product' => $product));
    $this->getCheckoutSession()->setLastAddedProductId($productId);
    return $this;
}

但是当我尝试echo 这个函数中的任何东西时,它不会出现并重定向到可配置的产品页面并显示以下消息:

请指定产品的选项。

更新 1:

$_helper = $this->helper('catalog/product');
$_product = $this->getProduct();
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
<?php if (count($_attributes) > 0):?>
    <dl>
    <?php foreach($_attributes as $_attribute):?>
    <div class="row">
        <h2 class="gws_p_title" style="border-top:  none;"><?php echo $this->__($_attribute->getLabel()) ?><em>*</em></h2>
        <dd class="last">
            <div class="input-box">
                <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select js-example-basic-single">
                    <option><?php echo $this->__('Choose an Option...') ?></option>
                  </select>
              </div>
        </dd>
        </div>
    <?php endforeach; ?>
    </dl>

【问题讨论】:

    标签: php magento magento-1.9 configurable-product


    【解决方案1】:

    请指定产品的选项。

    当您的产品有一些需要的自定义选项,但在添加到购物车时未选择,或者在添加到购物车时未选择可配置的产品可配置选项(如颜色或尺寸)时,会出现此错误。确保您已正确创建可配置选项。

    您是否通过导入创建产品,因为 magento 导入配置文件不导入可配置选项。您需要在您的管理员-> 目录-> 产品中检查这个东西打开不工作的产品。您需要检查产品是否有一些可配置的选项。

    如果产品有选项,您需要检查分配给该选项的产品是否应该启用并且该商店有库存。

    如果您没有对可配置产品进行任何自定义,那么这么多操作就足以为您的问题找到解决方案。

    【讨论】:

    • 非常感谢您的评论,这里发生的情况是我的可配置有尺寸可配置选项,当我选择它并选择数量并按添加到购物车时,会发生此错误,我检查了配置选项,我检查了产品,一切对我来说似乎都是正确的。
    • 我在这里解决了这个问题,通过我所有的研究并检查 phtml 文件中的所有代码。请检查更新和我给出的答案。
    【解决方案2】:

    这对我来说是个问题,因为我是 magento 的新手,所以我花了很多时间。

    在文件前面添加这一行

    app/design/frontend/yourtheme/default/template/catalog/product/view/type/options/cofigurable.phtml

    $attValConfig = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);
    

    所以在搜索并检查了我拥有的每个代码之后,我在这些行中发现了错误。

    <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select js-example-basic-single">
        <option><?php echo $this->__('Choose an Option...') ?></option>
    </select>
    

    应该是这样的

    <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select js-example-basic-single">
    <option><?php echo $this->__('Choose an Option...') ?></option>
    <?php
    foreach ($attValConfig as $values) {
        foreach ($values['values'] as $value) {
           ?>
           <option value="<?php echo $value['value_index']; ?>"><?php echo $this->__($value['label']); ?></option>
           <?php
    
        }
    }
    ?>
    </select>
    

    现在一切正常

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多