{"id":1331,"date":"2020-05-12T20:10:11","date_gmt":"2020-05-12T13:10:11","guid":{"rendered":"https:\/\/lab.wptips.dev\/?p=1331"},"modified":"2020-05-23T12:09:12","modified_gmt":"2020-05-23T05:09:12","slug":"timber-woocommerce-2","status":"publish","type":"post","link":"https:\/\/pixelstudio.id\/blog\/timber-woocommerce-2\/","title":{"rendered":"Timber + WooCommerce Ch. 2 &#8211; Cart Dropdown"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\"><p>Timber + WooCommerce is our series of integrating WooCommerce with Timber Library.<\/p><p><a href=\"https:\/\/pixelstudio.id\/blog\/timber-woocommerce-1\/\">Ch.1 &#8211; Shop and Single page<\/a><br><a href=\"https:\/\/pixelstudio.id\/blog\/timber-woocommerce-2\/\">Ch.2 &#8211; Cart Dropdown<\/a><br>Ch.3 &#8211; Login Page (Coming Soon)<\/p><\/blockquote>\n\n\n\n<p>Most WooCommerce theme has that cart dropdown which shows all products you have selected. In this tutorial, we are going to create that with Timber.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/05\/wctimber2-sample.jpg\" alt=\"\" class=\"wp-image-1424\" width=\"720\" height=\"336\" srcset=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/05\/wctimber2-sample.jpg 750w, https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/05\/wctimber2-sample-480x225.jpg 480w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><figcaption>Cart Dropdown in Storefront Theme<\/figcaption><\/figure>\n\n\n\n<p class=\"has-small-font-size\"><strong>Note<\/strong>: WooCommerce version used in this article is 3.9.0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Preparing the Template<\/h2>\n\n\n\n<p>First, decide where you want to place it.<\/p>\n\n\n\n<p>Ideally, it should be at the top, besides the navigation menu. But for simplicity&#8217;s sake, we are going to make it floating at the bottom-right corner like below:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"514\" height=\"593\" src=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/05\/wctimber2-cart.jpg\" alt=\"\" class=\"wp-image-1419\" srcset=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/05\/wctimber2-cart.jpg 514w, https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2020\/05\/wctimber2-cart-416x480.jpg 416w\" sizes=\"auto, (max-width: 514px) 100vw, 514px\" \/><figcaption>Floating Cart button that shows selected products on click<\/figcaption><\/figure><\/div>\n\n\n\n<p>Then create <strong>2 template files<\/strong>: One contains just the button\/trigger, and the other one  contains the whole package:<\/p>\n\n\n\n<pre title=\"\/views\/shop\/_cart-button.twig\" class=\"wp-block-code\"><code lang=\"twig\" class=\"language-twig\">{# The button template #}\n\n&lt;a id=\"cart-button\"\n  class=\"{{ woo.cart.is_empty ? 'button button--passive' : 'button' }}\"\n  href=\"{{ fn('wc_get_cart_url') }}\">\n\n  &lt;span>Cart&lt;\/span>\n  &lt;b>({{ woo.cart.get_cart_contents_count }})&lt;\/b>\n&lt;\/a>\n<\/code><\/pre>\n\n\n\n<pre title=\"\/views\/shop\/cart.twig\" class=\"wp-block-code\"><code lang=\"twig\" class=\"language-twig\">{# The whole template, including the button #}\n\n&lt;aside class=\"cart-menu\">\n  {% include 'shop\/_cart-button.twig' %}\n\n  &lt;div class=\"cart-dialog\">\n    {# The product list is actually an existing widget #}\n    {{ fn('the_widget', 'WC_Widget_Cart' ) }}\n  &lt;\/div>\n&lt;\/aside><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Echo It<\/h2>\n\n\n\n<p>Since I want to place it floating at the bottom-right corner, I chose to echo it at <code>wp_footer<\/code>:<\/p>\n\n\n\n<pre title=\"functions.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">add_action( 'wp_footer', function() {\n  global $woocommerce;\n  $context = [ 'woo' => $woocommerce ];\n\n  Timber::render( 'shop\/cart.twig', $context );\n} );<\/code><\/pre>\n\n\n\n<p>If you want to put it besides the navigation menu, then you can include it in your Header template like below: (Don&#8217;t forget to add <code>$woocommerce<\/code> object named <code>woo<\/code> to context).<\/p>\n\n\n\n<pre title=\"\/views\/layout.twig\" class=\"wp-block-code\"><code lang=\"twig\" class=\"language-twig\">&lt;header class=\"site-header\">\n  &lt;nav class=\"site-nav\"> ... &lt;\/nav>\n\n  {% include \"shop\/cart.twig\" %}\n&lt;\/header><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Enable AJAX Update on Button<\/h2>\n\n\n\n<p>When you click &#8220;Add to Cart&#8221; in the product listing, it&#8217;s appended to your Cart Widget without refreshing the page.<\/p>\n\n\n\n<p>This is done by using a JavaScript technique called AJAX.<\/p>\n\n\n\n<p>But the button that shows the product count is not updated. We need to fix this by using this hook:<\/p>\n\n\n\n<pre title=\"functions.php\" class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">\/\/ Run everytime the cart widget is updated\nadd_filter( 'woocommerce_add_to_cart_fragments', function( $fragments ) {\n  ob_start();\n  global $woocommerce;\n  $context = [ 'woo' => $woocommerce ];\n\n  Timber::render( 'shop\/_cart-button.twig', $context );\n  \n  \/\/ Must be the same button ID with the one we use in template\n  $fragments['#cart-button'] = ob_get_clean();\n  return $fragments;\n} );<\/code><\/pre>\n\n\n\n<p>Even though that hook looks weird, that method is used by the <a href=\"https:\/\/woocommerce.com\/storefront\/\">official Storefront theme<\/a>. So don&#8217;t worry, it&#8217;s the right way.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Done?<\/h2>\n\n\n\n<p>There is actually one last step: <strong>Writing the CSS and JS<\/strong> to toggle the dropdown. But I won&#8217;t dive into that because it&#8217;s up to your preferences.<\/p>\n\n\n\n<p>If you like to see my styling, I put it <a rel=\"noreferrer noopener\" aria-label=\"in this CodePen (opens in a new tab)\" href=\"https:\/\/codepen.io\/hrsetyono\/pen\/QWjxJZV\" target=\"_blank\">in CodePen<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-dots\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Figuring out Cart Dropdown in Timber took me the longest back then. There are no resources available online, so I need to experiment on my own.<\/p>\n\n\n\n<p>In my first iteration, I was using the global <code>$woocommerce<\/code> object to extract Cart data. It does work, but you need to write your own AJAX update.<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-dots\"\/>\n\n\n\n<p>I initially planned to discuss Cart Dropdown and User Sign-in for Part 2. But I feel the first topic is already long enough. So it will be reserved for Part 3 \ud83d\ude42<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Let me know if you have any question regarding Cart dropdown with Timber \ud83d\ude42<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Most WooCommerce theme has that cart dropdown which shows all products you have selected. In this tutorial, we are going to create that with Timber.<\/p>\n","protected":false},"author":1,"featured_media":1426,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[24,25],"class_list":["post-1331","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-timber","tag-timber","tag-woocommerce"],"blocksy_meta":"","_links":{"self":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1331","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/comments?post=1331"}],"version-history":[{"count":9,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1331\/revisions"}],"predecessor-version":[{"id":1471,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/1331\/revisions\/1471"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/media\/1426"}],"wp:attachment":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/media?parent=1331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/categories?post=1331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/tags?post=1331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}