-1

次の関数は ajax 呼び出しを行いますが、いくつかの問題があります。まず、ajax 呼び出しを行う必要がありますか? PHP の値は $product_id だけです。アプリケーションは事前に ajax 呼び出しを行い、色が選択された後に利用可能な製品をページに取り込みます。次に、ボタン要素に間違った番号が割り当てられ続けます! 最後に、このようなクリック機能を割り当てても機能しますか? 任意の提案をいただければ幸いです。

jquery:

function addToCartBlock(value)
    {
    console.log ('the function value is ' + value); 
    var product_id = <?= $product_id ?>;
        $j.ajax({
            type: "POST", 
            url: "/ajax_calls/addToCartBlock.php",
            data: { 'value': value, 'product_id': product_id} 
            }).done(function(data) {
                $j('.vendorListItem'+value).append('<span class="add-to-cart" id="add-to-cart'+ value+'"></span>');
                $j('.vendorListItem'+value).css({'display':'inline'});
                $j('#add-to-cart' + value).append('<label for="qty">Qty:</label>'); 
                $j('#add-to-cart' + value).append('<input type="text" name="qty" id="qty" maxlength="12" value="1" title="Qty" class="input-text qty" />');
                $j('#add-to-cart' + value).append('<button type="button" title="" class="button btn-cart" id="' + value + '"><span>Add To Cart</span></button>');

                $j($j('button').attr('id',value)).on('click',function()
                {
                    console.log('The onclick value is ' + $j('button').attr('id'));//this logs a random number btwn 1-3, last add to cart button logs once the others multiple times
                    //take value of list item
                    $j('#attribute136 option[value=' + $j('button').attr('id') + ']').attr('selected', 'selected').ready(function () {;//make the applicable selection
                    $j('#attribute136').val($j('button').attr('id'));

                    console.log($j('#attribute136').val()); //this keeps logging '3'
                    //CAITLIN not all buttons have an ID of the correct value though the prior span class seems to... console.log through the program to debug

                    //initiate add to cart function
                    productAddToCartForm.submit(this); 
                    });
                });
            }); 
    }

PHP スクリプト addtocart.php:

<?php

require_once('/var/www/Staging/public_html/app/Mage.php');
umask(0);
Mage::app(); 

//ensure that the value is legitimate
if($_POST && is_numeric($_POST['value'])){
    $value = $_POST['value'];
}

//pass this in your ajax call for the add button
if($_POST && is_numeric($_POST['product_id'])){
    $product_id = $_POST['product_id'];
}

$helper = Mage::helper('core'); //for translation
$block = new Mage_Catalog_Block_Product_View(); // not best practice, but neither are standalones
$product =  Mage::getModel('catalog/product')->load($product_id); // no need to use the _ here, it's not protected/private; additonally Mage::registry won't work because you're technically not on a product detail page

$buttonTitle = ''; //you are using this, but it isn't set

$resultsArray= [];
$resultsArray['Qty'] = $helper->__('Qty:');
$resultsArray['Qtyno'] = $helper->__('Qty');
$resultsArray['DefaultQty'] = $block->getProductDefaultQty($product);
$resultsArray['windowLocation'] = Mage::helper('checkout/cart')->getAddUrl($product);
$resultsArray['value']=$value;
echo json_encode($resultsArray);

?>

編集!!!!AJAX呼び出しの代わりに以下の機能を試しています。このコードで私が抱えている唯一の問題は、ID が 3 のボタンが選択された場合にのみカートに追加されることです。該当する id 値の商品をカートに追加するには、すべてのボタンが必要です。

function addToCartBlock(value)
    {
    console.log ('the addToCartBlock function value is ' + value); 
    //var product_id = <?= $product_id ?>;

                $j('.vendorListItem'+value).append('<span class="add-to-cart" id="add-to-cart'+ value+'"></span>');
                $j('.vendorListItem'+value).css({'display':'inline'});
                $j('#add-to-cart' + value).append('<label for="qty">Qty:</label>'); 
                $j('#add-to-cart' + value).append('<input type="text" name="qty" id="qty" maxlength="12" value="1" title="Qty" class="input-text qty" />');
                $j('#add-to-cart' + value).append('<button type="button" onclick="selectAndAddToCart('+value+')" title="" class="button btn-cart" id="' + value + '"><span>Add To Cart</span></button>');
    }  

function selectAndAddToCart(value)
{
    console.log('The selectAndAddToCart onclick value is ' + value);
    $j('#attribute136 option[value=' + value + ']').attr('selected', 'selected').ready(function () {;//make the applicable selection
        $j('#attribute136').val(value);

        console.log('Selection made.');     
        console.log($j('#attribute136').val()); //this keeps logging '3'

        //initiate add to cart function
        productAddToCartForm.submit(this); 
    });
}
4

2 に答える 2

1

次のコードは、動的に追加されたボタンのクリック イベントの問題を解決するのに役立ちます。

$j(document).on("click","#" + value, function () {
    alert('test');
});
于 2013-05-06T19:19:34.057 に答える
0

値を返す必要がなかったので、ajax 関数を使用する必要はありませんでした。javascript で一定の​​ php 変数を使用するだけで済みました。ご提案いただきありがとうございます。selectAndAddToCart 関数にまだ問題がありますが、質問を分けるために再度投稿します。

于 2013-05-06T20:14:41.740 に答える