Skip to content

Instantly share code, notes, and snippets.

@MarkKevin08
Created November 5, 2019 06:16
Show Gist options
  • Save MarkKevin08/a2362cc008daa4b1dbec78938c746305 to your computer and use it in GitHub Desktop.
Save MarkKevin08/a2362cc008daa4b1dbec78938c746305 to your computer and use it in GitHub Desktop.
Add shipping policy to cart and checkout
add_action('woocommerce_cart_totals_before_order_total', 'wcv_display_shipping_policy');
add_action('woocommerce_review_order_before_shipping', 'wcv_display_shipping_policy');
function wcv_display_shipping_policy() {
$product = gastronomy_get_productID_from_cart();
$vendor_id = get_post_field( 'post_author', $product);
$vendor_meta = array_map( function( $a ){ return $a[0]; }, get_user_meta( $vendor_id ) );
$vendor_shipping = array_key_exists('_wcv_shipping', $vendor_meta ) ? unserialize( $vendor_meta['_wcv_shipping'] ) : self::get_shipping_defaults();
$policy = $vendor_shipping[ 'shipping_policy' ];
if (! wcv_national_shipping_disable()){
echo '<tr><th>'. __( 'Shipping policy', 'wcvendors' ) .'</th><td data-title=" '. __( 'Infos', 'woocommerce' ) .' ">'. $policy .'</td></tr>';
}
else
{
$policy = __('No shipping form vendor !');
echo '<tr><th>'. __( 'Shipping policy', 'wcvendors' ) .'</th><td data-title=" '. __( 'Infos', 'woocommerce' ) .' ">'. $policy .'</td></tr>';
}
}
function gastronomy_get_productID_from_cart()
{
// CAUTION
// !!An order can only have products from 1 and only 1 vendor !!
// get first product of the cart => then we can get vendor ID
$items = WC()->cart->get_cart();
foreach($items as $item => $values) {
$_product[] = $values['data']->post;
break;
}
return $_product[0]->ID;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment