id = 'frontglut_vendor_shipping'; $this->instance_id = absint( $instance_id ); $this->method_title = __( 'Vendor shipping', 'frontglut' ); $this->method_description = __( 'Each vendor sets their own rates (Dashboard → My store → Shipping).', 'frontglut' ); $this->supports = array( 'shipping-zones', 'instance-settings' ); $this->instance_form_fields = array( 'title' => array( 'title' => __( 'Title', 'frontglut' ), 'type' => 'text', 'default' => __( 'Vendor shipping', 'frontglut' ) ), ); $this->title = $this->get_option( 'title', __( 'Vendor shipping', 'frontglut' ) ); } public function calculate_shipping( $package = array() ) { $vid = isset( $package['frontglut_vendor'] ) ? (int) $package['frontglut_vendor'] : 0; if ( ! $vid ) { return; } $st = \FrontGlut\Modules\Vendors\Vendors::store( $vid ); $country = isset( $package['destination']['country'] ) ? strtoupper( $package['destination']['country'] ) : ''; $cost = null; foreach ( preg_split( '/\r\n|\r|\n/', (string) $st['ship_rates'] ) as $line ) { $p = array_map( 'trim', explode( '|', $line, 2 ) ); if ( 2 === count( $p ) && is_numeric( $p[1] ) ) { if ( strtoupper( $p[0] ) === $country ) { $cost = (float) $p[1]; break; } if ( '*' === $p[0] && null === $cost ) { $cost = (float) $p[1]; } } } if ( null === $cost && '' !== (string) $st['ship_default'] ) { $cost = (float) $st['ship_default']; } if ( null === $cost ) { return; } /* translators: %s: value. */ $label = sprintf( __( 'Shipping from %s', 'frontglut' ), $st['name'] ?: __( 'vendor', 'frontglut' ) ) . ( $st['ship_time'] ? ' (' . $st['ship_time'] . ')' : '' ); $this->add_rate( array( 'id' => $this->get_rate_id(), 'label' => $label, 'cost' => $cost, 'package' => $package ) ); } }