Best way to code a shopping cart consisting of items with different properties

This is an interview question. Interviewer wanted a scalable solution

Goal is to find value of shopping cart. Cart could have 3 different types of items(Physical,Digital,URL).

Only Physical Items have a shipping address.

Only Digital items have a redeemable code that is used in a merchant site.

Only URL items have a URL that gives access to some online resource.

I’ve suggested having an Item class with price property which is extended by classes PhysicalItem, DigitalItem, UrlItem. Objects of these classes added to list which is iterated over to find the total order amount. Interviewer wasn’t impressed.

Is there any good design pattern/approach that is best applied here?

Trying to style Woocommerce sidebar Cart Widget

I have looked at length to find CSS that will change the background color, border radius and product info text color. I think this may not be a "mini cart". The price of the item, subtotal and View Cart / Checkout buttons follow the styling Ive done to the cart. I have successfully changed my product text color on my actual Cart page. So frustrating! Help is much appreciated.

Changing the Shopping cart Table order / VAT disappeared

I want to change the column order in the Shopping cart table. I want to exchange the Posititions of the Sum and VAT Column so that the Sum is the Last position. I guest the right php Template is the cart.php. Also the VAT name disappeared somehow in my column. I have VAT activated in all of my plugins and settings but it wont show :S I would be really thankful if the Pros out there could help me 🙂

