post_type && in_array( $hook, array( 'post.php', 'post-new.php' ), true );
$is_term = 'product_cat' === $screen->taxonomy;
if ( ! $is_product && ! $is_term ) {
return;
}
wp_enqueue_script( 'wc-enhanced-select' );
wp_enqueue_style( 'woocommerce_admin_styles' );
wp_enqueue_style( 'hamrah-admin', HAMRAH_URL . 'assets/css/admin.css', array(), HAMRAH_VERSION );
wp_enqueue_script( 'hamrah-admin', HAMRAH_URL . 'assets/js/admin.js', array( 'jquery', 'wc-enhanced-select' ), HAMRAH_VERSION, true );
}
/**
* Adds the "Suggestions" tab.
*
* @param array $tabs Tabs.
* @return array
*/
public static function add_tab( $tabs ) {
$tabs['hamrah'] = array(
'label' => __( 'Suggestions', 'hamrah' ),
'target' => 'hamrah_product_data',
'class' => array( 'hide_if_grouped', 'hide_if_external' ),
'priority' => 65,
);
return $tabs;
}
/**
* Product data panel.
*/
public static function render_panel() {
global $product_object;
$product = $product_object instanceof WC_Product ? $product_object : null;
$config = $product ? Hamrah_Helpers::get_config( $product ) : Hamrah_Helpers::get_config( 0 );
$gift = $product ? Hamrah_Gift_Boxes::product_mode( $product ) : 'auto';
$boxes = $product ? array_map( 'absint', (array) $product->get_meta( Hamrah_Helpers::META_GIFT_BOXES ) ) : array();
?>
'hamrah_enabled',
'label' => __( 'Show suggestions', 'hamrah' ),
'description' => __( 'Offer the products below on this product page.', 'hamrah' ),
'value' => $config['enabled'] ? 'yes' : 'no',
)
);
woocommerce_wp_radio(
array(
'id' => 'hamrah_mode',
'label' => __( 'How they are sold', 'hamrah' ),
'value' => $config['mode'],
'options' => array(
Hamrah_Helpers::MODE_OPTIONAL => __( 'Optional – the customer ticks the ones they want', 'hamrah' ),
Hamrah_Helpers::MODE_REQUIRED => __( 'Required set – all are added together with this product', 'hamrah' ),
),
)
);
woocommerce_wp_text_input(
array(
'id' => 'hamrah_title',
'label' => __( 'Heading', 'hamrah' ),
'value' => $config['title'],
'placeholder' => Hamrah_Helpers::option( 'default_title', __( 'Complete your order', 'hamrah' ) ),
'desc_tip' => true,
'description' => __( 'Leave empty to use the default heading from the settings.', 'hamrah' ),
)
);
?>
|
|
|
|
|
$row ) {
self::render_item_row( $index, $row );
}
?>
'hamrah_gift',
'label' => __( 'Gift boxes', 'hamrah' ),
'value' => $gift,
'options' => array(
'auto' => __( 'Automatic (category and size)', 'hamrah' ),
'manual' => __( 'Only the boxes chosen below', 'hamrah' ),
'disabled' => __( 'No gift box for this product', 'hamrah' ),
),
'desc_tip' => true,
'description' => __( 'Gift boxes must be enabled in the plugin settings.', 'hamrah' ),
)
);
?>
0,
'qty' => 1,
'discount_type' => 'none',
'discount' => 0,
'checked' => false,
)
);
$base = 'hamrah_items[' . $index . ']';
?>
|
|
|
|
/>
|
|
get_meta( Hamrah_Helpers::META_IS_BOX );
$inner = $product ? $product->get_meta( Hamrah_Helpers::META_BOX_INNER ) : array();
$inner = wp_parse_args(
is_array( $inner ) ? $inner : array(),
array(
'l' => '',
'w' => '',
'h' => '',
)
);
echo '';
woocommerce_wp_checkbox(
array(
'id' => 'hamrah_is_gift_box',
'label' => __( 'Gift box', 'hamrah' ),
'description' => __( 'This product is a gift box that can be offered with other products.', 'hamrah' ),
'value' => $is_box ? 'yes' : 'no',
)
);
?>
update_meta_data( Hamrah_Helpers::META_ENABLED, isset( $_POST['hamrah_enabled'] ) ? 'yes' : 'no' );
$mode = isset( $_POST['hamrah_mode'] ) ? sanitize_key( wp_unslash( $_POST['hamrah_mode'] ) ) : Hamrah_Helpers::MODE_OPTIONAL;
$product->update_meta_data( Hamrah_Helpers::META_MODE, Hamrah_Helpers::MODE_REQUIRED === $mode ? Hamrah_Helpers::MODE_REQUIRED : Hamrah_Helpers::MODE_OPTIONAL );
$title = isset( $_POST['hamrah_title'] ) ? sanitize_text_field( wp_unslash( $_POST['hamrah_title'] ) ) : '';
$product->update_meta_data( Hamrah_Helpers::META_TITLE, $title );
// Rows are sanitised field by field in Hamrah_Helpers::sanitize_items().
$raw_items = isset( $_POST['hamrah_items'] ) && is_array( $_POST['hamrah_items'] ) ? wp_unslash( $_POST['hamrah_items'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$items = Hamrah_Helpers::sanitize_items( array_values( $raw_items ), $product->get_id() );
$items = array_values(
array_filter(
$items,
static function ( $row ) {
$item = wc_get_product( $row['product_id'] );
return $item && ! $item->is_type( array( 'variable', 'grouped', 'external' ) );
}
)
);
$product->update_meta_data( Hamrah_Helpers::META_ITEMS, $items );
$gift = isset( $_POST['hamrah_gift'] ) ? sanitize_key( wp_unslash( $_POST['hamrah_gift'] ) ) : 'auto';
$product->update_meta_data( Hamrah_Helpers::META_GIFT, in_array( $gift, array( 'auto', 'manual', 'disabled' ), true ) ? $gift : 'auto' );
$boxes = isset( $_POST['hamrah_gift_boxes'] ) ? array_filter( array_map( 'absint', (array) wp_unslash( $_POST['hamrah_gift_boxes'] ) ) ) : array();
$product->update_meta_data( Hamrah_Helpers::META_GIFT_BOXES, array_values( $boxes ) );
$is_box = isset( $_POST['hamrah_is_gift_box'] ) ? 'yes' : 'no';
$product->update_meta_data( Hamrah_Helpers::META_IS_BOX, $is_box );
$inner = array();
if ( isset( $_POST['hamrah_box_inner'] ) && is_array( $_POST['hamrah_box_inner'] ) ) {
$raw = wp_unslash( $_POST['hamrah_box_inner'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Each side is sanitised below.
foreach ( array( 'l', 'w', 'h' ) as $side ) {
$value = isset( $raw[ $side ] ) ? wc_format_decimal( sanitize_text_field( $raw[ $side ] ) ) : '';
$inner[ $side ] = '' === $value ? '' : (string) max( 0, (float) $value );
}
}
$product->update_meta_data( Hamrah_Helpers::META_BOX_INNER, $inner );
wp_cache_delete( 'all_box_ids', 'hamrah' );
}
/**
* Category field on the "Add category" form.
*/
public static function render_term_add() {
?>
term_id, Hamrah_Helpers::TERM_GIFT_BOXES, true );
$boxes = is_array( $boxes ) ? array_map( 'absint', $boxes ) : array();
?>
|
|