<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Lao Ning</title>
    <description>The latest articles on DEV Community by Lao Ning (@laoning).</description>
    <link>https://dev.to/laoning</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1227342%2Ff72c78d3-f67d-4db5-9c49-6be7efd5c999.png</url>
      <title>DEV Community: Lao Ning</title>
      <link>https://dev.to/laoning</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/laoning"/>
    <language>en</language>
    <item>
      <title>Implementing a Dynamic Free Shipping Progress Bar in Shopify with JavaScript</title>
      <dc:creator>Lao Ning</dc:creator>
      <pubDate>Mon, 18 Dec 2023 12:34:42 +0000</pubDate>
      <link>https://dev.to/laoning/implementing-a-dynamic-free-shipping-progress-bar-in-shopify-with-javascript-59o</link>
      <guid>https://dev.to/laoning/implementing-a-dynamic-free-shipping-progress-bar-in-shopify-with-javascript-59o</guid>
      <description>&lt;p&gt;In the competitive e-commerce world, online stores must innovate to boost average order values and enhance customer experiences.&lt;/p&gt;

&lt;p&gt;A key strategy is adding a free shipping progress bar to Shopify stores. This feature motivates customers to increase their cart size, thus improving their shopping experience. &lt;/p&gt;

&lt;p&gt;We'll show you how to add a dynamic free shipping progress bar to your Shopify store with the help of JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Free Shipping Progress Bar?
&lt;/h2&gt;

&lt;p&gt;A free shipping progress bar is a visual indicator. It shows how close customers are to earning free shipping based on their cart value. &lt;/p&gt;

&lt;p&gt;For example, Imagine you're browsing an online clothing store, adding cute sweaters and comfy sweatpants to your cart. Suddenly, a progress bar appears, inching closer to "Free Shipping!" with every item. Now, picture the thrill of seeing it reach 100% and realizing you just snagged free delivery on your cozy haul. That's the magic of a free shipping progress bar in Shopify!&lt;/p&gt;

&lt;p&gt;By encouraging customers to add items to reach the free shipping limit, it helps increase the store's average order value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step-by-Step Implementation
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Set Your Free Shipping Threshold&lt;/strong&gt;&lt;br&gt;
Before implementing the progress bar, determine the minimum order value for free shipping in your store. This amount will be the target customers try to reach as they shop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Access Your Shopify Theme Code&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Log in to your Shopify admin panel.&lt;/li&gt;
&lt;li&gt;Navigate to Online Store &amp;gt; Themes.&lt;/li&gt;
&lt;li&gt;Find your active theme, click Actions, and select Edit code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Insert the Progress Bar HTML&lt;/strong&gt;&lt;br&gt;
In your theme's code editor, locate the file where you want the progress bar to appear, such as &lt;code&gt;cart.liquid&lt;/code&gt; or &lt;code&gt;header.liquid&lt;/code&gt;. Insert the following HTML code where you want the progress bar to display:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="free-shipping-progress-bar"&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4: Style the Progress Bar&lt;/strong&gt;&lt;br&gt;
Add custom CSS to your theme's &lt;code&gt;theme.scss.liquid&lt;/code&gt; file to style the progress bar:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.free-shipping-progress-bar {
    height: 20px;
    background-color: #ddd;
    border-radius: 5px;
    transition: width 0.3s ease;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 5: Implement the Progress Logic with JavaScript&lt;/strong&gt;&lt;br&gt;
