Skip to content

Instantly share code, notes, and snippets.

View deivamagalhaes's full-sized avatar

Deiva Magalhaes deivamagalhaes

View GitHub Profile
@deivamagalhaes
deivamagalhaes / php-engineer-improve-code.php
Last active April 17, 2019 00:38
SkyVerge - PHP Engineer - How would you improve this code? Please do not fork this Gist.
<?php
/**
* This code retrieves course data from an external API and displays it in the user's
* My Account area. A merchant has noticed that there's a delay when loading the page.
*
* 1) What changes would you suggest to reduce or remove that delay?
* I've refactored it to retrieve the data from the API using AJAX, so that this does not block the page.
*
* 2) Is there any other code changes that you would make?
* I'd also make the AJAX call secure (nonce) and move the JS and HTML to separate files
<?php
namespace App\Services;
use Analytics;
use Config;
use Illuminate\Support\Facades\App;
use Spatie\Analytics\Period;
/**
@deivamagalhaes
deivamagalhaes / functions.php
Last active March 13, 2020 21:19 — forked from jdeeburke/functions.php
Jilt support for WooCommerce Phone Orders
<?php
// Add the following snippet to your theme's functions.php file, or use a plugin like Code Snippets.
add_action( 'init', function() {
// Only make changes as long as Jilt for WooCommerce is installed and connected to Jilt
if ( function_exists( 'wc_jilt' ) && wc_jilt()->get_integration()->is_jilt_connected() ) {
// Disable Storefront JS for Phone Order carts
@deivamagalhaes
deivamagalhaes / snippet.php
Last active July 2, 2020 17:40
Track AddToCart event on Basel theme
<?php
function sv_add_filter_for_conditional_add_to_cart_fragments() {
// only make changes as long as Facebook for WooCommerce is installed
if ( function_exists( 'facebook_for_woocommerce' ) ) {
$events_tracker = facebook_for_woocommerce()->get_integration()->events_tracker;
$events_tracker->add_filter_for_conditional_add_to_cart_fragment();
}
@deivamagalhaes
deivamagalhaes / restrict_comments.php
Last active April 16, 2020 17:26
Code snippet to hide the comments and the comment form for all non-members
<?php
add_filter( 'comments_array', function ( $comments_flat, $post_id ) {
$post = get_post( $post_id );
// checks if the current post has any restricted content
if ( false !== strpos( $post->post_content, '[wcm_restrict' ) ) {
if ( ! wc_memberships_is_user_active_member()
@deivamagalhaes
deivamagalhaes / snippet.php
Last active April 15, 2020 21:59
Allows to sort the packing list excluding a given category tree
<?php
add_filter( 'woocommerce_get_product_terms', function ( $terms, $product_id, $taxonomy, $args ) {
if ( isset( $_GET['wc_pip_action'] ) && ! empty( $_GET['wc_pip_document'] ) && 'packing-list' == $_GET['wc_pip_document'] ) {
$terms = wp_get_post_terms( $product_id, $taxonomy, array(
'orderby' => 'parent',
'order' => 'DESC',
'exclude_tree' => '33',
@deivamagalhaes
deivamagalhaes / snippet.php
Created April 16, 2020 17:19
Add membership notes to user memberships GET in REST API
<?php
add_filter( 'woocommerce_rest_prepare_wc_user_membership', function ( WP_REST_Response $response, $post, $request ) {
$membership = wc_memberships_get_user_membership( $post->ID );
if ( $membership instanceof WC_Memberships_User_Membership ) {
$data = $response->get_data();
$data['notes'] = $membership->get_notes();
@deivamagalhaes
deivamagalhaes / snippet.php
Last active May 14, 2020 19:11
Hide checkout add-on from members - replace my-addon with the name of your add-on
<?php
add_filter( 'woocommerce_checkout_add_on_get_enabled', function( $value, $checkout_add_on ) {
// bail if Memberships is not active
if ( ! function_exists( 'wc_memberships_is_user_member' ) ) {
return $value;
}
@deivamagalhaes
deivamagalhaes / snippet.php
Created May 14, 2020 20:17
Increase Facebook feed generation interval to one hour
<?php
add_filter( 'wc_facebook_feed_generation_interval', function( $interval ) {
return HOUR_IN_SECONDS;
} );
@deivamagalhaes
deivamagalhaes / snippet.php
Created June 23, 2020 17:14
Unset transaction source for Braintree
<?php
add_filter( 'wc_braintree_transaction_data', 'sv_unset_transaction_source', 10, 2 );
/**
* Unset the transaction source to prevent authorizations from expiring too soon.
*
* @param array $request_data The transaction/sale data
* @param \WC_Braintree_API_Transaction_Request $request the request object
* @return array