문제

I have OpenCart 1.5.6. Inside the product page, a javascript count.js is attached to the text area

counter = function() {
    var value = $('#bob').val();
    if (value.length == 0) {
        $('#wordCount').html(o);
        return;
    }
var wordCount = value.split(/\s+/)
                     .filter(function(v){ return v.length>2 })
                     .length;
    $('#wordCount').html(wordCount);
};
$(document).ready(function() {
    $('#count').click(counter);
    $('#bob').change(counter);
    $('#bob').keydown(counter);
    $('#bob').keypress(counter);
    $('#bob').keyup(counter);
    $('#bob').blur(counter);
    $('#bob').focus(counter);
});

I want the price to dynamically change depending on the output. So for example I want the price to become

price = price * wordcount;

So that the price changes depending on the amount of words that are entered into the textbox

Can someone please help me to do that. My php knowledge is minimal, and I'm not sure where, in what file, can i make that happen, and how.

도움이 되었습니까?

해결책

I think you'll need to learn a bit of PHP for this.

First of all your text area (bob, I presume) should be set up as an option for the product with a type of "Textarea". Then set the product's price to be whatever the price per word is.

In catalog/controller/product/product.php, you need to add the following code:

$this->data['word_price'] = $this->tax->calculate($product_info['price'], $product_info['tax_class_id'], $this->config->get('config_tax'));

In your product.tpl make sure where the price is displayed is encapsulated by <span id="product_price">xxx</span>

Then in your javascript function counter() at the beginning

var word_price = <?php echo (float)$word_price; ?>;

(Though you'll need to first take your function out of count.js and put it in the product.tpl inside <script> tags)

And at the end of the function counter()

$('#product_price').val(word_price * wordCount);

That will get it displaying on the product detail page.

You'll also need to make changes in /system/engine/cart.php to calculate the price again when it gets added to the cart. (You don't want the browser to be telling your system what the price is!)

In function getProducts() in the following if statement..

elseif ($option_query->row['type'] == 'text' || $option_query->row['type'] == 'textarea' || $option_query->row['type'] == 'file' || $option_query->row['type'] == 'date' || $option_query->row['type'] == 'datetime' || $option_query->row['type'] == 'time') {

Remove the check for textarea.

Then after that elseif, add another:

elseif ($option_query->row['type'] == 'textarea') {

    $word_count = $this->countWords($option_value);

(countWords() is a private function you have to add in this cart.php file but you seem to have a handle on that already as you have it in your javascript)

    $product_price = $word_count * $product_query->row['price'];

$option_data[] = array(
    'product_option_id'       => $product_option_id,
    'product_option_value_id' => '',
    'option_id'               => $option_query->row['option_id'],
    'option_value_id'         => '',
    'name'                    => $option_query->row['name'],
    'option_value'            => $option_value,
    'type'                    => $option_query->row['type'],
    'quantity'                => '',
    'subtract'                => '',
    'price'                   => '',
    'price_prefix'            => '',
    'points'                  => '',
    'points_prefix'           => '',                                
    'weight'                  => '',
    'weight_prefix'           => ''
);
}                           

Then below that entire loop where you see

$price = $product_query->row['price'];

Replace with:

if (isset($product_price)) 
    $price = $product_price;
else
    $price = $product_query->row['price'];

Obviously the code needs cleaning up and I haven't tested it at all (e.g. it will trigger for all textarea options), but that's how I'd approach it. Though I'm not sure if it's much help if as you say your PHP knowledge is minimal.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top