This is where the dynamic aspect of the progress bar comes into play. You will need to edit your theme's JavaScript file (usually &lt;code&gt;theme.js&lt;/code&gt; or &lt;code&gt;theme.js.liquid&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A. Define the Free Shipping Threshold&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const freeShippingThreshold = 50; // Set your free shipping threshold
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;B. Get the Current Cart Total&lt;/strong&gt;&lt;br&gt;
Use Shopify's AJAX API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getCartTotal(callback) {
    fetch('/cart.js')
        .then(response =&amp;gt; response.json())
        .then(data =&amp;gt; callback(data.total_price / 100)); // Price in cents
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;C. Update the Progress Bar&lt;/strong&gt;&lt;br&gt;
Create a function to adjust the progress bar's width:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function updateProgressBar(cartTotal) {
    let progressBar = document.querySelector('.free-shipping-progress-bar');
    let percentage = Math.min(cartTotal / freeShippingThreshold * 100, 100);
    progressBar.style.width = percentage + '%';
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;D. Initial Update and Event Binding&lt;/strong&gt;&lt;br&gt;
Ensure the bar updates correctly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.addEventListener('DOMContentLoaded', () =&amp;gt; {
    getCartTotal(updateProgressBar);
});

document.addEventListener('cart:updated', () =&amp;gt; {
    getCartTotal(updateProgressBar);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 6: Testing and Optimization&lt;/strong&gt;&lt;br&gt;
After implementing the JavaScript logic, test the progress bar by adding or removing items from the cart. Ensure that the bar updates in real time and is compatible across different browsers and devices.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In Final words,&lt;/strong&gt;&lt;br&gt;
Adding a dynamic free shipping progress bar can significantly improve the shopping experience and boost sales.&lt;/p&gt;

&lt;p&gt;This feature engages customers to add more to their carts, increasing your store's average order value. With our guide, anyone with basic coding skills can implement this enhancement in their Shopify store.&lt;/p&gt;

&lt;p&gt;To get more Shopify resources, Visit: &lt;a href="https://meetanshi.com/blog/category/shopify/"&gt;https://meetanshi.com/blog/category/shopify/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>shopify</category>
    </item>
    <item>
      <title>How to Create a Custom Magento 2 API?</title>
      <dc:creator>Lao Ning</dc:creator>
      <pubDate>Fri, 08 Dec 2023 09:51:12 +0000</pubDate>
      <link>https://dev.to/laoning/how-to-create-a-custom-magento-2-api-4pde</link>
      <guid>https://dev.to/laoning/how-to-create-a-custom-magento-2-api-4pde</guid>
      <description>&lt;p&gt;Magento 2 is a robust e-commerce platform with extensive API support. While it offers default REST APIs, you might need to create a custom REST API. This article explores the process of creating a custom REST API in Magento 2.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Magento 2 Custom REST API?
&lt;/h2&gt;

&lt;p&gt;A Custom REST API in Magento 2 is a web service endpoint you create for specific actions or data retrieval from your Magento store. Custom APIs are tailored to your business needs. They integrate Magento with external systems, mobile apps, or other software applications. Custom APIs are crucial when default APIs do not cover certain functionalities or data needs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenarios for Creating a Custom REST API in Magento 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Integrating with Third-party Systems: Custom APIs allow data exchange with external platforms like ERP systems, CRM software, or payment gateways.&lt;/li&gt;
&lt;li&gt;Building Mobile Apps: These APIs are essential for mobile apps that need interaction with your Magento store.&lt;/li&gt;
&lt;li&gt;Implementing Unique Features: Custom APIs enable features like unique loyalty programs or personalized product recommendation engines.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Steps to Create a Custom Magento 2 REST API:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Set Up Your Development Environment&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ensure that you have a development environment with Magento 2 installed. You'll also need a code editor and a REST client for testing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Define Your API Endpoints&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Determine the functionality you want to expose. For this example, let's create an API to retrieve product details by SKU.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Create a Module&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a custom module for your API. Replace Vendor and Module with your own module and namespace names.&lt;/p&gt;

&lt;p&gt;Create &lt;strong&gt;&lt;em&gt;app/code/Vendor/Module/etc/module.xml&lt;/em&gt;&lt;/strong&gt; with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"&amp;gt;
    &amp;lt;module name="Vendor_Module" setup_version="1.0.0" /&amp;gt;
&amp;lt;/config&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create &lt;strong&gt;&lt;em&gt;app/code/Vendor/Module/registration.php&lt;/em&gt;&lt;/strong&gt; with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4: Create a Web API Schema&lt;/strong&gt;&lt;br&gt;
Create a webapi.xml file to define the API endpoint.&lt;/p&gt;

&lt;p&gt;Create &lt;strong&gt;&lt;em&gt;app/code/Vendor/Module/etc/webapi.xml&lt;/em&gt;&lt;/strong&gt; with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd"&amp;gt;
    &amp;lt;route method="GET" url="/V1/custom/product/:sku"&amp;gt;
        &amp;lt;service class="Vendor\Module\Api\ProductInterface" method="getProductBySku"/&amp;gt;
        &amp;lt;resources&amp;gt;
            &amp;lt;resource ref="anonymous"/&amp;gt;
        &amp;lt;/resources&amp;gt;
    &amp;lt;/route&amp;gt;
&amp;lt;/routes&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 5: Create a Di.xml File&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a di.xml file to specify the implementation of your API interface.&lt;/p&gt;

&lt;p&gt;Create &lt;strong&gt;&lt;em&gt;app/code/Vendor/Module/etc/di.xml&lt;/em&gt;&lt;/strong&gt; with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"&amp;gt;
    &amp;lt;preference for="Vendor\Module\Api\ProductInterface" type="Vendor\Module\Model\Product"/&amp;gt;
&amp;lt;/config&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 6: Create the API Interface&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create an interface for your API. This defines the methods your API will expose.&lt;/p&gt;

&lt;p&gt;Create &lt;strong&gt;&lt;em&gt;app/code/Vendor/Module/Api/ProductInterface.php&lt;/em&gt;&lt;/strong&gt; with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
namespace Vendor\Module\Api;

interface ProductInterface
{
    /**
     * Retrieve product details by SKU
     *
     * @param string $sku
     * @return \Vendor\Module\Api\Data\ProductInterface
     * @throws \Magento\Framework\Exception\NoSuchEntityException If product with the specified SKU doesn't exist.
     */
    public function getProductBySku($sku);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 7: Implement the API Interface&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create the implementation of your API interface.&lt;/p&gt;

&lt;p&gt;Create &lt;strong&gt;&lt;em&gt;app/code/Vendor/Module/Model/Product.php&lt;/em&gt;&lt;/strong&gt; with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
namespace Vendor\Module\Model;

use Vendor\Module\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;

class Product implements ProductInterface
{
    /**
     * @var \Magento\Catalog\Api\ProductRepositoryInterface
     */
    protected $productRepository;

    public function __construct(
        ProductRepositoryInterface $productRepository
    ) {
        $this-&amp;gt;productRepository = $productRepository;
    }

    /**
     * {@inheritdoc}
     */
    public function getProductBySku($sku)
    {
        try {
            $product = $this-&amp;gt;productRepository-&amp;gt;get($sku);
            return $product;
        } catch (NoSuchEntityException $e) {
            throw new NoSuchEntityException(__('Product with SKU %1 not found.', $sku));
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 8: Test Your Custom API&lt;/strong&gt;&lt;br&gt;
You can now test your custom API using a REST client like Postman. Make a GET request to the following endpoint:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[YourMagentoURL]/rest/V1/custom/product/{SKU}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace {SKU} with the SKU of the product you want to retrieve. You should receive a JSON response with the product details.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; This post was referenced from &lt;a href="https://meetanshi.com/blog/create-custom-rest-api-in-magento-2/" rel="noopener noreferrer"&gt;https://meetanshi.com/blog/create-custom-rest-api-in-magento-2/&lt;/a&gt;. It may contain excerpts from the original author.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you have any questions or need further clarification, please feel free to leave a comment.&lt;/p&gt;

</description>
      <category>magento</category>
      <category>restapi</category>
      <category>magento2</category>
    </item>
  </channel>
</rss>
