How can I add custom price to cart as an attribute of product type in Drupal Commerce?

In detail am creating a custom tshirt design UI where user have the options to design a tshirt(add image,text etc) and add to cart. I have loaded all the products in the site to the UI and user have the option to add product to cart after designing. User is charged with an extra amount for designing like adding each images to tshirt which increases the base price of tshirt. But my issue is that the products get added to cart with the base price. I have used the folowing code to add the product to cart.

commerce_cart_product_add_by_id($pid, $quan, $combine = TRUE, $uid);

When I searched and found that there is no way to add the custom price as parameter of the above function so that the altered price gets added to cart. But found another solution that it can be done by adding as an attribute of the product.

How can I map the custom price I have created as an attribute of the product and add to cart for altering the price section?

  • Why does this have to be "programmatically"? Consider editng your question to explain the reason for that (or reword the question so that that does not appear to be a "must") .... Commented Jul 28, 2015 at 6:48
  • @Pierre.Vriens I just wanted to know how can I alter the price in the cart programatically?
    – Aby
    Commented Jul 28, 2015 at 7:26
  • What are you using the store the custom prices? Any chance you have a URL where this can be observed in action?
    – Brady
    Commented Jul 28, 2015 at 7:42
  • OK, now the question seems ok to me (and it leaves an option open to yes/no do it programatically) ... +1! Commented Jul 28, 2015 at 7:57
  • @Brady custom price is stored using a variable in js file. $(".sample_icons").click(function(){ pr1 = pr1 + 2; xr=xr+2; alert(pr1); var $srcimg=$(this).children("img").attr('src'); image_icon($srcimg); }); when the click function for adding an image to the tshirt is used, price increases by certain amount. here the variable is pr1 and it increases by 2 for each addition of image.
    – Aby
    Commented Jul 28, 2015 at 8:43

4 Answers 4


Below these modules will help you with bit of customization or addons

Commerce Product Attributes

This module provides some enhancements to the Drupal Commerce attribute handling.

Commerce Product Option

This module allows you to add user-customizable options to the commerce "add to cart" form. You do this by defining "option sets", to which you can attach any field of your choosing. These option sets can then be added to your commerce products using entity reference, after which the fields in your option set will show up in the add to cart form.

Commerce Pricing Attributes

This module provides some enhancements to the Commerce Product Option and Commerce Product Attributes handling by applying pricing rules to custom product attributes. So, you can add attributes to products that affect the final product price.

  • 1
    I have created a custom UI for this project. All the addition of products to cart takes place in this UI through custom module. How the modules mentioned above by you can help me?
    – Aby
    Commented Jul 28, 2015 at 7:17

Consider using Drupal Commerce's core product pricing rules system as explained in the Product Pricing Rules documentation (on DrupalCommerce.org). This is the only supported way to make consistent line item unit price alterations that are reflected in the final order total.


Use the following functions :

 $line_item = commerce_product_line_item_new($product, $quantity, $order_id, $data = array(), $type);

// You can add the required additional price components here.

    //Later using the following function will create the line item with the required price.

    commerce_cart_product_add($uid, $line_item, $combine = TRUE);

//If you insist on changing the Base price of the products and not adding price components to Line item then you need to include Price change logic into the line item refresh hook.

Get more details on this hook : http://api.drupalcommerce.org/api/Drupal%20Commerce/sites!all!modules!commerce!modules!cart!commerce_cart.api.php/function/hook_commerce_cart_line_item_refresh/DC


Check the following links. May be this will be useful.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.