Skip to content

Instantly share code, notes, and snippets.

@marcusig
marcusig / linked-product--remove-configurable-product.php
Created May 11, 2023 15:59
Remove configurable product from the cart, and only keep linked products.
<?php
// Prevent the linked items to be removed automatically when removing the main product
add_filter( 'mkl_pc/remove_linked_items', '__return_false' );
// Remove the configurable item from the cart, if the linked items were added to the cart.
add_action( 'mkl_pc/added_linked_product_to_the_cart', function( $cart_item_key, $product_id ) {
WC()->cart->remove_cart_item( $cart_item_key );
// Add custom notice
wc_add_notice( '<a href="' . esc_url( wc_get_cart_url() ) . '" class="button wc-forward">' . esc_html__( 'View cart', 'woocommerce' ) . '</a>' . ' Din kjole design er blevet tilføjet til din kurv.' );
// Prevent default notice
@marcusig
marcusig / display-product-attributes.php
Created December 22, 2022 11:19
List all product attributes with the variation attributes
<?php
add_filter( 'woocommerce_get_item_data', 'wall_display_custom_item_data', 10, 2 );
function wall_display_custom_item_data( $cart_item_data, $cart_item ) {
$product = $cart_item['data'];
$attributes = wall_format_attributes( $product );
if ( ! $attributes ) {
return $cart_item_data;
}
@marcusig
marcusig / remove-html-from-admin-config.php
Created December 12, 2022 12:59
Product Configurator: Remove HTML from the Admin order configuration
<?php
// Strip the HTML from the configuration
add_filter( 'woocommerce_order_item_get_formatted_meta_data', function( $formatted_meta, $order_item ) {
// Handle "dynamic data" orders
foreach( $formatted_meta as $index => $meta ) {
if ( property_exists( $meta, 'value' ) && strpos( $meta->value, 'order-configuration' ) && function_exists( 'mkl_pc' ) && mkl_pc( 'admin' ) ) {
$meta->value = str_replace( '<div', "\n\r<div", $meta->value );
$meta->value = trim( strip_tags( $meta->value ) );
$formatted_meta[$index] = $meta;
@marcusig
marcusig / remove-html-from-config-export.php
Last active November 8, 2022 08:55
Remove HTML from the configuration export, and add the image link
<?php
// Trip the HTML from the configuration
add_filter( 'woe_get_order_product_value_Configuration', function( $value, $order, $item ) {
// Handle "dynamic data" orders
if ( strpos( $value, 'order-configuration-details' ) && function_exists( 'mkl_pc' ) && mkl_pc( 'admin' ) ) {
$meta = new stdClass();
$meta->value = $value;
$value = mkl_pc( 'admin' )->order->format_meta( $value, $meta, $item );
}
@marcusig
marcusig / execute-shortcodes-on-description.php
Created October 29, 2022 15:15
Execute shortcodes found in the description fields, by applying the WP filter 'the_content'
<?php
add_filter( 'mkl_product_configurator_get_front_end_data', function( $data ) {
// Filter the choices
if ( isset( $data['content'] ) && is_array( $data['content'] ) ) {
foreach( $data['content'] as $lin => $layer ) {
foreach( $layer['choices'] as $cin => $choice ) {
if ( isset( $choice['description'] ) ) {
$data['content'][$lin]['choices'][$cin]['description'] = apply_filters( 'the_content', $choice['description'] );
}
@marcusig
marcusig / bulk-increase-prices.js
Created October 28, 2022 15:28
Increase the prices in bulk, in the Bulk price edit screen
var factor = 1.05;
var round = true;
jQuery( '.bulk-choice input.extra-price' ).each( function( ind, item ) {
if ( ! jQuery( item ).val() ) return;
var value = jQuery( item ).val() * factor;
if ( round ) value = Math.round( value );
jQuery( item ).val( value ).trigger( 'change' );
});
@marcusig
marcusig / filter-order-meta-for-rest-api.php
Last active October 26, 2022 12:17
Filter the rest api result when exporting orders, replacing the meta value by display_value, which stores the readable data.
<?php
add_filter( "woocommerce_rest_prepare_shop_order_object", function( $response, $object, $request ) {
foreach( $response->data['line_items'] as $ind => $item ) {
foreach( $item['meta_data'] as $md_ind => $meta_data ) {
// If the value contains order-configuration-details
if ( strpos( $meta_data['value'], 'order-configuration-details' ) ) {
// then replace the value by display_value
$response->data['line_items'][$ind]['meta_data'][$md_ind]['value'] = $meta_data['display_value'];
}
@marcusig
marcusig / make-everything-scroll-on-mobile.css
Created October 20, 2022 11:31
[Configurator] Make everything scroll on mobile
@media (max-width: 660px) {
.mkl_pc.opened .mkl_pc_container {
overflow: auto;
bottom: 4em;
}
.mkl_pc.opened .mkl_pc_container .mkl_pc_viewer {
position: relative;
height: calc(50vh - 60px);
bottom: auto;
top: 0;
@marcusig
marcusig / change-tooltip-duration.js
Last active August 23, 2022 11:07
[configurator] Change tooltip duration / delay
// Change the tooltip. See the available options on https://atomiks.github.io/tippyjs/v6/all-props/#duration
wp.hooks.addFilter( 'PC.fe.tooltip.options', 'myDomain', function( options ) {
options.duration = [100, 400];
return options;
} );
@marcusig
marcusig / add-html-support-to-layer-name.php
Created August 23, 2022 10:11
[Configurator] add html support to layer name
<?php
// Add the new label, with html support: {{{ instead of {{
function my_prefix_frontend_configurator_layer_name() {
?>
<span class="text layer-name">{{{data.name}}}</span>
<?php
}
add_action( 'tmpl-mkl-pc-configurator-layer-item-button', 'my_prefix_frontend_configurator_layer_name', 10 );