|
<?php |
|
function arti_mpme_custom_disable_me_shipping_field(){ |
|
|
|
global $post; |
|
|
|
$me_disabled = get_post_meta( $post->ID, '_me_product_disabled', true ); |
|
|
|
?> |
|
<div class="dokan-form-group"> |
|
<input id="_me_product_disabled" name="_me_product_disabled" value="yes" type="checkbox"<?php checked( $me_disabled, 'yes' ); ?>> |
|
<label for="_me_product_disabled" class="dokan-checkbox-inline"><?php _e( 'Desabilitar Melhor Envio para este produto.', 'arti-marketplace-melhorenvio' ); ?></label> |
|
<div class="dokan-clearfix"></div> |
|
</div> |
|
<?php |
|
|
|
} |
|
add_filter( 'dokan_product_options_shipping', 'arti_mpme_custom_disable_me_shipping_field', 10, 2 ); |
|
|
|
function arti_mpme_custom_update_product_meta( $product_id, $form_data ){ |
|
|
|
$me_disabled = isset( $form_data['_me_product_disabled'] ) ? 'yes' : 'no'; |
|
update_post_meta( $product_id, '_me_product_disabled', $me_disabled ); |
|
|
|
$product = wc_get_product( $product_id ); |
|
|
|
if( $product->has_child() ) { |
|
|
|
$children = $product->get_children(); |
|
|
|
foreach( $children as $child_id ) { |
|
update_post_meta( $child_id, '_me_product_disabled', $me_disabled ); |
|
} |
|
|
|
} |
|
|
|
} |
|
add_action( 'dokan_product_updated', 'arti_mpme_custom_update_product_meta', 10, 2 ); |
|
|
|
add_filter( 'arti_mpme_protopackage_set_product_context', function( $context, $item, $vendor_id ){ |
|
|
|
$product_id = $item['data']->get_id(); |
|
|
|
return arti_mpme_custom_is_me_disabled( $product_id ) ? 'melhor_envio_desabilitado' : $context; |
|
|
|
}, 10, 3 ); |
|
|
|
add_filter( 'arti_me_is_shipping_available', function( $is_available, $package ){ |
|
|
|
foreach( $package['contents'] as $item ){ |
|
|
|
$missing_dim = empty( $item['data']->get_width() ) || |
|
empty( $item['data']->get_height() ) || |
|
empty( $item['data']->get_length() ); |
|
|
|
if( |
|
arti_mpme_custom_is_me_disabled( $item['data']->get_id() ) |
|
|| $missing_dim // Se está faltando alguma dimensão, não cotar Melhor Envio. |
|
){ |
|
return false; |
|
} |
|
} |
|
|
|
return $is_available; |
|
|
|
}, 15, 2 ); |
|
|
|
function arti_mpme_custom_is_me_disabled( $product_id ){ |
|
$me_disabled = get_post_meta( $product_id, '_me_product_disabled', true ); |
|
return wc_string_to_bool( $me_disabled ); |
|
} |
|
|
|
add_filter( 'woocommerce_package_rates', function( $rates, $package ){ |
|
|
|
$me_rates = []; |
|
|
|
foreach( $rates as $key => $rate ){ |
|
if( in_array( $rate->get_method_id(), [ 'arti-melhorenvio', 'test-melhor-envio' ] ) ){ |
|
$me_rates[] = $key; |
|
} |
|
} |
|
|
|
return empty( $me_rates ) ? $rates : array_intersect_key( $rates, array_flip( $me_rates ) ); |
|
|
|
}, 15, 2 ); |