enter image description here

    <?php /**  * Cart Page  *  * This template can be overridden by copying it to yourtheme/woocommerce/cart/cart.php.  *  * HOWEVER, on occasion WooCommerce will need to update template files and you  * (the theme developer) will need to copy the new files to your theme to  * maintain compatibility. We try to do this as little as possible, but it does  * happen. When this occurs the version of the template file will be bumped and  * the readme will list any important changes.  *  * @see     https://docs.woocommerce.com/document/template-structure/  * @package WooCommerce/Templates  * @version 3.8.0  */  defined( 'ABSPATH' ) || exit;  do_action( 'woocommerce_before_cart' ); ?>  <form class="woocommerce-cart-form" action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post">     <?php do_action( 'woocommerce_before_cart_table' ); ?>      <table class="shop_table shop_table_responsive cart woocommerce-cart-form__contents" cellspacing="0">         <thead>             <tr>                 <th class="product-remove">&nbsp;</th>                 <th class="product-thumbnail">&nbsp;</th>                 <th class="product-name"><?php esc_html_e( 'Product', 'woocommerce' ); ?></th>                 <th class="product-price"><?php esc_html_e( 'Price', 'woocommerce' ); ?></th>                 <th class="product-quantity"><?php esc_html_e( 'Quantity', 'woocommerce' ); ?></th>                 <th class="product-subtotal"><?php esc_html_e( 'Subtotal', 'woocommerce' ); ?></th>             </tr>         </thead>         <tbody>             <?php do_action( 'woocommerce_before_cart_contents' ); ?>              <?php             foreach ( WC()->cart->get_cart() as $  cart_item_key => $  cart_item ) {                 $  _product   = apply_filters( 'woocommerce_cart_item_product', $  cart_item['data'], $  cart_item, $  cart_item_key );                 $  product_id = apply_filters( 'woocommerce_cart_item_product_id', $  cart_item['product_id'], $  cart_item, $  cart_item_key );                  if ( $  _product && $  _product->exists() && $  cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $  cart_item, $  cart_item_key ) ) {                     $  product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $  _product->is_visible() ? $  _product->get_permalink( $  cart_item ) : '', $  cart_item, $  cart_item_key );                     ?>                     <tr class="woocommerce-cart-form__cart-item <?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $  cart_item, $  cart_item_key ) ); ?>">                          <td class="product-remove">                             <?php                                 echo apply_filters( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped                                     'woocommerce_cart_item_remove_link',                                     sprintf(                                         '<a href="%s" class="remove" aria-label="%s" data-product_id="%s" data-product_sku="%s">&times;</a>',                                         esc_url( wc_get_cart_remove_url( $  cart_item_key ) ),                                         esc_html__( 'Remove this item', 'woocommerce' ),                                         esc_attr( $  product_id ),                                         esc_attr( $  _product->get_sku() )                                     ),                                     $  cart_item_key                                 );                             ?>                         </td>                          <td class="product-thumbnail">                         <?php                         $  thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $  _product->get_image(), $  cart_item, $  cart_item_key );                          if ( ! $  product_permalink ) {                             echo $  thumbnail; // PHPCS: XSS ok.                         } else {                             printf( '<a href="%s">%s</a>', esc_url( $  product_permalink ), $  thumbnail ); // PHPCS: XSS ok.                         }                         ?>                         </td>                          <td class="product-name" data-title="<?php esc_attr_e( 'Product', 'woocommerce' ); ?>">                         <?php                         if ( ! $  product_permalink ) {                             echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $  _product->get_name(), $  cart_item, $  cart_item_key ) . '&nbsp;' );                         } else {                             echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $  product_permalink ), $  _product->get_name() ), $  cart_item, $  cart_item_key ) );                         }                          do_action( 'woocommerce_after_cart_item_name', $  cart_item, $  cart_item_key );                          // Meta data.                         echo wc_get_formatted_cart_item_data( $  cart_item ); // PHPCS: XSS ok.                          // Backorder notification.                         if ( $  _product->backorders_require_notification() && $  _product->is_on_backorder( $  cart_item['quantity'] ) ) {                             echo wp_kses_post( apply_filters( 'woocommerce_cart_item_backorder_notification', '<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'woocommerce' ) . '</p>', $  product_id ) );                         }                         ?>                         </td>                          <td class="product-price" data-title="<?php esc_attr_e( 'Price', 'woocommerce' ); ?>">                             <?php                                 echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $  _product ), $  cart_item, $  cart_item_key ); // PHPCS: XSS ok.                             ?>                         </td>                          <td class="product-quantity" data-title="<?php esc_attr_e( 'Quantity', 'woocommerce' ); ?>">                         <?php                         if ( $  _product->is_sold_individually() ) {                             $  product_quantity = sprintf( '1 <input type="hidden" name="cart[%s][qty]" value="1" />', $  cart_item_key );                         } else {                             $  product_quantity = woocommerce_quantity_input(                                 array(                                     'input_name'   => "cart[{$  cart_item_key}][qty]",                                     'input_value'  => $  cart_item['quantity'],                                     'max_value'    => $  _product->get_max_purchase_quantity(),                                     'min_value'    => '0',                                     'product_name' => $  _product->get_name(),                                 ),                                 $  _product,                                 false                             );                         }                          echo apply_filters( 'woocommerce_cart_item_quantity', $  product_quantity, $  cart_item_key, $  cart_item ); // PHPCS: XSS ok.                         ?>                         </td>                          <td class="product-subtotal" data-title="<?php esc_attr_e( 'Subtotal', 'woocommerce' ); ?>">                             <?php                                 echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $  _product, $  cart_item['quantity'] ), $  cart_item, $  cart_item_key ); // PHPCS: XSS ok.                             ?>                         </td>                     </tr>                     <?php                 }             }             ?>              <?php do_action( 'woocommerce_cart_contents' ); ?>              <tr>                 <td colspan="6" class="actions">                      <?php if ( wc_coupons_enabled() ) { ?>                         <div class="coupon">                             <label for="coupon_code"><?php esc_html_e( 'Coupon:', 'woocommerce' ); ?></label> <input type="text" name="coupon_code" class="input-text" id="coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" /> <button type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>"><?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?></button>                             <?php do_action( 'woocommerce_cart_coupon' ); ?>                         </div>                     <?php } ?>                      <button type="submit" class="button" name="update_cart" value="<?php esc_attr_e( 'Update cart', 'woocommerce' ); ?>"><?php esc_html_e( 'Update cart', 'woocommerce' ); ?></button>                      <?php do_action( 'woocommerce_cart_actions' ); ?>                      <?php wp_nonce_field( 'woocommerce-cart', 'woocommerce-cart-nonce' ); ?>                 </td>             </tr>              <?php do_action( 'woocommerce_after_cart_contents' ); ?>         </tbody>     </table>     <?php do_action( 'woocommerce_after_cart_table' ); ?> </form>  <?php do_action( 'woocommerce_before_cart_collaterals' ); ?>  <div class="cart-collaterals">     <?php         /**          * Cart collaterals hook.          *          * @hooked woocommerce_cross_sell_display          * @hooked woocommerce_cart_totals - 10          */         do_action( 'woocommerce_cart_collaterals' );     ?> </div>  <?php do_action( 'woocommerce_after_cart' ); ?> 

WooComemrce Show selected color beside color variation title and separate Add to cart button from quantity

I need some help doing such changes on my site. First of all, I want to show n selected color label to show beside Color title. and second of all, I want to show Add to cart in a separate row not in the same row with quantity. I am attaching a picture below for the reference. Pardon me if it is a silly question I am learning about WooCoomerce. w

Add specific products to cart when a button is pressed

Im making this website that is basically going to be just a 2 product page and a checkout, and due to its target audience, the idea is to have two images, 2 number boxes under them and just one button to add the products to the cart and then to redirect to the checkout page.

What i imagined is having a form, with a button, that would call a PHP function and would get the value from the number boxes and then programatically add the products to the cart (do i insert this function in functions.php ?). However after some reading i found that i may have to use AJAX to connect the HTML code to the PHP.

Do you have any suggestions? Is my way of thinking flawed?

In a woocommerce’s project, How to update the cart quantity with ajax?

I’m building my own woocommerce theme with an HTML template that I bought for it. The question is: how can i update the cart’s label text with the ajax when a new product is added?

See my code to display the cart’s count:

<span class="cart_count" name='update_cart'><?= wc()->cart->cart_contents_count?></span> 

The button’s code to add a new product to the cart:

<li class="add-to-cart">   <a product-id="<?= $  featured_product->id ?>" href="#" class="single_add_to_cart_button">     <i class="icon-basket-loaded"></i>   </a> </li> 

the add-cart.php function:

<?php  function woocommerce_ajax_add_to_cart_js() {   if (function_exists('is_product') && is_product()) {       wp_enqueue_script('woocommerce-ajax-add-to-cart', plugin_dir_url(__FILE__) . 'assets/ajax-add-to-cart.js', array('jquery'), '', true);   } } add_action('wp_enqueue_scripts', 'woocommerce_ajax_add_to_cart_js', 99);  add_action('wp_ajax_woocommerce_ajax_add_to_cart', 'woocommerce_ajax_add_to_cart'); add_action('wp_ajax_nopriv_woocommerce_ajax_add_to_cart', 'woocommerce_ajax_add_to_cart');  function woocommerce_ajax_add_to_cart() {             $  product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($  _POST['product_id']));             $  quantity = empty($  _POST['quantity']) ? 1 : wc_stock_amount($  _POST['quantity']);             $  variation_id = absint($  _POST['variation_id']);             $  passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $  product_id, $  quantity);             $  product_status = get_post_status($  product_id);              if ($  passed_validation && WC()->cart->add_to_cart($  product_id, $  quantity, $  variation_id) && 'publish' === $  product_status) {                  do_action('woocommerce_ajax_added_to_cart', $  product_id);                  if ('yes' === get_option('woocommerce_cart_redirect_after_add')) {                     wc_add_to_cart_message(array($  product_id => $  quantity), true);                 }                  WC_AJAX :: get_refreshed_fragments();             } else {                  $  data = array(                     'error' => true,                     'product_url' => apply_filters('woocommerce_cart_redirect_after_error', get_permalink($  product_id), $  product_id));                  echo wp_send_json($  data);             }              wp_die();         }  ?> 

the ajax-add-to-cart.js :

(function ($  ) {   $  (document).on("click", ".single_add_to_cart_button", function (e) {     e.preventDefault();      var $  thisbutton = $  (this),       product_qty = 1,       product_id = $  thisbutton.attr("product-id") || id,       variation_id = 0;      var data = {       action: "woocommerce_ajax_add_to_cart",       product_id: product_id,       product_sku: "",       quantity: product_qty,       variation_id: variation_id,     };      $  (document.body).trigger("adding_to_cart", [$  thisbutton, data]);      $  .ajax({       type: "post",       url: wc_add_to_cart_params.ajax_url,       data: data,       beforeSend: function (response) {         $  thisbutton.removeClass("added").addClass("loading");       },       complete: function (response) {         $  thisbutton.addClass("added").removeClass("loading");       },       success: function (response) {         if (response.error & response.product_url) {           window.location = response.product_url;           return;         } else {           $  (document.body).trigger("added_to_cart", [             response.fragments,             response.cart_hash,             $  thisbutton,           ]);         }       },     });      return false;   }); })(jQuery); 

I’ve already tried adding a script to the wp_footer:

jQuery('div.woocommerce').on('change', '.qty', function() {   jQuery("[name='update_cart']").prop("disabled", false);   jQuery("[name='update_cart']").trigger("click"); }); 

but I actually don’t know what dom element should be that div.woocommerce.

Woocommerce redirection to checkout for certain after product added to cart

I have implement this code in function.php to redirect the page to checkout if product with id(26285) added to the cart .This is the code

add_action( 'woocommerce_add_to_cart', 'custom_add_to_cart', 1, 2 ); function custom_add_to_cart( $  cart_item_key, $  product_id ) {  $  site_url = get_site_url(); if($  product_id == 26285) {  // Redirecting to some url wp_redirect($  site_url.'/my-account/'); exit; }} 

the condition i need to satisfy here is , if the product with ID:26285 added to the cart i need to redirect to checkout.The problem i am facing now is the page is redirecting but the product was not adding to cart . How to achive that?

Your help will very much appreciated. Have a nice day!

Add to Cart / Remove item from Car works only with base url

I’ve experienced a problem when strange situation is happening – I can only add/remove items to cart using the base url. Works:

<base_url>/?addtocart=10 | <base_url>/?remove_item=<item_key>&_wpnonce=<nonce>   

Doesn’t work:

<base_url>/<cart_slug>/?addtocart=10 | <base_url>/<cart_slug>/?remove_item=<item_key>&_wpnonce=<nonce>   

Any idea why it works that way?