how to show our shipping rates on checkout page by shopify app

how to show our shipping rates on checkout page by shopify app

alvinchrist_22
Shopify Partner
1 0 0

im struggling with development shopify app, so basically i try to learn how to create shipping app and then i've make the shipping rates by code and it works but the problem is my shipping rates by code is not showing as a shipping method on the checkout page any idea guys?  here's the code

 

 

 

async function createCarrierService(session) {
  const host = process.env.HOST || 'localhost'; // Pastikan `HOST` terdefinisi, jika tidak, gunakan fallback
  const callbackUrl = `https://${host}/carrier/shipping_rate`;

  const carrierServiceData = {
    carrier_service: {
      name: "Vins Shipping",
      callback_url: callbackUrl,
      service_discovery: true,
    },
  };

  try {
    const response = await axios.post(
      `https://${session.shop}/admin/api/2024-07/carrier_services.json`,
      carrierServiceData,
      {
        headers: {
          'X-Shopify-Access-Token': session.accessToken,
          'Content-Type': 'application/json',
        },
      }
    );
    console.log('Carrier service created:', response.data);
    return response.data;
  } catch (error) {
    console.error('Error creating carrier service:', error.response ? error.response.data : error.message);
    throw error; // Re-throw the error so it can be handled by the caller
  }
}

app.post("/carrier/shipping_rate", express.json(), async (req, res) => {
  console.log("Received shipping rate request:", req.body);
  
  // Mengambil informasi tujuan pengiriman
  const { destination } = req.body;
  
  // Logika penetapan rate pengiriman berdasarkan negara tujuan
  let rate = 0; // Default ke pengiriman gratis
  if (destination.country === "ID") {
    rate = 10000; // Rp 10.000 untuk pengiriman domestik (Indonesia)
  } else {
    rate = 50000; // Rp 50.000 untuk pengiriman internasional
  }
  
  const response = {
    rates: [
      {
        service_name: "Vins Standard Shipping",
        service_code: "VINS_STANDARD",
        total_price: rate.toString(),
        description: "5-7 business days",
        currency: "IDR",
      },
      {
        service_name: "Vins Express Shipping",
        service_code: "VINS_EXPRESS",
        total_price: rate.toString(), // Gunakan rate yang sama, tapi bisa disesuaikan jika ada perbedaan
        description: "2-3 business days",
        currency: "IDR",
      }
    ]
  };
  
  console.log("Sending response:", response);
  res.json(response);
});

 

 

 

 

alvinchrist_22_0-1726221137061.png

alvinchrist_22_1-1726221164936.png

 

Replies 0 (0)