Skip to content

Instantly share code, notes, and snippets.

View nczz's full-sized avatar
🇹🇼
寫 code 不一定會幫你賺到錢,但會寫 code 能生活的有意思點。

一介資男 nczz

🇹🇼
寫 code 不一定會幫你賺到錢,但會寫 code 能生活的有意思點。
View GitHub Profile
@nczz
nczz / woocommerce_orders_shortcode.php
Created June 27, 2019 14:30
[WooCommerce] 顯示訂單資訊的短碼(Shortcode) https://www.mxp.tw/8545/
<?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 June 23, 2019 13:33
[WordPress] 從 Google Analytics 匯入網站人氣的外掛組合技 https://www.mxp.tw/8529/
<?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 June 17, 2019 18:48
[WooCommerce] 付款方法項目的排序控制(訂單滿額、變更順序) https://www.mxp.tw/8513/
<?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
Last active December 26, 2019 09:26
[WooCommerce] 運送方法項目的排序控制(指定順序)
<?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 June 14, 2019 17:08
[WooCommerce] 運送方法項目的排序控制(由運費高低排序)
<?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 June 14, 2019 16:52
用 WC_Session 紀錄結帳欄位資料與 WooCommerce 片段程式互動設計
<?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 June 11, 2019 09:54
從 Cloudflare 的 API 拿取瀏覽者的瀏覽細節
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 June 4, 2019 15:27
[WooCommerce] 商店管理員無法編輯隱私權政策頁面
<?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
Last active March 31, 2020 17:02
程式化 WooCommerce 運費折扣
<?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) {
@nczz
nczz / canceled_order_email.php
Created May 25, 2019 12:28
[WooCommerce] 程式化觸發訂單狀態發信機制
<?php
function mxp_woo_cao_cancel_order_event($order_id){
$mailer = WC()->mailer();
$mails = $mailer->get_emails();
if ( ! empty( $mails ) ) {
foreach ( $mails as $mail ) {
if ( $mail->id == 'cancelled_order' ) {
$mail->trigger( $order_id );
}
}