Skip to content

Instantly share code, notes, and snippets.

View mrpsiho's full-sized avatar

Vitalii Kiiko mrpsiho

View GitHub Profile
@mrpsiho
mrpsiho / php
Created June 10, 2024 13:03
Example of dynamically populated choices for ACF select field
add_filter( 'acf/load_field', 'my_plugin_get_proper_form_templates', 10, 1 );
function my_plugin_get_proper_form_templates($field) {
if ( 'select_form_tmpl' === $field['name'] ) {
$forms_cfg = my_plugin_get_form_fields_cfg(); // external function, we get data from here
$choices = [];
if (array_keys($forms_cfg)) {
foreach (array_keys($forms_cfg) as $tmpl_name) {
$choices[$tmpl_name] = $forms_cfg[$tmpl_name]['label'];
}
@mrpsiho
mrpsiho / php
Created May 26, 2024 14:55
Displaying "From X" formatted price in WooCommerce
/*
Working snippet of code we are using in Uni CPO 4 plugin (https://wordpress.org/plugins/uni-woo-custom-product-options/).
This code display prices like "From $20.00" on product archives.
*/
add_filter('woocommerce_get_price_html', 'your_prefix_display_custom_price_on_archives', 10, 2);
function your_prefix_display_custom_price_on_archives($price, $product)
{
if (is_admin()) {
// do not display in admin area
return $price;
@mrpsiho
mrpsiho / gist:b4e61d6cb6add6c861cd01117baf8556
Created September 3, 2023 06:58
Builderius GraphQL query for WC Shop page template: products with filter by category
{
# taxonomy terms loop
taxonomy: terms (
query: {
taxonomy: "product_cat"
hide_empty: true
}
) @private {
terms: nodes {
id: term_id
@mrpsiho
mrpsiho / block.js
Last active March 17, 2022 10:34
Example of Gutenberg block with server side rendering.
(function(blocks, data, compose, element, blockEditor, components, serverSideRender) {
const el = element.createElement;
const registerBlockType = blocks.registerBlockType;
const { InspectorControls } = blockEditor;
const { Fragment } = element;
const { PanelBody, PanelRow, SelectControl } = components;
const useBlockProps = blockEditor.useBlockProps;
const { withSelect } = data;
registerBlockType('prefix/block_name', {
@mrpsiho
mrpsiho / functions.php
Created November 17, 2021 14:11
Conditionally display blocks on the page
add_filter( 'the_content', 'conditional_block_displaying', 1, 10 );
function conditional_block_displaying($content) {
$blocks = parse_blocks( $content );
$filtered_content = [];
foreach ( $blocks as $block ) {
if ( !wp_is_mobile() && 'core/paragraph' !== $block['blockName'] ) { // put your condition here
$filtered_content[] = $block;
}
@mrpsiho
mrpsiho / gist:60e5e8717b0af28e7b14a551c04a77d7
Created April 16, 2021 19:58
Get WP nav menu items - function helpers
function nav_sort_menu_items( $items ) {
return _nav_get_children( $items, 0, 0 );
}
// This is the inner recursive function - do not use directly
function _nav_get_children( $items, $parentId, $depth ) {
$children = array();
foreach ( $items as $id => $child ) {
if ( (int) $child->menu_item_parent === (int) $parentId ) {
$child->depth = $depth;
@mrpsiho
mrpsiho / functions.php
Created February 6, 2021 20:18
Pairing custom meta field with NOV in Uni CPO
add_action( 'woocommerce_product_options_pricing', 'uni_display_fields' );
add_action( 'woocommerce_process_product_meta', 'uni_save_fields' );
function uni_display_fields() {
woocommerce_wp_text_input(
array(
'id' => 'uni_paired_meta',
'label' => 'Meta field paired with NOV',
'description' => 'This value will be used instead of {uni_nov_cpo_paired_meta}',
)
@mrpsiho
mrpsiho / gist:73357ad1409a2b345dc865296f8279f6
Created January 29, 2019 08:42
Fixing background color selection in Flatsome theme
const flickty = jQuery('.woocommerce-product-gallery__wrapper').data('flickity');
if (typeof flickty !== 'undefined' && flickty.slides.length > 1) {
flickty.destroy();
jQuery('.woocommerce-product-gallery__wrapper').flickity({
draggable: false
});
}
@mrpsiho
mrpsiho / gist:12cadb35d84d21d844d10385f1c9f70d
Last active October 2, 2018 13:27
Exclude/change attributtes for billing/shipping fields on the WooCommerce checkout page
add_action( 'wp', 'uni_checkout_under_certain_circumstances' );
function uni_checkout_under_certain_circumstances() {
if ( function_exists( 'is_checkout' ) && ( is_checkout() || is_ajax() ) ) {
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
// the following line disables "notes" field
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
// an example of unsetting checkout fields
add_filter( 'woocommerce_checkout_fields', 'uni_unset_billing_checkout_fields', 10, 1 );
// an example of modifying atributtes of checkout fields
@mrpsiho
mrpsiho / gist:ddd09acf639ab486ca368461c80d68ff
Last active March 5, 2018 17:59
ShipperHQ for WC (ver. 1.3.2 https://wordpress.org/plugins/woo-shipperhq/). Replace 'populateAttributes' method with one mentioned below in order to hook in and read cart item dimensions instead of static ones added to a product. It makes ShipperHQ compatible with UniCPO 4 plugin.
/**
* Reads attributes from the item
*
* @param $productData
* @param $product
* @return array
*/
protected function populateAttributes($productData, $product)
{
$attributes = [];