Skip to content

Instantly share code, notes, and snippets.

🇹🇼
寫 code 不一定會幫你賺到錢,但會寫 code 能生活的有意思點。

莫小屁 nczz

🇹🇼
寫 code 不一定會幫你賺到錢,但會寫 code 能生活的有意思點。
Block or report user

Report or block nczz

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View show_shipping_calculator_on_checkout_page.php
<?php
function action_woocommerce_before_checkout_form($wccm_autocreate_account) {
wc_cart_totals_shipping_html();
woocommerce_shipping_calculator('選取運送方式');
};
add_action('woocommerce_before_checkout_form', 'action_woocommerce_before_checkout_form', 10, 1);
function woocommerce_shipping_calculator_scripts() {
if (is_checkout()) {
if (wp_script_is('wc-cart', 'registered') && !wp_script_is('wc-cart', 'enqueued')) {
@nczz
nczz / woocommerce_orders_shortcode.php
Created Jun 27, 2019
[WooCommerce] 顯示訂單資訊的短碼(Shortcode) https://www.mxp.tw/8545/
View woocommerce_orders_shortcode.php
<?php
function woocommerce_orders() {
$user_id = get_current_user_id();
if ($user_id == 0) {
//沒登入就呼叫用戶後台的標準短碼
return do_shortcode('[woocommerce_my_account]');
} else {
ob_start();
//登入的用戶就撈 orders 的樣板來顯示訂單資料
wc_get_template('myaccount/my-orders.php', array(
@nczz
nczz / show_ga_pageviews_ext.php
Created Jun 23, 2019
[WordPress] 從 Google Analytics 匯入網站人氣的外掛組合技 https://www.mxp.tw/8529/
View show_ga_pageviews_ext.php
<?php
if (function_exists('gapp_options')) {
function gapp_get_total_pageviews($format = true) {
$options = gapp_options();
if (empty($options['gapp_token'])) {
//還沒授權過就先回傳預設值
return $options['gapp_defaultval'];
}
//起始日
$startDate = $options['gapp_startdate'];
@nczz
nczz / mxp_woocommerce_payment_gateways.php
Last active Jun 17, 2019
[WooCommerce] 付款方法項目的排序控制(訂單滿額、變更順序) https://www.mxp.tw/8513/
View mxp_woocommerce_payment_gateways.php
<?php
//較早觸發、無法改變付款方式順序
function mxp_woocommerce_payment_gateways($payment_gateways) {
// 判斷付款方式的 Class 名稱後根據邏輯比對取消註冊
foreach ($payment_gateways as $key => $gateways) {
if ($gateways == "WC_Gateway_COD") {
unset($payment_gateways[$key]);
}
}
return $payment_gateways;
@nczz
nczz / mxp_sort_shipping_methods_order.php
Created Jun 14, 2019
[WooCommerce] 運送方法項目的排序控制(指定順序)
View mxp_sort_shipping_methods_order.php
function mxp_sort_shipping_methods_order($available_shipping_methods, $package) {
if (!$available_shipping_methods) {
return;
}
// 運送方式,檢視 radio 元素,找到的 value 值
$sort_order = array(
'flat_rate:1' => null,
'some_other_shipping_method_slug:3' => null,
'flat_rate:2' => null,
);
@nczz
nczz / mxp_sort_shipping_services_by_cost.php
Created Jun 14, 2019
[WooCommerce] 運送方法項目的排序控制(由運費高低排序)
View mxp_sort_shipping_services_by_cost.php
<?php
function mxp_sort_shipping_services_by_cost($available_shipping_methods, $package) {
if (!$available_shipping_methods) {
return;
}
$rate_cost_set = array();
// 抽出運費多寡
foreach ($available_shipping_methods as $rate) {
$rate_cost_set[] = $rate->cost;
}
@nczz
nczz / wc_session_save_checkout_fields_data.php
Created Jun 14, 2019
用 WC_Session 紀錄結帳欄位資料與 WooCommerce 片段程式互動設計
View wc_session_save_checkout_fields_data.php
<?php
function save_data_before_open_new_page($value) {
$data = $_POST['post_data'];
parse_str(html_entity_decode($data), $pdata);
if (isset($pdata['billing_first_name']) && $pdata['billing_first_name'] != "") {
WC()->session->set('billing_first_name', $pdata['billing_first_name']);
}
if (isset($pdata['billing_phone']) && $pdata['billing_phone'] != "") {
WC()->session->set('billing_phone', $pdata['billing_phone']);
}
@nczz
nczz / get_location_from_cloudflare.js
Created Jun 11, 2019
從 Cloudflare 的 API 拿取瀏覽者的瀏覽細節
View get_location_from_cloudflare.js
jQuery.get('https://www.mxp.tw/cdn-cgi/trace', function(r) {
r = r.split('\n'), a = [];
for (var i = 0; i < r.length; ++i) a[r[i].split('=')[0]] = r[i].split('=')[1];
console.log(a['loc']);
})
@nczz
nczz / wp_add_privacy_page_edit_cap.php
Created Jun 4, 2019
[WooCommerce] 商店管理員無法編輯隱私權政策頁面
View wp_add_privacy_page_edit_cap.php
<?php
function add_privacy_page_edit_cap($caps, $cap, $user_id, $args) {
if ('manage_privacy_options' === $cap) {
$manage_name = is_multisite() ? 'manage_network' : 'manage_options';
$caps = array_diff($caps, [$manage_name]);
}
return $caps;
}
add_filter('map_meta_cap', 'add_privacy_page_edit_cap', 10, 4);
@nczz
nczz / mxp_shipping_fee_discount.php
Created Jun 3, 2019
程式化 WooCommerce 運費折扣
View mxp_shipping_fee_discount.php
<?php
function mxp_shipping_fee_discount() {
$fee = 0; // 新增運費底價
if (is_admin() && !defined('DOING_AJAX')) {
// 避免在管理介面下被觸發
return;
}
$total_pirce = 0;
foreach (WC()->cart->get_cart() as $item => $values) {
// 撈取購物車中的每一個商品
You can’t perform that action at this time.