Change SKU on select product size Magento [duplicate] - php

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Change Add to cart button to Preorder on select
I have a small issue with my script to select the sku of every product on select.
The issue is that, if i add 2 drop-down options the jquery is matching the same id twice.
But if i have only 1 drop-down with color or size, the sku is mapping showing correctly.
My question is... how can i get the correct sku for the second color ?
<?php
$_product = $this->getProduct();
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
?>
<?php if ($_product->isSaleable() && count($_attributes)):?>
<dl>
<?php foreach($_attributes as $_attribute): ?>
<dt><label class="required"><em>*</em><?php echo $_attribute->getLabel() ?></label></dt>
<dd<?php if ($_attribute->decoratedIsLast){?> class="last"<?php }?>>
<div class="input-box">
<select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select"
onchange="return changeSku(<?php echo $_attribute->getAttributeId() ?>, this);">
<option><?php echo $this->__('Choose an Option...') ?></option>
</select>
</div>
</dd>
<?php endforeach; ?>
</dl>
<script type="text/javascript">
var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);
</script>
<div id="sku-container"></div>
<?php
$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
$productMap = array();
foreach($col as $simpleProduct){
$productMap[$simpleProduct->getId()] = $simpleProduct->getAttributeText('sku');
}
?>
<script type="text/javascript">
document.observe("dom:loaded", function() {
$("sku-container").update("<strong>SKU: </strong> ...");
});
function changeSku(confAttributeId, sel) {
var productMap = <?php echo Mage::helper('core')->jsonEncode($productMap);?>;
var selectedAttributeId = sel.options[sel.selectedIndex].value;
if (selectedAttributeId) {
var options = spConfig.config.attributes[confAttributeId].options;
var productId = options.find(function (option) {return option.id == selectedAttributeId}).products[0]
$("sku-container").update("<strong>SKU: </strong>" + productMap[productId]);
} else {
$("sku-container").update("<strong>SKU: </strong> ...");
}
}
</script>
spConfig Result: var spConfig = new Product.Config({"attributes":{"80":{"id":"80","code":"color","label":"Color","options":[{"id":"42","label":"Blue","price":"0","oldPrice":"0","products":["6873","6874"]},{"id":"46","label":"Green","price":"0","oldPrice":"0","products":["6881","6882","6883","6884","6885"]}]},"123":{"id":"123","code":"size","label":"Size","options":[{"id":"13","label":"4 UK","price":"0","oldPrice":"0","products":["6881"]},{"id":"12","label":"4.5 UK","price":"0","oldPrice":"0","products":["6873","6882"]},{"id":"11","label":"5 UK","price":"0","oldPrice":"0","products":["6874","6883"]},{"id":"10","label":"5.5 UK","price":"0","oldPrice":"0","products":["6884"]},{"id":"9","label":"6 UK","price":"0","oldPrice":"0","products":["6885"]}]}},"template":"#{price}\u00a0USD","basePrice":"489","oldPrice":"489","productId":"9844","chooseText":"Select\u0163i o op\tion...","taxConfig":{"includeTax":true,"showIncludeTax":true,"showBothPrices":false,"defaultTax":24,"currentTax":24,"inclTaxTitle":"Whit tax"}});
Any help is appreciated.

Issue resolved by SCP Simple Configurable Product. made by Organic Internet.
Link to Extension

Related

Magento: Display Product Options in Product Page as List Elements in Two Columns

Having actual Magento 1.9.3 situation...
In product-page there are a lot of configuration options. Need to save space, so need to turn the one column vertical product options list into a two column list to reduce the space to 50%.
Already tried several methods, also this one:
https://magento.stackexchange.com/questions/70857/display-product-options-change-layout-of-in-block-after-info-column
in combination with
Css Styling list elements in two columns
and this
https://csswizardry.com/2010/02/mutiple-column-lists-using-one-ul/
But did not figure it out really. Was stopping on the results, to display all in only one horizontal line. The issue is, that if there are 10 attributes to select, the line is becoming very pressed and nothing is recognisable.
Is anyone out there, who is able to adjust the code?
Here is the code having in:
/template/catalog/product/view/type/options/configurable.phtml
<?php
$_product = $this->getProduct();
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
$_jsonConfig = $this->getJsonConfig();
$_renderers = $this->getChild('attr_renderers')->getSortedChildren();
?>
<?php if ($_product->isSaleable() && count($_attributes)):?>
<div class="items">
<dl>
<?php foreach($_attributes as $_attribute): ?>
<?php
$_rendered = false;
foreach ($_renderers as $_rendererName):
$_renderer = $this->getChild('attr_renderers')->getChild($_rendererName);
if (method_exists($_renderer, 'shouldRender') && $_renderer->shouldRender($_attribute, $_jsonConfig)):
$_renderer->setProduct($_product);
$_renderer->setAttributeObj($_attribute);
echo $_renderer->toHtml();
$_rendered = true;
break;
endif;
endforeach;
if (!$_rendered):
?>
<dt><label class="required"><em>*</em><?php echo $_attribute->getLabel() ?></label></dt>
<dd<?php if ($_attribute->decoratedIsLast){?> class="last"<?php }?>>
<div class="input-box">
<select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select">
<option><?php echo $this->__('Choose an Option...') ?></option>
</select>
</div>
</dd>
<?php endif; ?>
<?php endforeach; ?>
</dl>
</div>
<script type="text/javascript">
var spConfig = new Product.Config(<?php echo $_jsonConfig ?>);
</script>
<?php echo $this->getChildHtml('after') ?>
<?php endif;?>
Without Seeing the output its hard to know what you want exactly, but if you want the options to be in 2 columns this should work.
.items dl {
display:flex;
flex-wrap: wrap;
}
.items dl .options-wrapper{
width: 50%;
}
//If you want on smaller screens to make them 1 row.
#media screen and (max-width: 768px) {
.items dl .options-wrapper{
width: 100%;
}
}
<?php
$_product = $this->getProduct();
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
$_jsonConfig = $this->getJsonConfig();
$_renderers = $this->getChild('attr_renderers')->getSortedChildren();
?>
<?php if ($_product->isSaleable() && count($_attributes)):?>
<div class="items">
<dl>
<?php foreach($_attributes as $_attribute): ?>
<?php
$_rendered = false;
foreach ($_renderers as $_rendererName):
$_renderer = $this->getChild('attr_renderers')->getChild($_rendererName);
if (method_exists($_renderer, 'shouldRender') && $_renderer->shouldRender($_attribute, $_jsonConfig)):
$_renderer->setProduct($_product);
$_renderer->setAttributeObj($_attribute);
echo $_renderer->toHtml();
$_rendered = true;
break;
endif;
endforeach;
if (!$_rendered):
?>
<div class="options-wrapper">
<dt><label class="required"><em>*</em><?php echo $_attribute->getLabel() ?></label></dt>
<dd<?php if ($_attribute->decoratedIsLast){?> class="last"<?php }?>>
<div class="input-box">
<select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select">
<option><?php echo $this->__('Choose an Option...') ?></option>
</select>
</div>
</dd>
</div>
<?php endif; ?>
<?php endforeach; ?>
</dl>
</div>
<script type="text/javascript">
var spConfig = new Product.Config(<?php echo $_jsonConfig ?>);
</script>
<?php echo $this->getChildHtml('after') ?>
<?php endif;?>

AJAX - add to cart is not updating the cart in list.phtml (productlist page) in Magento

Above I have added an image of product list page.
My problem is when I add 5 items in the cart, it is adding properly. However, when I am increasing it to 6 and again "adding the same item to cart", the total quantity of that item becomes 11 in the shopping cart. But my requirement is not adding that (6+5=11) item to cart, it should become 6.
My code is below:
<?php if($_product->isSaleable()): ?>
<script type="text/javascript">
function setQty(id, url) {
var qty = document.getElementById('qty_' + id).value;
document.getElementById('cart_button_' + id).innerHTML = '<button type="button" class="button" onmouseup="setLocation(\'' + url + 'qty/' + qty + '/\')"><span><span>Add to Cart</span></span></button>';
}
</script>
<label for="qty"><?php echo $this->__('Qty:') ?></label>
<a class="decrement_qty" href="javascript:void(0)"> &nbsp - &nbsp</a>
<?php
$_quote = Mage::getSingleton('checkout/session')->getQuote();
$_item = $_quote ? $_quote->getItemByProduct($_product) : false;
$qty = $_item ? $_item->getQty() : 1;
$style = ($qty > 1) ? "background-color: #84c1e0;" : "background-color:white";
?>
<input type="text"
name="qty_<?php echo $_product->getId(); ?>"
id="qty_<?php echo $_product->getId(); ?>"
maxlength="12" value="<?php echo $qty;?>"
title="<?php echo $this->__('Qty') ?>" class="input-text qty" style=" <?php echo $style;?>"/>
<a class="increment_qty" href="javascript:void(0)"> &nbsp + &nbsp</a>
<span id="cart_button_<?php echo $_product->getId(); ?>">
<button type="button"
class="button"
onmousedown="setQty(<?php echo $_product->getId(); ?>, '<?php echo $this->getAddToCartUrl($_product) ?>');">
<span><span><?php echo $this->__('Add to Cart') ?></span></span>
</button>
</span>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
jquery code for increment and dcrement the quantity is below.
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('.increment_qty').click(function() {
var oldVal = jQuery(this).parent().find("input").val();
if ( parseFloat(oldVal) >= 1 ) {
var newVal = parseFloat(oldVal) + 1;
jQuery(this).parent().find("input").val(newVal);
}
});
jQuery('.decrement_qty').click(function() {
var oldVal = jQuery(this).parent().find("input").val();
if ( parseFloat(oldVal) >= 2 ) {
var newVal = parseFloat(oldVal) - 1;
jQuery(this).parent().find("input").val(newVal);
}
});
});
</script>
NB: I am also using an AJAX add to cart extension.

Disable Cash on Delivery for specific products

I am running Magento v1.9.0.1. I want to disable Cash on Delivery for specific products. I have installed COD extension from magento commerce.
I am new to Magento structure and no nothing to little about it. Tried some debugging on it but it is going up above my head.
Also, I have search every where over the internet but no one has ever provided the code for Magento v1.9.0.1.
Create a custom attribute with attribute code as cod and assign it to General of attribute sets.
Now as per you requirement change your methods.phtml (if there is no custom code included in this) as follows.
<?php
$methods = $this->getMethods();
$oneMethod = count($methods) <= 1;
?>
<?php if (empty($methods)): ?>
<dt>
<?php echo $this->__('No Payment Methods') ?>
</dt>
<?php else:
foreach ($methods as $_method):
$_code = $_method->getCode();
?>
<dt>
<?php
//cod verification starts
$attr_cod = array();
$cartItems = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
foreach ($cartItems as $item) {
$attr_cod[] = Mage::getModel('catalog/product')->load($item->getProduct()->getId())->getCod();
}
if(in_array('1', $attr_cod) && $_code == 'cashondelivery') {continue; }
//cod verification ends
?>
<?php if(!$oneMethod): ?>
<input id="p_method_<?php echo $_code ?>" value="<?php echo $_code ?>" type="radio" name="payment[method]" title="<?php echo $this->escapeHtml($_method->getTitle()) ?>" onclick="payment.switchMethod('<?php echo $_code ?>')"<?php if($this->getSelectedMethodCode()==$_code): ?> checked="checked"<?php endif; ?> class="radio" />
<?php else: ?>
<span class="no-display"><input id="p_method_<?php echo $_code ?>" value="<?php echo $_code ?>" type="radio" name="payment[method]" checked="checked" class="radio" /></span>
<?php $oneMethod = $_code; ?>
<?php endif; ?>
<label for="p_method_<?php echo $_code ?>"><?php echo $this->escapeHtml($this->getMethodTitle($_method)) ?> <?php echo $this->getMethodLabelAfterHtml($_method) ?></label>
</dt>
<?php if ($html = $this->getPaymentMethodFormHtml($_method)): ?>
<dd>
<?php echo $html; ?>
</dd>
<?php endif; ?>
<?php endforeach;
endif;
?>
<?php echo $this->getChildChildHtml('additional'); ?>
<script type="text/javascript">
//<![CDATA[
<?php echo $this->getChildChildHtml('scripts'); ?>
payment.init();
<?php if (is_string($oneMethod)): ?>
payment.switchMethod('<?php echo $oneMethod ?>');
<?php endif; ?>
//]]>
</script>
I have tested this and works fine.
COD is already in magento you didn't need to install it.
There are two way
create a new config setting in which you can enter ids of products
for which you didn't want COD option.
You can create product attribute too for checking COD option.
and check these ids (if you create config settings ) with the
product id or check for the product attribute on
app\design\frontend\base\default\template\checkout\onepage\payment\methods.phtml.
And by simple if else conditions you can avoid COD option for these
products.

Magento Related Product don't show up

I am trying to get the related products block to show up on my product detail page.
I have the folling code in the respective .phtml file
<?php
<?php echo "Related product block"?>
<?php if($this->getItems()->getSize()): ?>
<div class="block block-related">
<div class="block-title">
<strong><span><?php echo $this->__('Related Products') ?></span></strong>
</div>
<div class="block-content">
<p class="block-subtitle"><?php echo $this->__('Check items to add to the cart or') ?> <?php echo $this->__('select all') ?></p>
<ol class="mini-products-list" id="block-related">
<?php foreach($this->getItems() as $_item): ?>
<li class="item">
<?php if(!$_item->isComposite() && $_item->isSaleable()): ?>
<?php if (!$_item->getRequiredOptions()): ?>
<input type="checkbox" class="checkbox related-checkbox" id="related-checkbox<?php echo $_item->getId() ?>" name="related_products[]" value="<?php echo $_item->getId() ?>" />
<?php endif; ?>
<?php endif; ?>
<div class="product">
<img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail')->resize(50) ?>" width="50" height="50" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" />
<div class="product-details">
<p class="product-name"><?php echo $this->htmlEscape($_item->getName()) ?></p>
<?php echo $this->getPriceHtml($_item, true, '-related') ?>
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<?php echo $this->__('Add to Wishlist') ?>
<?php endif; ?>
</div>
</div>
</li>
<?php endforeach ?>
</ol>
<script type="text/javascript">decorateList('block-related', 'none-recursive')</script>
</div>
<script type="text/javascript">
//<![CDATA[
$$('.related-checkbox').each(function(elem){
Event.observe(elem, 'click', addRelatedToProduct)
});
var relatedProductsCheckFlag = false;
function selectAllRelated(txt){
if (relatedProductsCheckFlag == false) {
$$('.related-checkbox').each(function(elem){
elem.checked = true;
});
relatedProductsCheckFlag = true;
txt.innerHTML="<?php echo $this->__('unselect all') ?>";
} else {
$$('.related-checkbox').each(function(elem){
elem.checked = false;
});
relatedProductsCheckFlag = false;
txt.innerHTML="<?php echo $this->__('select all') ?>";
}
addRelatedToProduct();
}
function addRelatedToProduct(){
var checkboxes = $$('.related-checkbox');
var values = [];
for(var i=0;i<checkboxes.length;i++){
if(checkboxes[i].checked) values.push(checkboxes[i].value);
}
if($('related-products-field')){
$('related-products-field').value = values.join(',');
}
}
//]]>
</script>
The echo above the code shows up on my page. Which of course proves that i implemented the block correctly.
Just everything in the if-statement doesn't show.
I spend some time looking for solutions and i tried rebuilding the indexes and my related product is visible on the frontend.
Anyone know how i can fix this?
Magento will always return a low number or 0 for the condition:
$this->getItems()->getSize()
if some/all of the related products are in the user's cart, and consequently items will appear as though they are missing.
To prevent this behaviour, duplicate the core Magento 'Related' class:
/app/code/core/Mage/Catalog/Block/Product/List/Related.php
to local:
/app/code/local/Mage/Catalog/Block/Product/List/Related.php
Then comment out the following lines in the if statement:
if (Mage::helper('catalog')->isModuleEnabled('Mage_Checkout')) {
//mod - show all related products, even if they have been added to the cart
//Mage::getResourceSingleton('checkout/cart')->addExcludeProductFilter($this->_itemCollection,
// Mage::getSingleton('checkout/session')->getQuoteId()
//);
$this->_addProductAttributesAndPrices($this->_itemCollection);
}
On your 4 line:
<?php if($this->getItems()->getSize() > 0 ? true : false) ?>
Try it.
At the top of your code type
var_dump($this->getItems()->getSize()) // does this print NUll, False or > 0
If the code above print NULL or false or less than 1, then do
print_r($this->getItems());
If none of the above print the information you expected then check your block getItems() method
Also where is the closing IF for if($this->getItems()->getSize()):

AJAX problems with jquery request sent only some of the time

I have have a website where there are multiple products, the user can add one to their cart and get on screen feedback via ajax that the basket updated.
However on certain products this does not work below is the code that gets used.
THE PHP
function updateBasket()
{
$this->load->model('Checkout_model');
$this->load->model('Product_model');
$derivativeId = $this->input->post('selDerivative-1');
$quantity = $this->input->post('selQuantity');
$derivative = $this->Product_model->GetProducts(array('pdId' => $derivativeId), 'small');
// Add item to shopping bag.
$attributes = $this->Product_model->GetProductDerivatives(array('pdId' => $derivativeId));
$this->Checkout_model->AddProduct($derivative, $attributes, $quantity);
$this->data['message'] = 'Item added to Shopping Bag.';
// Update Delivery Price
$this->Checkout_model->updateDelivery(49);
$this->data['items'] = $this->Checkout_model->GetProducts();
$this->template
->build('checkout/quickbasket', $this->data);
}
THE HTML FEEDBACK
<?php
//var_dump($items);
//print_r($this->session->userdata);
?>
<div id="basketoverview">
<div id="quickbasket">
<h1><?php echo $this->cart->total_items(); ?> item in bag</h1>
<?php foreach ($items as $item) : ?>
<div class="item">
<img src="<?php echo base_url().$item['imageUrl'];?>" alt="<?php echo $item['imageAlt'];?>" width="70"/>
<h4><?php echo $item['imageAlt'];?></h4>
<span class="price">£<?php echo $item["price"]; ?></span>
<span class="qauntity">Quantity: <?php echo $item['qty']; ?></span>
</div>
<?php endforeach; ?>
</div>
<div id="basket_options">
VIEW BAG / CHECKOUT
</div>
</div>
** THE AJAX SCRIPT**
$("#frmProducts").submit(function(){
var dataSet = $("#frmProducts").serialize();
$.ajax({
url: "<?php echo base_url();?>products/updateBasket",
data: dataSet,
type: "POST",
success: function(data){
$('html, body').animate({scrollTop:0}, 'slow');
$("#miniCart").load("<?php echo base_url();?>checkout/loadCartView");
$('body').append(data);
$('#basketoverview').fadeIn(2000);
setTimeout(function () { $('#basketoverview').fadeOut(2000).hide(); }, 8000);
}
});
return false;
});
A SUCCESSFUL POST
selDerivative-1 171
selQuantity 1
submitted 1
** AN UNSUCCESSFUL POST **
selDerivative-1 223
selQuantity 1
selURL-1 colonial/dining/prestige-dining-for-six
submitted 1
The frmProducts form
<?php echo form_open(current_url(), array('id' => 'frmProducts'), array('submitted' => '1')); ?>
<div class="formRow">
<label for="rattanType"><?php echo $product_attribute_names; ?> </label><br />
<?php
$options = array();
foreach ($product_derivatives as $derivative) :
$options[$derivative['derivativeId']] = $derivative['attributeValues'];
endforeach;
?>
<?php echo form_dropdown('selDerivative-1', $options, $product_details->pdId, 'class="select clear" id="selDerivative-1"'); ?>
</div>
<?php if (count($individual_products) > 0) : ?>
<div class="formRow">
<label for="itemType">Item</label><br />
<select class="select clear" id="selURL-1" name="selURL-1">
<option value="<?php echo current_url(); ?>">Full Set</option>
<?php foreach ($individual_products as $product) : ?>
<option value="<?php echo site_url($product->fullProductPath); ?>"><?php echo $product->productTitle; ?> - £<?php echo ($product->productSavingType != 'none' ? $product->productSavingPrice : $product->productPrice); ?></option>
<?php endforeach; ?>
</select>
<input id="btnGo-1" name="btnGo-1" type="submit" value="GO" />
</div>
<?php endif; ?>
<div class="formRow">
<label for="addQty">Quantity</label><br />
<?php
$options = array();
for ($i = 1; $i < 10; $i++) :
$options[$i] = $i;
endfor;
?>
<?php echo form_dropdown('selQuantity', $options, '1', 'class="small select clear"'); ?>
</div>
<input type="submit" value="add to bag" name="btnAddToBag" id="btnAddToBag" />
<?php echo form_close(); ?>
I have absolutly why the first post would get added to the basket and the second would not, does any one have any idea from looking at my code?
This is kind of a longshot, but I have the suspicion that you have a session problem. The CI Session manager has a few issues randomly creating new sessions from Ajax calls. The issue and some solutions are discussed here:
http://codeigniter.com/forums/viewthread/186070/

Categories