Skip to content

Instantly share code, notes, and snippets.

@tarunranka
Created September 29, 2017 18:31
Show Gist options
  • Save tarunranka/c35d16e8aa6b55303cf5399e0de1ed22 to your computer and use it in GitHub Desktop.
Save tarunranka/c35d16e8aa6b55303cf5399e0de1ed22 to your computer and use it in GitHub Desktop.
function load_style()
{
$cssJSON = wp_remote_get(get_stylesheet_directory_uri().'/webpack-assets.json');
$css_data = json_decode($cssJSON['body']);
foreach ($css_data as $key => $cssjson) {
if ($cssjson && isset($cssjson->css)) {
$css_url = get_stylesheet_directory_uri().'/dist/'.$cssjson->css;
if (is_front_page() && $key == "home") {
echo "<link href='$css_url' rel='stylesheet'/>";
} elseif ((is_search() || is_archive()) && $key == "search") {
echo "<link href='$css_url' rel='stylesheet'/>";
} elseif (is_product() && $key == "product") {
echo "<link href='$css_url' rel='stylesheet'/>";
} elseif (is_cart() && $key == "cart") {
echo "<link href='$css_url' rel='stylesheet'/>";
} elseif (is_checkout() && $key == "checkout") {
echo "<link href='$css_url' rel='stylesheet'/>";
} elseif (is_account_page() && $key == "account") {
echo "<link href='$css_url' rel='stylesheet'/>";
} elseif ($key == "critical") {
echo "<link href='$css_url' rel='stylesheet'/>";
} elseif (is_page_template('template-rfq-form.php') && $key == 'rfq') {
echo "<link href='$css_url' rel='stylesheet'/>";
}
}
}
}
function load_script()
{
$currency = get_woocommerce_currency_symbol();
$base_country = wc_get_base_location();
$base_country = $base_country['country']; ?>
<script>
var sdgCurrency ="<?php echo $currency; ?>"
var sdgajax = {
ajax_url: "<?php echo WC()->ajax_url(); ?>",
wc_ajax_url : "<?php echo WC_AJAX::get_endpoint("%%endpoint%%")?>",
rest_url : "<?php echo esc_url_raw(rest_url()); ?>"
}
var themepath = "<?php echo get_stylesheet_directory_uri(); ?>";
var sdgcountry = "<?php echo $base_country ?>";
<?php if (is_checkout()) {
?>
var wc_checkout_params ={
'ajax_url' :"<?php echo WC()->ajax_url(); ?>",
'wc_ajax_url' : "<?php echo WC_AJAX::get_endpoint("%%endpoint%%"); ?>",
'update_order_review_nonce' :"<?php echo wp_create_nonce('update-order-review'); ?>",
'apply_coupon_nonce' :"<?php echo wp_create_nonce('apply-coupon'); ?>",
'remove_coupon_nonce' :"<?php echo wp_create_nonce('remove-coupon'); ?>",
'option_guest_checkout' :"<?php echo get_option('woocommerce_enable_guest_checkout'); ?>",
'checkout_url' :"<?php echo WC_AJAX::get_endpoint("checkout"); ?>",
'is_checkout' :"<?php echo is_page(wc_get_page_id('checkout')) && empty($wp->query_vars['order-pay']) && ! isset($wp->query_vars['order-received']) ? 1 : 0; ?>",
'debug_mode' :"<?php echo defined('WP_DEBUG') && WP_DEBUG; ?>",
'i18n_checkout_error' :"<?php echo esc_attr__('Error processing checkout. Please try again.', 'woocommerce'); ?>",
};
<?php
} ?>
</script>
<?php
$scriptJSON = wp_remote_get(get_stylesheet_directory_uri().'/webpack-assets.json');
$script_data = json_decode($scriptJSON['body']);
foreach ($script_data as $key => $script) {
if ($script && isset($script->js)) {
$script_url = get_stylesheet_directory_uri().'/dist/'.$script->js;
if (is_front_page() && $key == "home") {
echo "<script src='$script_url'></script>";
} elseif ((is_search() || is_archive()) && $key == "search") {
echo "<script src='$script_url'></script>";
} elseif (is_product() && $key == "product") {
echo "<script src='$script_url'></script>";
} elseif (is_cart() && $key == "cart") {
echo "<script src='$script_url'></script>";
} elseif (is_checkout() && $key == "checkout") {
echo "<script src='$script_url'></script>";
} elseif ($key == "vendor") {
echo "<script src='$script_url'></script>";
} elseif ($key == "critical") {
echo "<script src='$script_url'></script>";
} elseif (is_page_template('template-rfq-form.php') && $key == 'rfq') {
echo "<script src='$script_url'></script>";
} elseif (is_account_page() && $key == "account") {
echo "<script src='$script_url'></script>";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment